blob: 3da526cd86498d742a030894ff44af31c636ffcb [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 Reiss1cb979b2010-08-30 22:05:25 +0000118
119%% NOTE: Keep this in sync with thrift_protocol_impl:read
120-spec read
121 (#protocol{}, {struct, _Info}) -> {ok, tuple()} | {error, _Reason};
122 (#protocol{}, tprot_cont_tag()) -> {ok, term()} | {error, _Reason};
123 (#protocol{}, tprot_empty_tag()) -> ok | {error, _Reason};
124 (#protocol{}, tprot_header_tag()) -> tprot_header_val() | {error, _Reason};
125 (#protocol{}, tprot_data_tag()) -> {ok, term()} | {error, _Reason}.
David Reiss5e6637b2010-08-30 22:05:18 +0000126
David Reiss76f0d112008-06-10 22:57:35 +0000127read(IProto, {struct, {Module, StructureName}}) when is_atom(Module),
128 is_atom(StructureName) ->
David Reiss58a961a2008-06-11 01:13:19 +0000129 read(IProto, Module:struct_info(StructureName), StructureName);
130
131read(IProto, S={struct, Structure}) when is_list(Structure) ->
132 read(IProto, S, undefined);
David Reiss76f0d112008-06-10 22:57:35 +0000133
David Reissf32d0fb2010-08-30 22:05:00 +0000134read(IProto, {list, Type}) ->
135 #protocol_list_begin{etype = EType, size = Size} =
136 read(IProto, list_begin),
137 List = [Result || {ok, Result} <-
138 [read(IProto, Type) || _X <- lists:duplicate(Size, 0)]],
139 ok = read(IProto, list_end),
140 {ok, List};
David Reissae756f42008-06-10 22:57:11 +0000141
David Reissf32d0fb2010-08-30 22:05:00 +0000142read(IProto, {map, KeyType, ValType}) ->
143 #protocol_map_begin{size = Size} =
144 read(IProto, map_begin),
David Reissae756f42008-06-10 22:57:11 +0000145
David Reissf32d0fb2010-08-30 22:05:00 +0000146 List = [{Key, Val} || {{ok, Key}, {ok, Val}} <-
147 [{read(IProto, KeyType),
148 read(IProto, ValType)} || _X <- lists:duplicate(Size, 0)]],
149 ok = read(IProto, map_end),
150 {ok, dict:from_list(List)};
David Reissae756f42008-06-10 22:57:11 +0000151
David Reissf32d0fb2010-08-30 22:05:00 +0000152read(IProto, {set, Type}) ->
153 #protocol_set_begin{etype = _EType,
154 size = Size} =
155 read(IProto, set_begin),
156 List = [Result || {ok, Result} <-
157 [read(IProto, Type) || _X <- lists:duplicate(Size, 0)]],
158 ok = read(IProto, set_end),
159 {ok, sets:from_list(List)};
David Reissae756f42008-06-10 22:57:11 +0000160
David Reiss480d5ab2010-08-30 22:05:26 +0000161read(Protocol, ProtocolType) ->
162 read_specific(Protocol, ProtocolType).
163
164%% NOTE: Keep this in sync with thrift_protocol_impl:read
165-spec read_specific
166 (#protocol{}, tprot_empty_tag()) -> ok | {error, _Reason};
167 (#protocol{}, tprot_header_tag()) -> tprot_header_val() | {error, _Reason};
168 (#protocol{}, tprot_data_tag()) -> {ok, term()} | {error, _Reason}.
169read_specific(#protocol{module = Module,
170 data = ModuleData}, ProtocolType) ->
David Reissf32d0fb2010-08-30 22:05:00 +0000171 Module:read(ModuleData, ProtocolType).
David Reissac549552008-06-10 22:56:59 +0000172
David Reissf32d0fb2010-08-30 22:05:00 +0000173read_struct_loop(IProto, SDict, RTuple) ->
174 #protocol_field_begin{type = FType, id = Fid, name = Name} =
175 thrift_protocol:read(IProto, field_begin),
David Reissae756f42008-06-10 22:57:11 +0000176 case {FType, Fid} of
177 {?tType_STOP, _} ->
David Reissf32d0fb2010-08-30 22:05:00 +0000178 RTuple;
David Reissae756f42008-06-10 22:57:11 +0000179 _Else ->
180 case dict:find(Fid, SDict) of
181 {ok, {Type, Index}} ->
David Reiss233ace52009-03-30 20:46:47 +0000182 case term_to_typeid(Type) of
183 FType ->
David Reissf32d0fb2010-08-30 22:05:00 +0000184 {ok, Val} = read(IProto, Type),
185 thrift_protocol:read(IProto, field_end),
David Reiss233ace52009-03-30 20:46:47 +0000186 NewRTuple = setelement(Index, RTuple, Val),
David Reissf32d0fb2010-08-30 22:05:00 +0000187 read_struct_loop(IProto, SDict, NewRTuple);
David Reiss233ace52009-03-30 20:46:47 +0000188 Expected ->
189 error_logger:info_msg(
190 "Skipping field ~p with wrong type (~p != ~p)~n",
191 [Fid, FType, Expected]),
David Reissf32d0fb2010-08-30 22:05:00 +0000192 skip_field(FType, IProto, SDict, RTuple)
David Reiss233ace52009-03-30 20:46:47 +0000193 end;
David Reissae756f42008-06-10 22:57:11 +0000194 _Else2 ->
David Reiss233ace52009-03-30 20:46:47 +0000195 error_logger:info_msg("Skipping field ~p with unknown fid~n", [Fid]),
David Reissf32d0fb2010-08-30 22:05:00 +0000196 skip_field(FType, IProto, SDict, RTuple)
David Reissae756f42008-06-10 22:57:11 +0000197 end
198 end.
David Reissac549552008-06-10 22:56:59 +0000199
David Reissf32d0fb2010-08-30 22:05:00 +0000200skip_field(FType, IProto, SDict, RTuple) ->
David Reiss233ace52009-03-30 20:46:47 +0000201 FTypeAtom = thrift_protocol:typeid_to_atom(FType),
David Reissf32d0fb2010-08-30 22:05:00 +0000202 thrift_protocol:skip(IProto, FTypeAtom),
203 read(IProto, field_end),
204 read_struct_loop(IProto, SDict, RTuple).
David Reiss233ace52009-03-30 20:46:47 +0000205
David Reiss5e6637b2010-08-30 22:05:18 +0000206-spec skip(#protocol{}, term()) -> ok.
David Reissac549552008-06-10 22:56:59 +0000207
David Reissf32d0fb2010-08-30 22:05:00 +0000208skip(Proto, struct) ->
209 ok = read(Proto, struct_begin),
210 ok = skip_struct_loop(Proto),
211 ok = read(Proto, struct_end);
212
213skip(Proto, map) ->
214 Map = read(Proto, map_begin),
215 ok = skip_map_loop(Proto, Map),
216 ok = read(Proto, map_end);
217
218skip(Proto, set) ->
219 Set = read(Proto, set_begin),
220 ok = skip_set_loop(Proto, Set),
221 ok = read(Proto, set_end);
222
223skip(Proto, list) ->
224 List = read(Proto, list_begin),
225 ok = skip_list_loop(Proto, List),
226 ok = read(Proto, list_end);
227
228skip(Proto, Type) when is_atom(Type) ->
229 _Ignore = read(Proto, Type),
230 ok.
231
232
233skip_struct_loop(Proto) ->
234 #protocol_field_begin{type = Type} = read(Proto, field_begin),
David Reissac549552008-06-10 22:56:59 +0000235 case Type of
236 ?tType_STOP ->
David Reissf32d0fb2010-08-30 22:05:00 +0000237 ok;
David Reissac549552008-06-10 22:56:59 +0000238 _Else ->
David Reissf32d0fb2010-08-30 22:05:00 +0000239 skip(Proto, Type),
240 ok = read(Proto, field_end),
241 skip_struct_loop(Proto)
David Reissac549552008-06-10 22:56:59 +0000242 end.
243
David Reissf32d0fb2010-08-30 22:05:00 +0000244skip_map_loop(Proto, Map = #protocol_map_begin{ktype = Ktype,
245 vtype = Vtype,
246 size = Size}) ->
David Reissac549552008-06-10 22:56:59 +0000247 case Size of
248 N when N > 0 ->
David Reissf32d0fb2010-08-30 22:05:00 +0000249 skip(Proto, Ktype),
250 skip(Proto, Vtype),
251 skip_map_loop(Proto,
David Reissac549552008-06-10 22:56:59 +0000252 Map#protocol_map_begin{size = Size - 1});
David Reissf32d0fb2010-08-30 22:05:00 +0000253 0 -> ok
David Reissac549552008-06-10 22:56:59 +0000254 end.
255
David Reissf32d0fb2010-08-30 22:05:00 +0000256skip_set_loop(Proto, Map = #protocol_set_begin{etype = Etype,
257 size = Size}) ->
David Reissac549552008-06-10 22:56:59 +0000258 case Size of
259 N when N > 0 ->
David Reissf32d0fb2010-08-30 22:05:00 +0000260 skip(Proto, Etype),
261 skip_set_loop(Proto,
David Reissac549552008-06-10 22:56:59 +0000262 Map#protocol_set_begin{size = Size - 1});
David Reissf32d0fb2010-08-30 22:05:00 +0000263 0 -> ok
David Reissac549552008-06-10 22:56:59 +0000264 end.
265
David Reissf32d0fb2010-08-30 22:05:00 +0000266skip_list_loop(Proto, Map = #protocol_list_begin{etype = Etype,
267 size = Size}) ->
David Reissac549552008-06-10 22:56:59 +0000268 case Size of
269 N when N > 0 ->
David Reissf32d0fb2010-08-30 22:05:00 +0000270 skip(Proto, Etype),
271 skip_list_loop(Proto,
David Reissac549552008-06-10 22:56:59 +0000272 Map#protocol_list_begin{size = Size - 1});
David Reissf32d0fb2010-08-30 22:05:00 +0000273 0 -> ok
David Reissac549552008-06-10 22:56:59 +0000274 end.
David Reissae756f42008-06-10 22:57:11 +0000275
276
277%%--------------------------------------------------------------------
278%% Function: write(OProto, {Type, Data}) -> ok
David Reiss6b3e40f2008-06-11 00:59:03 +0000279%%
David Reissae756f42008-06-10 22:57:11 +0000280%% Type = {struct, StructDef} |
281%% {list, Type} |
282%% {map, KeyType, ValType} |
283%% {set, Type} |
284%% BaseType
285%%
286%% Data =
287%% tuple() -- for struct
288%% | list() -- for list
289%% | dictionary() -- for map
290%% | set() -- for set
David Reissf32d0fb2010-08-30 22:05:00 +0000291%% | term() -- for base types
David Reissae756f42008-06-10 22:57:11 +0000292%%
David Reiss6b3e40f2008-06-11 00:59:03 +0000293%% Description:
David Reissae756f42008-06-10 22:57:11 +0000294%%--------------------------------------------------------------------
David Reiss5e6637b2010-08-30 22:05:18 +0000295-spec write(#protocol{}, term()) -> ok | {error, _Reason}.
296
David Reissf32d0fb2010-08-30 22:05:00 +0000297write(Proto, {{struct, StructDef}, Data})
David Reiss76f0d112008-06-10 22:57:35 +0000298 when is_list(StructDef), is_tuple(Data), length(StructDef) == size(Data) - 1 ->
299
300 [StructName | Elems] = tuple_to_list(Data),
David Reissf32d0fb2010-08-30 22:05:00 +0000301 ok = write(Proto, #protocol_struct_begin{name = StructName}),
302 ok = struct_write_loop(Proto, StructDef, Elems),
303 ok = write(Proto, struct_end),
304 ok;
David Reissae756f42008-06-10 22:57:11 +0000305
David Reiss76f0d112008-06-10 22:57:35 +0000306write(Proto, {{struct, {Module, StructureName}}, Data})
307 when is_atom(Module),
308 is_atom(StructureName),
309 element(1, Data) =:= StructureName ->
David Reissf32d0fb2010-08-30 22:05:00 +0000310 StructType = Module:struct_info(StructureName),
David Reiss76f0d112008-06-10 22:57:35 +0000311 write(Proto, {Module:struct_info(StructureName), Data});
312
David Reissf32d0fb2010-08-30 22:05:00 +0000313write(Proto, {{list, Type}, Data})
David Reissae756f42008-06-10 22:57:11 +0000314 when is_list(Data) ->
David Reissf32d0fb2010-08-30 22:05:00 +0000315 ok = write(Proto,
David Reissae756f42008-06-10 22:57:11 +0000316 #protocol_list_begin{
317 etype = term_to_typeid(Type),
318 size = length(Data)
319 }),
David Reissf32d0fb2010-08-30 22:05:00 +0000320 lists:foreach(fun(Elem) ->
321 ok = write(Proto, {Type, Elem})
322 end,
323 Data),
324 ok = write(Proto, list_end),
325 ok;
David Reissae756f42008-06-10 22:57:11 +0000326
David Reissf32d0fb2010-08-30 22:05:00 +0000327write(Proto, {{map, KeyType, ValType}, Data}) ->
328 ok = write(Proto,
329 #protocol_map_begin{
330 ktype = term_to_typeid(KeyType),
331 vtype = term_to_typeid(ValType),
332 size = dict:size(Data)
333 }),
334 dict:fold(fun(KeyData, ValData, _Acc) ->
335 ok = write(Proto, {KeyType, KeyData}),
336 ok = write(Proto, {ValType, ValData})
337 end,
338 _AccO = ok,
339 Data),
340 ok = write(Proto, map_end),
341 ok;
David Reissae756f42008-06-10 22:57:11 +0000342
David Reissf32d0fb2010-08-30 22:05:00 +0000343write(Proto, {{set, Type}, Data}) ->
David Reissae756f42008-06-10 22:57:11 +0000344 true = sets:is_set(Data),
David Reissf32d0fb2010-08-30 22:05:00 +0000345 ok = write(Proto,
346 #protocol_set_begin{
347 etype = term_to_typeid(Type),
348 size = sets:size(Data)
349 }),
350 sets:fold(fun(Elem, _Acc) ->
351 ok = write(Proto, {Type, Elem})
352 end,
353 _Acc0 = ok,
354 Data),
355 ok = write(Proto, set_end),
356 ok;
David Reissae756f42008-06-10 22:57:11 +0000357
David Reissf32d0fb2010-08-30 22:05:00 +0000358write(#protocol{module = Module,
359 data = ModuleData}, Data) ->
360 Module:write(ModuleData, Data).
David Reissae756f42008-06-10 22:57:11 +0000361
David Reissf32d0fb2010-08-30 22:05:00 +0000362struct_write_loop(Proto, [{Fid, Type} | RestStructDef], [Data | RestData]) ->
363 case Data of
364 undefined ->
365 % null fields are skipped in response
366 skip;
367 _ ->
368 ok = write(Proto,
369 #protocol_field_begin{
370 type = term_to_typeid(Type),
371 id = Fid
372 }),
373 ok = write(Proto, {Type, Data}),
374 ok = write(Proto, field_end)
375 end,
376 struct_write_loop(Proto, RestStructDef, RestData);
David Reissae756f42008-06-10 22:57:11 +0000377struct_write_loop(Proto, [], []) ->
David Reissf32d0fb2010-08-30 22:05:00 +0000378 ok = write(Proto, field_stop),
379 ok.