2 -- Licensed to the Apache Software Foundation (ASF) under one
3 -- or more contributor license agreements. See the NOTICE file
4 -- distributed with this work for additional information
5 -- regarding copyright ownership. The ASF licenses this file
6 -- to you under the Apache License, Version 2.0 (the
7 -- "License"); you may not use this file except in compliance
8 -- with the License. You may obtain a copy of the License at
10 -- http://www.apache.org/licenses/LICENSE-2.0
12 -- Unless required by applicable law or agreed to in writing,
13 -- software distributed under the License is distributed on an
14 -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 -- KIND, either express or implied. See the License for the
16 -- specific language governing permissions and limitations
22 --setmetatable(thrift, {__index = _G}) --> perf hit for accessing global methods
25 package.cpath = package.cpath .. ';bin/?.so' -- TODO FIX
27 if type(obj) == 'table' and
29 type(obj.__type) == 'string' then
36 if e and e.__tostring then
43 function ttable_size(t)
45 for k, v in pairs(t) do
80 -- Recursive __index function to achieve inheritance
81 function __tobj_index(self, key)
82 local v = rawget(self, key)
87 local p = rawget(self, '__parent')
89 return __tobj_index(p, key)
95 -- Basic Thrift-Lua Object
99 __index = __tobj_index
102 function __TObject:new(init_obj)
104 if ttype(obj) == 'table' then
108 -- Use the __parent key and the __index function to achieve inheritance
110 setmetatable(obj, __TObject.__mt)
114 -- Return a string representation of any lua variable
115 function thrift_print_r(t)
117 local ltype = type(t)
118 if (ltype == 'table') then
120 for key,value in pairs(t) do
121 ret = ret .. tostring(key) .. '=' .. thrift_print_r(value) .. ' '
124 elseif ltype == 'string' then
125 ret = ret .. "'" .. tostring(t) .. "'"
127 ret = ret .. tostring(t)
133 TException = __TObject:new{
136 __type = 'TException'
138 function TException:__tostring()
140 return string.format('%s: %s', self.__type, self.message)
143 if self.errorCode and self.__errorCodeToString then
144 message = string.format('%d: %s', self.errorCode, self:__errorCodeToString())
146 message = thrift_print_r(self)
148 return string.format('%s:%s', self.__type, message)
152 TApplicationException = TException:new{
155 INVALID_MESSAGE_TYPE = 2,
156 WRONG_METHOD_NAME = 3,
161 INVALID_TRANSFORM = 8,
162 INVALID_PROTOCOL = 9,
163 UNSUPPORTED_CLIENT_TYPE = 10,
165 __type = 'TApplicationException'
168 function TApplicationException:__errorCodeToString()
169 if self.errorCode == self.UNKNOWN_METHOD then
170 return 'Unknown method'
171 elseif self.errorCode == self.INVALID_MESSAGE_TYPE then
172 return 'Invalid message type'
173 elseif self.errorCode == self.WRONG_METHOD_NAME then
174 return 'Wrong method name'
175 elseif self.errorCode == self.BAD_SEQUENCE_ID then
176 return 'Bad sequence ID'
177 elseif self.errorCode == self.MISSING_RESULT then
178 return 'Missing result'
179 elseif self.errorCode == self.INTERNAL_ERROR then
180 return 'Internal error'
181 elseif self.errorCode == self.PROTOCOL_ERROR then
182 return 'Protocol error'
183 elseif self.errorCode == self.INVALID_TRANSFORM then
184 return 'Invalid transform'
185 elseif self.errorCode == self.INVALID_PROTOCOL then
186 return 'Invalid protocol'
187 elseif self.errorCode == self.UNSUPPORTED_CLIENT_TYPE then
188 return 'Unsupported client type'
190 return 'Default (unknown)'
194 function TException:read(iprot)
195 iprot:readStructBegin()
197 local fname, ftype, fid = iprot:readFieldBegin()
198 if ftype == TType.STOP then
201 if ftype == TType.STRING then
202 self.message = iprot:readString()
207 if ftype == TType.I32 then
208 self.errorCode = iprot:readI32()
217 iprot:readStructEnd()
220 function TException:write(oprot)
221 oprot:writeStructBegin('TApplicationException')
223 oprot:writeFieldBegin('message', TType.STRING, 1)
224 oprot:writeString(self.message)
225 oprot:writeFieldEnd()
227 if self.errorCode then
228 oprot:writeFieldBegin('type', TType.I32, 2)
229 oprot:writeI32(self.errorCode)
230 oprot:writeFieldEnd()
232 oprot:writeFieldStop()
233 oprot:writeStructEnd()
236 -- Basic Client (used in generated lua code)
237 __TClient = __TObject:new{
238 __type = '__TClient',
241 function __TClient:new(obj)
242 if ttype(obj) ~= 'table' then
243 error('TClient must be initialized with a table')
248 obj.iprot = obj.protocol
249 obj.oprot = obj.protocol
251 elseif not obj.iprot then
252 error('You must provide ' .. ttype(self) .. ' with an iprot')
254 if not obj.oprot then
255 obj.oprot = obj.iprot
258 return __TObject.new(self, obj)
261 function __TClient:close()
262 self.iprot.trans:close()
263 self.oprot.trans:close()
266 -- Basic Processor (used in generated lua code)
267 __TProcessor = __TObject:new{
268 __type = '__TProcessor'
270 function __TProcessor:new(obj)
271 if ttype(obj) ~= 'table' then
272 error('TProcessor must be initialized with a table')
275 -- Ensure a handler is provided
276 if not obj.handler then
277 error('You must provide ' .. ttype(self) .. ' with a handler')
280 return __TObject.new(self, obj)