blob: 3ccb4eeba9459e8a87eeaeeefdc74ad1c2cb4806 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001%%
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
9%%
10%% http://www.apache.org/licenses/LICENSE-2.0
11%%
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
17%% under the License.
18%%
19
David Reissac549552008-06-10 22:56:59 +000020-module(thrift_protocol).
21
22-export([new/2,
23 write/2,
24 read/2,
David Reiss58a961a2008-06-11 01:13:19 +000025 read/3,
David Reissac549552008-06-10 22:56:59 +000026 skip/2,
David Reiss90b40832008-06-10 22:58:52 +000027 flush_transport/1,
David Reissc11734e2008-06-11 00:59:48 +000028 close_transport/1,
David Reiss6b3e40f2008-06-11 00:59:03 +000029 typeid_to_atom/1
30 ]).
David Reissac549552008-06-10 22:56:59 +000031
David Reiss6b3e40f2008-06-11 00:59:03 +000032-export([behaviour_info/1]).
David Reissac549552008-06-10 22:56:59 +000033
34-include("thrift_constants.hrl").
35-include("thrift_protocol.hrl").
36
37-record(protocol, {module, data}).
38
39behaviour_info(callbacks) ->
40 [
41 {read, 2},
David Reiss90b40832008-06-10 22:58:52 +000042 {write, 2},
David Reissc11734e2008-06-11 00:59:48 +000043 {flush_transport, 1},
44 {close_transport, 1}
David Reissac549552008-06-10 22:56:59 +000045 ];
46behaviour_info(_Else) -> undefined.
47
David Reissac549552008-06-10 22:56:59 +000048new(Module, Data) when is_atom(Module) ->
49 {ok, #protocol{module = Module,
50 data = Data}}.
51
David Reiss5e6637b2010-08-30 22:05:18 +000052-spec flush_transport(#protocol{}) -> ok.
David Reissf32d0fb2010-08-30 22:05:00 +000053flush_transport(#protocol{module = Module,
54 data = Data}) ->
55 Module:flush_transport(Data).
David Reiss90b40832008-06-10 22:58:52 +000056
David Reiss5e6637b2010-08-30 22:05:18 +000057-spec close_transport(#protocol{}) -> ok.
David Reissc11734e2008-06-11 00:59:48 +000058close_transport(#protocol{module = Module,
59 data = Data}) ->
60 Module:close_transport(Data).
61
David Reissac549552008-06-10 22:56:59 +000062typeid_to_atom(?tType_STOP) -> field_stop;
63typeid_to_atom(?tType_VOID) -> void;
64typeid_to_atom(?tType_BOOL) -> bool;
65typeid_to_atom(?tType_BYTE) -> byte;
66typeid_to_atom(?tType_DOUBLE) -> double;
67typeid_to_atom(?tType_I16) -> i16;
68typeid_to_atom(?tType_I32) -> i32;
69typeid_to_atom(?tType_I64) -> i64;
70typeid_to_atom(?tType_STRING) -> string;
71typeid_to_atom(?tType_STRUCT) -> struct;
72typeid_to_atom(?tType_MAP) -> map;
73typeid_to_atom(?tType_SET) -> set;
74typeid_to_atom(?tType_LIST) -> list.
David Reissae756f42008-06-10 22:57:11 +000075
76term_to_typeid(void) -> ?tType_VOID;
77term_to_typeid(bool) -> ?tType_BOOL;
78term_to_typeid(byte) -> ?tType_BYTE;
79term_to_typeid(double) -> ?tType_DOUBLE;
80term_to_typeid(i16) -> ?tType_I16;
81term_to_typeid(i32) -> ?tType_I32;
82term_to_typeid(i64) -> ?tType_I64;
83term_to_typeid(string) -> ?tType_STRING;
84term_to_typeid({struct, _}) -> ?tType_STRUCT;
85term_to_typeid({map, _, _}) -> ?tType_MAP;
86term_to_typeid({set, _}) -> ?tType_SET;
87term_to_typeid({list, _}) -> ?tType_LIST.
88
David Reissae756f42008-06-10 22:57:11 +000089%% Structure is like:
90%% [{Fid, Type}, ...]
David Reiss5e6637b2010-08-30 22:05:18 +000091-spec read(#protocol{}, {struct, _StructDef}, atom()) -> {ok, tuple()}.
David Reissf32d0fb2010-08-30 22:05:00 +000092read(IProto, {struct, Structure}, Tag)
David Reiss58a961a2008-06-11 01:13:19 +000093 when is_list(Structure), is_atom(Tag) ->
94
95 % If we want a tagged tuple, we need to offset all the tuple indices
96 % by 1 to avoid overwriting the tag.
97 Offset = if Tag =/= undefined -> 1; true -> 0 end,
David Reisseea82982008-06-10 22:58:21 +000098 IndexList = case length(Structure) of
David Reiss58a961a2008-06-11 01:13:19 +000099 N when N > 0 -> lists:seq(1 + Offset, N + Offset);
David Reisseea82982008-06-10 22:58:21 +0000100 _ -> []
101 end,
102
David Reissae756f42008-06-10 22:57:11 +0000103 SWithIndices = [{Fid, {Type, Index}} ||
104 {{Fid, Type}, Index} <-
David Reisseea82982008-06-10 22:58:21 +0000105 lists:zip(Structure, IndexList)],
David Reissae756f42008-06-10 22:57:11 +0000106 % Fid -> {Type, Index}
107 SDict = dict:from_list(SWithIndices),
108
David Reissf32d0fb2010-08-30 22:05:00 +0000109 ok = read(IProto, struct_begin),
David Reiss58a961a2008-06-11 01:13:19 +0000110 RTuple0 = erlang:make_tuple(length(Structure) + Offset, undefined),
111 RTuple1 = if Tag =/= undefined -> setelement(1, RTuple0, Tag);
112 true -> RTuple0
113 end,
David Reissae756f42008-06-10 22:57:11 +0000114
David Reissf32d0fb2010-08-30 22:05:00 +0000115 RTuple2 = read_struct_loop(IProto, SDict, RTuple1),
116 {ok, RTuple2}.
David Reissae756f42008-06-10 22:57:11 +0000117
David Reiss5e6637b2010-08-30 22:05:18 +0000118-spec read(#protocol{}, term()) -> term().
119
David Reiss76f0d112008-06-10 22:57:35 +0000120read(IProto, {struct, {Module, StructureName}}) when is_atom(Module),
121 is_atom(StructureName) ->
David Reiss58a961a2008-06-11 01:13:19 +0000122 read(IProto, Module:struct_info(StructureName), StructureName);
123
124read(IProto, S={struct, Structure}) when is_list(Structure) ->
125 read(IProto, S, undefined);
David Reiss76f0d112008-06-10 22:57:35 +0000126
David Reissf32d0fb2010-08-30 22:05:00 +0000127read(IProto, {list, Type}) ->
128 #protocol_list_begin{etype = EType, size = Size} =
129 read(IProto, list_begin),
130 List = [Result || {ok, Result} <-
131 [read(IProto, Type) || _X <- lists:duplicate(Size, 0)]],
132 ok = read(IProto, list_end),
133 {ok, List};
David Reissae756f42008-06-10 22:57:11 +0000134
David Reissf32d0fb2010-08-30 22:05:00 +0000135read(IProto, {map, KeyType, ValType}) ->
136 #protocol_map_begin{size = Size} =
137 read(IProto, map_begin),
David Reissae756f42008-06-10 22:57:11 +0000138
David Reissf32d0fb2010-08-30 22:05:00 +0000139 List = [{Key, Val} || {{ok, Key}, {ok, Val}} <-
140 [{read(IProto, KeyType),
141 read(IProto, ValType)} || _X <- lists:duplicate(Size, 0)]],
142 ok = read(IProto, map_end),
143 {ok, dict:from_list(List)};
David Reissae756f42008-06-10 22:57:11 +0000144
David Reissf32d0fb2010-08-30 22:05:00 +0000145read(IProto, {set, Type}) ->
146 #protocol_set_begin{etype = _EType,
147 size = Size} =
148 read(IProto, set_begin),
149 List = [Result || {ok, Result} <-
150 [read(IProto, Type) || _X <- lists:duplicate(Size, 0)]],
151 ok = read(IProto, set_end),
152 {ok, sets:from_list(List)};
David Reissae756f42008-06-10 22:57:11 +0000153
David Reissf32d0fb2010-08-30 22:05:00 +0000154read(#protocol{module = Module,
155 data = ModuleData}, ProtocolType) ->
156 Module:read(ModuleData, ProtocolType).
David Reissac549552008-06-10 22:56:59 +0000157
David Reissf32d0fb2010-08-30 22:05:00 +0000158read_struct_loop(IProto, SDict, RTuple) ->
159 #protocol_field_begin{type = FType, id = Fid, name = Name} =
160 thrift_protocol:read(IProto, field_begin),
David Reissae756f42008-06-10 22:57:11 +0000161 case {FType, Fid} of
162 {?tType_STOP, _} ->
David Reissf32d0fb2010-08-30 22:05:00 +0000163 RTuple;
David Reissae756f42008-06-10 22:57:11 +0000164 _Else ->
165 case dict:find(Fid, SDict) of
166 {ok, {Type, Index}} ->
David Reiss233ace52009-03-30 20:46:47 +0000167 case term_to_typeid(Type) of
168 FType ->
David Reissf32d0fb2010-08-30 22:05:00 +0000169 {ok, Val} = read(IProto, Type),
170 thrift_protocol:read(IProto, field_end),
David Reiss233ace52009-03-30 20:46:47 +0000171 NewRTuple = setelement(Index, RTuple, Val),
David Reissf32d0fb2010-08-30 22:05:00 +0000172 read_struct_loop(IProto, SDict, NewRTuple);
David Reiss233ace52009-03-30 20:46:47 +0000173 Expected ->
174 error_logger:info_msg(
175 "Skipping field ~p with wrong type (~p != ~p)~n",
176 [Fid, FType, Expected]),
David Reissf32d0fb2010-08-30 22:05:00 +0000177 skip_field(FType, IProto, SDict, RTuple)
David Reiss233ace52009-03-30 20:46:47 +0000178 end;
David Reissae756f42008-06-10 22:57:11 +0000179 _Else2 ->
David Reiss233ace52009-03-30 20:46:47 +0000180 error_logger:info_msg("Skipping field ~p with unknown fid~n", [Fid]),
David Reissf32d0fb2010-08-30 22:05:00 +0000181 skip_field(FType, IProto, SDict, RTuple)
David Reissae756f42008-06-10 22:57:11 +0000182 end
183 end.
David Reissac549552008-06-10 22:56:59 +0000184
David Reissf32d0fb2010-08-30 22:05:00 +0000185skip_field(FType, IProto, SDict, RTuple) ->
David Reiss233ace52009-03-30 20:46:47 +0000186 FTypeAtom = thrift_protocol:typeid_to_atom(FType),
David Reissf32d0fb2010-08-30 22:05:00 +0000187 thrift_protocol:skip(IProto, FTypeAtom),
188 read(IProto, field_end),
189 read_struct_loop(IProto, SDict, RTuple).
David Reiss233ace52009-03-30 20:46:47 +0000190
David Reiss5e6637b2010-08-30 22:05:18 +0000191-spec skip(#protocol{}, term()) -> ok.
David Reissac549552008-06-10 22:56:59 +0000192
David Reissf32d0fb2010-08-30 22:05:00 +0000193skip(Proto, struct) ->
194 ok = read(Proto, struct_begin),
195 ok = skip_struct_loop(Proto),
196 ok = read(Proto, struct_end);
197
198skip(Proto, map) ->
199 Map = read(Proto, map_begin),
200 ok = skip_map_loop(Proto, Map),
201 ok = read(Proto, map_end);
202
203skip(Proto, set) ->
204 Set = read(Proto, set_begin),
205 ok = skip_set_loop(Proto, Set),
206 ok = read(Proto, set_end);
207
208skip(Proto, list) ->
209 List = read(Proto, list_begin),
210 ok = skip_list_loop(Proto, List),
211 ok = read(Proto, list_end);
212
213skip(Proto, Type) when is_atom(Type) ->
214 _Ignore = read(Proto, Type),
215 ok.
216
217
218skip_struct_loop(Proto) ->
219 #protocol_field_begin{type = Type} = read(Proto, field_begin),
David Reissac549552008-06-10 22:56:59 +0000220 case Type of
221 ?tType_STOP ->
David Reissf32d0fb2010-08-30 22:05:00 +0000222 ok;
David Reissac549552008-06-10 22:56:59 +0000223 _Else ->
David Reissf32d0fb2010-08-30 22:05:00 +0000224 skip(Proto, Type),
225 ok = read(Proto, field_end),
226 skip_struct_loop(Proto)
David Reissac549552008-06-10 22:56:59 +0000227 end.
228
David Reissf32d0fb2010-08-30 22:05:00 +0000229skip_map_loop(Proto, Map = #protocol_map_begin{ktype = Ktype,
230 vtype = Vtype,
231 size = Size}) ->
David Reissac549552008-06-10 22:56:59 +0000232 case Size of
233 N when N > 0 ->
David Reissf32d0fb2010-08-30 22:05:00 +0000234 skip(Proto, Ktype),
235 skip(Proto, Vtype),
236 skip_map_loop(Proto,
David Reissac549552008-06-10 22:56:59 +0000237 Map#protocol_map_begin{size = Size - 1});
David Reissf32d0fb2010-08-30 22:05:00 +0000238 0 -> ok
David Reissac549552008-06-10 22:56:59 +0000239 end.
240
David Reissf32d0fb2010-08-30 22:05:00 +0000241skip_set_loop(Proto, Map = #protocol_set_begin{etype = Etype,
242 size = Size}) ->
David Reissac549552008-06-10 22:56:59 +0000243 case Size of
244 N when N > 0 ->
David Reissf32d0fb2010-08-30 22:05:00 +0000245 skip(Proto, Etype),
246 skip_set_loop(Proto,
David Reissac549552008-06-10 22:56:59 +0000247 Map#protocol_set_begin{size = Size - 1});
David Reissf32d0fb2010-08-30 22:05:00 +0000248 0 -> ok
David Reissac549552008-06-10 22:56:59 +0000249 end.
250
David Reissf32d0fb2010-08-30 22:05:00 +0000251skip_list_loop(Proto, Map = #protocol_list_begin{etype = Etype,
252 size = Size}) ->
David Reissac549552008-06-10 22:56:59 +0000253 case Size of
254 N when N > 0 ->
David Reissf32d0fb2010-08-30 22:05:00 +0000255 skip(Proto, Etype),
256 skip_list_loop(Proto,
David Reissac549552008-06-10 22:56:59 +0000257 Map#protocol_list_begin{size = Size - 1});
David Reissf32d0fb2010-08-30 22:05:00 +0000258 0 -> ok
David Reissac549552008-06-10 22:56:59 +0000259 end.
David Reissae756f42008-06-10 22:57:11 +0000260
261
262%%--------------------------------------------------------------------
263%% Function: write(OProto, {Type, Data}) -> ok
David Reiss6b3e40f2008-06-11 00:59:03 +0000264%%
David Reissae756f42008-06-10 22:57:11 +0000265%% Type = {struct, StructDef} |
266%% {list, Type} |
267%% {map, KeyType, ValType} |
268%% {set, Type} |
269%% BaseType
270%%
271%% Data =
272%% tuple() -- for struct
273%% | list() -- for list
274%% | dictionary() -- for map
275%% | set() -- for set
David Reissf32d0fb2010-08-30 22:05:00 +0000276%% | term() -- for base types
David Reissae756f42008-06-10 22:57:11 +0000277%%
David Reiss6b3e40f2008-06-11 00:59:03 +0000278%% Description:
David Reissae756f42008-06-10 22:57:11 +0000279%%--------------------------------------------------------------------
David Reiss5e6637b2010-08-30 22:05:18 +0000280-spec write(#protocol{}, term()) -> ok | {error, _Reason}.
281
David Reissf32d0fb2010-08-30 22:05:00 +0000282write(Proto, {{struct, StructDef}, Data})
David Reiss76f0d112008-06-10 22:57:35 +0000283 when is_list(StructDef), is_tuple(Data), length(StructDef) == size(Data) - 1 ->
284
285 [StructName | Elems] = tuple_to_list(Data),
David Reissf32d0fb2010-08-30 22:05:00 +0000286 ok = write(Proto, #protocol_struct_begin{name = StructName}),
287 ok = struct_write_loop(Proto, StructDef, Elems),
288 ok = write(Proto, struct_end),
289 ok;
David Reissae756f42008-06-10 22:57:11 +0000290
David Reiss76f0d112008-06-10 22:57:35 +0000291write(Proto, {{struct, {Module, StructureName}}, Data})
292 when is_atom(Module),
293 is_atom(StructureName),
294 element(1, Data) =:= StructureName ->
David Reissf32d0fb2010-08-30 22:05:00 +0000295 StructType = Module:struct_info(StructureName),
David Reiss76f0d112008-06-10 22:57:35 +0000296 write(Proto, {Module:struct_info(StructureName), Data});
297
David Reissf32d0fb2010-08-30 22:05:00 +0000298write(Proto, {{list, Type}, Data})
David Reissae756f42008-06-10 22:57:11 +0000299 when is_list(Data) ->
David Reissf32d0fb2010-08-30 22:05:00 +0000300 ok = write(Proto,
David Reissae756f42008-06-10 22:57:11 +0000301 #protocol_list_begin{
302 etype = term_to_typeid(Type),
303 size = length(Data)
304 }),
David Reissf32d0fb2010-08-30 22:05:00 +0000305 lists:foreach(fun(Elem) ->
306 ok = write(Proto, {Type, Elem})
307 end,
308 Data),
309 ok = write(Proto, list_end),
310 ok;
David Reissae756f42008-06-10 22:57:11 +0000311
David Reissf32d0fb2010-08-30 22:05:00 +0000312write(Proto, {{map, KeyType, ValType}, Data}) ->
313 ok = write(Proto,
314 #protocol_map_begin{
315 ktype = term_to_typeid(KeyType),
316 vtype = term_to_typeid(ValType),
317 size = dict:size(Data)
318 }),
319 dict:fold(fun(KeyData, ValData, _Acc) ->
320 ok = write(Proto, {KeyType, KeyData}),
321 ok = write(Proto, {ValType, ValData})
322 end,
323 _AccO = ok,
324 Data),
325 ok = write(Proto, map_end),
326 ok;
David Reissae756f42008-06-10 22:57:11 +0000327
David Reissf32d0fb2010-08-30 22:05:00 +0000328write(Proto, {{set, Type}, Data}) ->
David Reissae756f42008-06-10 22:57:11 +0000329 true = sets:is_set(Data),
David Reissf32d0fb2010-08-30 22:05:00 +0000330 ok = write(Proto,
331 #protocol_set_begin{
332 etype = term_to_typeid(Type),
333 size = sets:size(Data)
334 }),
335 sets:fold(fun(Elem, _Acc) ->
336 ok = write(Proto, {Type, Elem})
337 end,
338 _Acc0 = ok,
339 Data),
340 ok = write(Proto, set_end),
341 ok;
David Reissae756f42008-06-10 22:57:11 +0000342
David Reissf32d0fb2010-08-30 22:05:00 +0000343write(#protocol{module = Module,
344 data = ModuleData}, Data) ->
345 Module:write(ModuleData, Data).
David Reissae756f42008-06-10 22:57:11 +0000346
David Reissf32d0fb2010-08-30 22:05:00 +0000347struct_write_loop(Proto, [{Fid, Type} | RestStructDef], [Data | RestData]) ->
348 case Data of
349 undefined ->
350 % null fields are skipped in response
351 skip;
352 _ ->
353 ok = write(Proto,
354 #protocol_field_begin{
355 type = term_to_typeid(Type),
356 id = Fid
357 }),
358 ok = write(Proto, {Type, Data}),
359 ok = write(Proto, field_end)
360 end,
361 struct_write_loop(Proto, RestStructDef, RestData);
David Reissae756f42008-06-10 22:57:11 +0000362struct_write_loop(Proto, [], []) ->
David Reissf32d0fb2010-08-30 22:05:00 +0000363 ok = write(Proto, field_stop),
364 ok.