blob: 613c5a0e98b83776553f462f8d6863c17572ad04 [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),
David Reissa863db62008-06-11 01:13:12 +000083 RTuple0 = erlang:make_tuple(length(Structure), undefined),
David Reissae756f42008-06-10 22:57:11 +000084
David Reissa863db62008-06-11 01:13:12 +000085 RTuple1 = read_struct_loop(IProto, SDict, RTuple0),
86 {ok, RTuple1};
David Reissae756f42008-06-10 22:57:11 +000087
David Reiss76f0d112008-06-10 22:57:35 +000088read(IProto, {struct, {Module, StructureName}}) when is_atom(Module),
89 is_atom(StructureName) ->
90 case read(IProto, Module:struct_info(StructureName)) of
91 {ok, StructureElems} ->
92 {ok, list_to_tuple([StructureName | tuple_to_list(StructureElems)])};
93 Else -> Else
94 end;
95
David Reissae756f42008-06-10 22:57:11 +000096read(IProto, {list, Type}) ->
97 #protocol_list_begin{etype = EType, size = Size} =
98 read(IProto, list_begin),
David Reiss6b3e40f2008-06-11 00:59:03 +000099 List = [Result || {ok, Result} <-
David Reisseea82982008-06-10 22:58:21 +0000100 [read(IProto, Type) || _X <- lists:duplicate(Size, 0)]],
David Reissae756f42008-06-10 22:57:11 +0000101 ok = read(IProto, list_end),
102 {ok, List};
103
104read(IProto, {map, KeyType, ValType}) ->
105 #protocol_map_begin{size = Size} =
106 read(IProto, map_begin),
107
David Reiss6b3e40f2008-06-11 00:59:03 +0000108 List = [{Key, Val} || {{ok, Key}, {ok, Val}} <-
David Reissae756f42008-06-10 22:57:11 +0000109 [{read(IProto, KeyType),
David Reisseea82982008-06-10 22:58:21 +0000110 read(IProto, ValType)} || _X <- lists:duplicate(Size, 0)]],
David Reissae756f42008-06-10 22:57:11 +0000111 ok = read(IProto, map_end),
112 {ok, dict:from_list(List)};
113
114read(IProto, {set, Type}) ->
115 #protocol_set_begin{etype = _EType,
116 size = Size} =
117 read(IProto, set_begin),
David Reiss6b3e40f2008-06-11 00:59:03 +0000118 List = [Result || {ok, Result} <-
David Reisseea82982008-06-10 22:58:21 +0000119 [read(IProto, Type) || _X <- lists:duplicate(Size, 0)]],
David Reissae756f42008-06-10 22:57:11 +0000120 ok = read(IProto, set_end),
121 {ok, sets:from_list(List)};
122
David Reissac549552008-06-10 22:56:59 +0000123read(#protocol{module = Module,
124 data = ModuleData}, ProtocolType) ->
125 Module:read(ModuleData, ProtocolType).
126
David Reissa863db62008-06-11 01:13:12 +0000127read_struct_loop(IProto, SDict, RTuple) ->
David Reissae756f42008-06-10 22:57:11 +0000128 #protocol_field_begin{type = FType, id = Fid, name = Name} =
129 thrift_protocol:read(IProto, field_begin),
130 case {FType, Fid} of
131 {?tType_STOP, _} ->
David Reissa863db62008-06-11 01:13:12 +0000132 RTuple;
David Reissae756f42008-06-10 22:57:11 +0000133 _Else ->
134 case dict:find(Fid, SDict) of
135 {ok, {Type, Index}} ->
136 {ok, Val} = read(IProto, Type),
137 thrift_protocol:read(IProto, field_end),
David Reissa863db62008-06-11 01:13:12 +0000138 NewRTuple = setelement(Index, RTuple, Val),
139 read_struct_loop(IProto, SDict, NewRTuple);
David Reissae756f42008-06-10 22:57:11 +0000140 _Else2 ->
141 error_logger:info_msg("Skipping fid ~p~n", [Fid]),
142 FTypeAtom = thrift_protocol:typeid_to_atom(FType),
143 thrift_protocol:skip(IProto, FTypeAtom),
144 read(IProto, field_end),
David Reissa863db62008-06-11 01:13:12 +0000145 read_struct_loop(IProto, SDict, RTuple)
David Reissae756f42008-06-10 22:57:11 +0000146 end
147 end.
David Reissac549552008-06-10 22:56:59 +0000148
149
150skip(Proto, struct) ->
151 ok = read(Proto, struct_begin),
152 ok = skip_struct_loop(Proto),
153 ok = read(Proto, struct_end);
154
155skip(Proto, map) ->
156 Map = read(Proto, map_begin),
157 ok = skip_map_loop(Proto, Map),
158 ok = read(Proto, map_end);
159
160skip(Proto, set) ->
161 Set = read(Proto, set_begin),
162 ok = skip_set_loop(Proto, Set),
163 ok = read(Proto, set_end);
164
165skip(Proto, list) ->
166 List = read(Proto, list_begin),
167 ok = skip_list_loop(Proto, List),
David Reiss6b3e40f2008-06-11 00:59:03 +0000168 ok = read(Proto, list_end);
David Reissac549552008-06-10 22:56:59 +0000169
170skip(Proto, Type) when is_atom(Type) ->
171 _Ignore = read(Proto, Type),
172 ok.
173
174
175skip_struct_loop(Proto) ->
176 #protocol_field_begin{type = Type} = read(Proto, field_begin),
177 case Type of
178 ?tType_STOP ->
179 ok;
180 _Else ->
181 skip(Proto, Type),
182 ok = read(Proto, field_end),
183 skip_struct_loop(Proto)
184 end.
185
186skip_map_loop(Proto, Map = #protocol_map_begin{ktype = Ktype,
187 vtype = Vtype,
188 size = Size}) ->
189 case Size of
190 N when N > 0 ->
191 skip(Proto, Ktype),
192 skip(Proto, Vtype),
193 skip_map_loop(Proto,
194 Map#protocol_map_begin{size = Size - 1});
195 0 -> ok
196 end.
197
198skip_set_loop(Proto, Map = #protocol_set_begin{etype = Etype,
199 size = Size}) ->
200 case Size of
201 N when N > 0 ->
202 skip(Proto, Etype),
203 skip_set_loop(Proto,
204 Map#protocol_set_begin{size = Size - 1});
205 0 -> ok
206 end.
207
208skip_list_loop(Proto, Map = #protocol_list_begin{etype = Etype,
209 size = Size}) ->
210 case Size of
211 N when N > 0 ->
212 skip(Proto, Etype),
213 skip_list_loop(Proto,
214 Map#protocol_list_begin{size = Size - 1});
215 0 -> ok
216 end.
David Reissae756f42008-06-10 22:57:11 +0000217
218
219%%--------------------------------------------------------------------
220%% Function: write(OProto, {Type, Data}) -> ok
David Reiss6b3e40f2008-06-11 00:59:03 +0000221%%
David Reissae756f42008-06-10 22:57:11 +0000222%% Type = {struct, StructDef} |
223%% {list, Type} |
224%% {map, KeyType, ValType} |
225%% {set, Type} |
226%% BaseType
227%%
228%% Data =
229%% tuple() -- for struct
230%% | list() -- for list
231%% | dictionary() -- for map
232%% | set() -- for set
233%% | term() -- for base types
234%%
David Reiss6b3e40f2008-06-11 00:59:03 +0000235%% Description:
David Reissae756f42008-06-10 22:57:11 +0000236%%--------------------------------------------------------------------
237write(Proto, {{struct, StructDef}, Data})
David Reiss76f0d112008-06-10 22:57:35 +0000238 when is_list(StructDef), is_tuple(Data), length(StructDef) == size(Data) - 1 ->
239
240 [StructName | Elems] = tuple_to_list(Data),
241 ok = write(Proto, #protocol_struct_begin{name = StructName}),
242 ok = struct_write_loop(Proto, StructDef, Elems),
David Reissae756f42008-06-10 22:57:11 +0000243 ok = write(Proto, struct_end),
244 ok;
245
David Reiss76f0d112008-06-10 22:57:35 +0000246write(Proto, {{struct, {Module, StructureName}}, Data})
247 when is_atom(Module),
248 is_atom(StructureName),
249 element(1, Data) =:= StructureName ->
250 StructType = Module:struct_info(StructureName),
251 write(Proto, {Module:struct_info(StructureName), Data});
252
David Reissae756f42008-06-10 22:57:11 +0000253write(Proto, {{list, Type}, Data})
254 when is_list(Data) ->
255 ok = write(Proto,
256 #protocol_list_begin{
257 etype = term_to_typeid(Type),
258 size = length(Data)
259 }),
260 lists:foreach(fun(Elem) ->
261 ok = write(Proto, {Type, Elem})
262 end,
263 Data),
264 ok = write(Proto, list_end),
265 ok;
266
267write(Proto, {{map, KeyType, ValType}, Data}) ->
David Reissae756f42008-06-10 22:57:11 +0000268 ok = write(Proto,
269 #protocol_map_begin{
270 ktype = term_to_typeid(KeyType),
271 vtype = term_to_typeid(ValType),
David Reiss6b3e40f2008-06-11 00:59:03 +0000272 size = dict:size(Data)
273 }),
274 dict:fold(fun(KeyData, ValData, _Acc) ->
275 ok = write(Proto, {KeyType, KeyData}),
276 ok = write(Proto, {ValType, ValData})
277 end,
278 _AccO = ok,
279 Data),
David Reissae756f42008-06-10 22:57:11 +0000280 ok = write(Proto, map_end),
281 ok;
282
283write(Proto, {{set, Type}, Data}) ->
284 true = sets:is_set(Data),
David Reissae756f42008-06-10 22:57:11 +0000285 ok = write(Proto,
286 #protocol_set_begin{
287 etype = term_to_typeid(Type),
David Reiss6b3e40f2008-06-11 00:59:03 +0000288 size = sets:size(Data)
289 }),
290 sets:fold(fun(Elem, _Acc) ->
291 ok = write(Proto, {Type, Elem})
292 end,
293 _Acc0 = ok,
294 Data),
David Reissae756f42008-06-10 22:57:11 +0000295 ok = write(Proto, set_end),
296 ok;
297
298write(#protocol{module = Module,
299 data = ModuleData}, Data) ->
300 Module:write(ModuleData, Data).
301
David Reissae756f42008-06-10 22:57:11 +0000302struct_write_loop(Proto, [{Fid, Type} | RestStructDef], [Data | RestData]) ->
303 case Data of
304 undefined ->
305 % null fields are skipped in response
306 skip;
307 _ ->
308 ok = write(Proto,
309 #protocol_field_begin{
310 type = term_to_typeid(Type),
311 id = Fid
312 }),
313 ok = write(Proto, {Type, Data}),
314 ok = write(Proto, field_end)
315 end,
316 struct_write_loop(Proto, RestStructDef, RestData);
317struct_write_loop(Proto, [], []) ->
318 ok = write(Proto, field_stop),
319 ok.