blob: 484dcd466c195cade593a3255cfbde7c96bbf8b0 [file] [log] [blame]
David Reissac549552008-06-10 22:56:59 +00001-module(thrift_protocol).
2
3-export([new/2,
4 write/2,
5 read/2,
6 skip/2,
David Reiss90b40832008-06-10 22:58:52 +00007 flush_transport/1,
David Reissc11734e2008-06-11 00:59:48 +00008 close_transport/1,
David Reiss6b3e40f2008-06-11 00:59:03 +00009 typeid_to_atom/1
10 ]).
David Reissac549552008-06-10 22:56:59 +000011
David Reiss6b3e40f2008-06-11 00:59:03 +000012-export([behaviour_info/1]).
David Reissac549552008-06-10 22:56:59 +000013
14-include("thrift_constants.hrl").
15-include("thrift_protocol.hrl").
16
17-record(protocol, {module, data}).
18
19behaviour_info(callbacks) ->
20 [
21 {read, 2},
David Reiss90b40832008-06-10 22:58:52 +000022 {write, 2},
David Reissc11734e2008-06-11 00:59:48 +000023 {flush_transport, 1},
24 {close_transport, 1}
David Reissac549552008-06-10 22:56:59 +000025 ];
26behaviour_info(_Else) -> undefined.
27
David Reissac549552008-06-10 22:56:59 +000028new(Module, Data) when is_atom(Module) ->
29 {ok, #protocol{module = Module,
30 data = Data}}.
31
David Reiss90b40832008-06-10 22:58:52 +000032flush_transport(#protocol{module = Module,
33 data = Data}) ->
34 Module:flush_transport(Data).
35
David Reissc11734e2008-06-11 00:59:48 +000036close_transport(#protocol{module = Module,
37 data = Data}) ->
38 Module:close_transport(Data).
39
David Reissac549552008-06-10 22:56:59 +000040typeid_to_atom(?tType_STOP) -> field_stop;
41typeid_to_atom(?tType_VOID) -> void;
42typeid_to_atom(?tType_BOOL) -> bool;
43typeid_to_atom(?tType_BYTE) -> byte;
44typeid_to_atom(?tType_DOUBLE) -> double;
45typeid_to_atom(?tType_I16) -> i16;
46typeid_to_atom(?tType_I32) -> i32;
47typeid_to_atom(?tType_I64) -> i64;
48typeid_to_atom(?tType_STRING) -> string;
49typeid_to_atom(?tType_STRUCT) -> struct;
50typeid_to_atom(?tType_MAP) -> map;
51typeid_to_atom(?tType_SET) -> set;
52typeid_to_atom(?tType_LIST) -> list.
David Reissae756f42008-06-10 22:57:11 +000053
54term_to_typeid(void) -> ?tType_VOID;
55term_to_typeid(bool) -> ?tType_BOOL;
56term_to_typeid(byte) -> ?tType_BYTE;
57term_to_typeid(double) -> ?tType_DOUBLE;
58term_to_typeid(i16) -> ?tType_I16;
59term_to_typeid(i32) -> ?tType_I32;
60term_to_typeid(i64) -> ?tType_I64;
61term_to_typeid(string) -> ?tType_STRING;
62term_to_typeid({struct, _}) -> ?tType_STRUCT;
63term_to_typeid({map, _, _}) -> ?tType_MAP;
64term_to_typeid({set, _}) -> ?tType_SET;
65term_to_typeid({list, _}) -> ?tType_LIST.
66
David Reissae756f42008-06-10 22:57:11 +000067%% Structure is like:
68%% [{Fid, Type}, ...]
69read(IProto, {struct, Structure}) when is_list(Structure) ->
David Reisseea82982008-06-10 22:58:21 +000070 IndexList = case length(Structure) of
71 N when N > 0 -> lists:seq(1, N);
72 _ -> []
73 end,
74
David Reissae756f42008-06-10 22:57:11 +000075 SWithIndices = [{Fid, {Type, Index}} ||
76 {{Fid, Type}, Index} <-
David Reisseea82982008-06-10 22:58:21 +000077 lists:zip(Structure, IndexList)],
David Reissae756f42008-06-10 22:57:11 +000078 % Fid -> {Type, Index}
79 SDict = dict:from_list(SWithIndices),
80
81
82 ok = read(IProto, struct_begin),
83 RDict = read_struct_loop(IProto, SDict, dict:new()),
84
85 List = [case dict:find(Index, RDict) of
86 {ok, Val} -> Val;
87 error -> undefined
David Reisseea82982008-06-10 22:58:21 +000088 end || Index <- IndexList],
David Reissae756f42008-06-10 22:57:11 +000089 {ok, list_to_tuple(List)};
90
David Reiss76f0d112008-06-10 22:57:35 +000091read(IProto, {struct, {Module, StructureName}}) when is_atom(Module),
92 is_atom(StructureName) ->
93 case read(IProto, Module:struct_info(StructureName)) of
94 {ok, StructureElems} ->
95 {ok, list_to_tuple([StructureName | tuple_to_list(StructureElems)])};
96 Else -> Else
97 end;
98
David Reissae756f42008-06-10 22:57:11 +000099read(IProto, {list, Type}) ->
100 #protocol_list_begin{etype = EType, size = Size} =
101 read(IProto, list_begin),
David Reiss6b3e40f2008-06-11 00:59:03 +0000102 List = [Result || {ok, Result} <-
David Reisseea82982008-06-10 22:58:21 +0000103 [read(IProto, Type) || _X <- lists:duplicate(Size, 0)]],
David Reissae756f42008-06-10 22:57:11 +0000104 ok = read(IProto, list_end),
105 {ok, List};
106
107read(IProto, {map, KeyType, ValType}) ->
108 #protocol_map_begin{size = Size} =
109 read(IProto, map_begin),
110
David Reiss6b3e40f2008-06-11 00:59:03 +0000111 List = [{Key, Val} || {{ok, Key}, {ok, Val}} <-
David Reissae756f42008-06-10 22:57:11 +0000112 [{read(IProto, KeyType),
David Reisseea82982008-06-10 22:58:21 +0000113 read(IProto, ValType)} || _X <- lists:duplicate(Size, 0)]],
David Reissae756f42008-06-10 22:57:11 +0000114 ok = read(IProto, map_end),
115 {ok, dict:from_list(List)};
116
117read(IProto, {set, Type}) ->
118 #protocol_set_begin{etype = _EType,
119 size = Size} =
120 read(IProto, set_begin),
David Reiss6b3e40f2008-06-11 00:59:03 +0000121 List = [Result || {ok, Result} <-
David Reisseea82982008-06-10 22:58:21 +0000122 [read(IProto, Type) || _X <- lists:duplicate(Size, 0)]],
David Reissae756f42008-06-10 22:57:11 +0000123 ok = read(IProto, set_end),
124 {ok, sets:from_list(List)};
125
David Reissac549552008-06-10 22:56:59 +0000126read(#protocol{module = Module,
127 data = ModuleData}, ProtocolType) ->
128 Module:read(ModuleData, ProtocolType).
129
David Reissae756f42008-06-10 22:57:11 +0000130read_struct_loop(IProto, SDict, RDict) ->
131 #protocol_field_begin{type = FType, id = Fid, name = Name} =
132 thrift_protocol:read(IProto, field_begin),
133 case {FType, Fid} of
134 {?tType_STOP, _} ->
135 RDict;
136 _Else ->
137 case dict:find(Fid, SDict) of
138 {ok, {Type, Index}} ->
139 {ok, Val} = read(IProto, Type),
140 thrift_protocol:read(IProto, field_end),
141 NewRDict = dict:store(Index, Val, RDict),
142 read_struct_loop(IProto, SDict, NewRDict);
143 _Else2 ->
144 error_logger:info_msg("Skipping fid ~p~n", [Fid]),
145 FTypeAtom = thrift_protocol:typeid_to_atom(FType),
146 thrift_protocol:skip(IProto, FTypeAtom),
147 read(IProto, field_end),
148 read_struct_loop(IProto, SDict, RDict)
149 end
150 end.
David Reissac549552008-06-10 22:56:59 +0000151
152
153skip(Proto, struct) ->
154 ok = read(Proto, struct_begin),
155 ok = skip_struct_loop(Proto),
156 ok = read(Proto, struct_end);
157
158skip(Proto, map) ->
159 Map = read(Proto, map_begin),
160 ok = skip_map_loop(Proto, Map),
161 ok = read(Proto, map_end);
162
163skip(Proto, set) ->
164 Set = read(Proto, set_begin),
165 ok = skip_set_loop(Proto, Set),
166 ok = read(Proto, set_end);
167
168skip(Proto, list) ->
169 List = read(Proto, list_begin),
170 ok = skip_list_loop(Proto, List),
David Reiss6b3e40f2008-06-11 00:59:03 +0000171 ok = read(Proto, list_end);
David Reissac549552008-06-10 22:56:59 +0000172
173skip(Proto, Type) when is_atom(Type) ->
174 _Ignore = read(Proto, Type),
175 ok.
176
177
178skip_struct_loop(Proto) ->
179 #protocol_field_begin{type = Type} = read(Proto, field_begin),
180 case Type of
181 ?tType_STOP ->
182 ok;
183 _Else ->
184 skip(Proto, Type),
185 ok = read(Proto, field_end),
186 skip_struct_loop(Proto)
187 end.
188
189skip_map_loop(Proto, Map = #protocol_map_begin{ktype = Ktype,
190 vtype = Vtype,
191 size = Size}) ->
192 case Size of
193 N when N > 0 ->
194 skip(Proto, Ktype),
195 skip(Proto, Vtype),
196 skip_map_loop(Proto,
197 Map#protocol_map_begin{size = Size - 1});
198 0 -> ok
199 end.
200
201skip_set_loop(Proto, Map = #protocol_set_begin{etype = Etype,
202 size = Size}) ->
203 case Size of
204 N when N > 0 ->
205 skip(Proto, Etype),
206 skip_set_loop(Proto,
207 Map#protocol_set_begin{size = Size - 1});
208 0 -> ok
209 end.
210
211skip_list_loop(Proto, Map = #protocol_list_begin{etype = Etype,
212 size = Size}) ->
213 case Size of
214 N when N > 0 ->
215 skip(Proto, Etype),
216 skip_list_loop(Proto,
217 Map#protocol_list_begin{size = Size - 1});
218 0 -> ok
219 end.
David Reissae756f42008-06-10 22:57:11 +0000220
221
222%%--------------------------------------------------------------------
223%% Function: write(OProto, {Type, Data}) -> ok
David Reiss6b3e40f2008-06-11 00:59:03 +0000224%%
David Reissae756f42008-06-10 22:57:11 +0000225%% Type = {struct, StructDef} |
226%% {list, Type} |
227%% {map, KeyType, ValType} |
228%% {set, Type} |
229%% BaseType
230%%
231%% Data =
232%% tuple() -- for struct
233%% | list() -- for list
234%% | dictionary() -- for map
235%% | set() -- for set
236%% | term() -- for base types
237%%
David Reiss6b3e40f2008-06-11 00:59:03 +0000238%% Description:
David Reissae756f42008-06-10 22:57:11 +0000239%%--------------------------------------------------------------------
240write(Proto, {{struct, StructDef}, Data})
David Reiss76f0d112008-06-10 22:57:35 +0000241 when is_list(StructDef), is_tuple(Data), length(StructDef) == size(Data) - 1 ->
242
243 [StructName | Elems] = tuple_to_list(Data),
244 ok = write(Proto, #protocol_struct_begin{name = StructName}),
245 ok = struct_write_loop(Proto, StructDef, Elems),
David Reissae756f42008-06-10 22:57:11 +0000246 ok = write(Proto, struct_end),
247 ok;
248
David Reiss76f0d112008-06-10 22:57:35 +0000249write(Proto, {{struct, {Module, StructureName}}, Data})
250 when is_atom(Module),
251 is_atom(StructureName),
252 element(1, Data) =:= StructureName ->
253 StructType = Module:struct_info(StructureName),
254 write(Proto, {Module:struct_info(StructureName), Data});
255
David Reissae756f42008-06-10 22:57:11 +0000256write(Proto, {{list, Type}, Data})
257 when is_list(Data) ->
258 ok = write(Proto,
259 #protocol_list_begin{
260 etype = term_to_typeid(Type),
261 size = length(Data)
262 }),
263 lists:foreach(fun(Elem) ->
264 ok = write(Proto, {Type, Elem})
265 end,
266 Data),
267 ok = write(Proto, list_end),
268 ok;
269
270write(Proto, {{map, KeyType, ValType}, Data}) ->
David Reissae756f42008-06-10 22:57:11 +0000271 ok = write(Proto,
272 #protocol_map_begin{
273 ktype = term_to_typeid(KeyType),
274 vtype = term_to_typeid(ValType),
David Reiss6b3e40f2008-06-11 00:59:03 +0000275 size = dict:size(Data)
276 }),
277 dict:fold(fun(KeyData, ValData, _Acc) ->
278 ok = write(Proto, {KeyType, KeyData}),
279 ok = write(Proto, {ValType, ValData})
280 end,
281 _AccO = ok,
282 Data),
David Reissae756f42008-06-10 22:57:11 +0000283 ok = write(Proto, map_end),
284 ok;
285
286write(Proto, {{set, Type}, Data}) ->
287 true = sets:is_set(Data),
David Reissae756f42008-06-10 22:57:11 +0000288 ok = write(Proto,
289 #protocol_set_begin{
290 etype = term_to_typeid(Type),
David Reiss6b3e40f2008-06-11 00:59:03 +0000291 size = sets:size(Data)
292 }),
293 sets:fold(fun(Elem, _Acc) ->
294 ok = write(Proto, {Type, Elem})
295 end,
296 _Acc0 = ok,
297 Data),
David Reissae756f42008-06-10 22:57:11 +0000298 ok = write(Proto, set_end),
299 ok;
300
301write(#protocol{module = Module,
302 data = ModuleData}, Data) ->
303 Module:write(ModuleData, Data).
304
David Reissae756f42008-06-10 22:57:11 +0000305struct_write_loop(Proto, [{Fid, Type} | RestStructDef], [Data | RestData]) ->
306 case Data of
307 undefined ->
308 % null fields are skipped in response
309 skip;
310 _ ->
311 ok = write(Proto,
312 #protocol_field_begin{
313 type = term_to_typeid(Type),
314 id = Fid
315 }),
316 ok = write(Proto, {Type, Data}),
317 ok = write(Proto, field_end)
318 end,
319 struct_write_loop(Proto, RestStructDef, RestData);
320struct_write_loop(Proto, [], []) ->
321 ok = write(Proto, field_stop),
322 ok.