blob: a207bca5fe5e50a14b8bf872ae53a857b3d910a2 [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 Reissf32d0fb2010-08-30 22:05:00 +0000161read(#protocol{module = Module,
162 data = ModuleData}, ProtocolType) ->
163 Module:read(ModuleData, ProtocolType).
David Reissac549552008-06-10 22:56:59 +0000164
David Reissf32d0fb2010-08-30 22:05:00 +0000165read_struct_loop(IProto, SDict, RTuple) ->
166 #protocol_field_begin{type = FType, id = Fid, name = Name} =
167 thrift_protocol:read(IProto, field_begin),
David Reissae756f42008-06-10 22:57:11 +0000168 case {FType, Fid} of
169 {?tType_STOP, _} ->
David Reissf32d0fb2010-08-30 22:05:00 +0000170 RTuple;
David Reissae756f42008-06-10 22:57:11 +0000171 _Else ->
172 case dict:find(Fid, SDict) of
173 {ok, {Type, Index}} ->
David Reiss233ace52009-03-30 20:46:47 +0000174 case term_to_typeid(Type) of
175 FType ->
David Reissf32d0fb2010-08-30 22:05:00 +0000176 {ok, Val} = read(IProto, Type),
177 thrift_protocol:read(IProto, field_end),
David Reiss233ace52009-03-30 20:46:47 +0000178 NewRTuple = setelement(Index, RTuple, Val),
David Reissf32d0fb2010-08-30 22:05:00 +0000179 read_struct_loop(IProto, SDict, NewRTuple);
David Reiss233ace52009-03-30 20:46:47 +0000180 Expected ->
181 error_logger:info_msg(
182 "Skipping field ~p with wrong type (~p != ~p)~n",
183 [Fid, FType, Expected]),
David Reissf32d0fb2010-08-30 22:05:00 +0000184 skip_field(FType, IProto, SDict, RTuple)
David Reiss233ace52009-03-30 20:46:47 +0000185 end;
David Reissae756f42008-06-10 22:57:11 +0000186 _Else2 ->
David Reiss233ace52009-03-30 20:46:47 +0000187 error_logger:info_msg("Skipping field ~p with unknown fid~n", [Fid]),
David Reissf32d0fb2010-08-30 22:05:00 +0000188 skip_field(FType, IProto, SDict, RTuple)
David Reissae756f42008-06-10 22:57:11 +0000189 end
190 end.
David Reissac549552008-06-10 22:56:59 +0000191
David Reissf32d0fb2010-08-30 22:05:00 +0000192skip_field(FType, IProto, SDict, RTuple) ->
David Reiss233ace52009-03-30 20:46:47 +0000193 FTypeAtom = thrift_protocol:typeid_to_atom(FType),
David Reissf32d0fb2010-08-30 22:05:00 +0000194 thrift_protocol:skip(IProto, FTypeAtom),
195 read(IProto, field_end),
196 read_struct_loop(IProto, SDict, RTuple).
David Reiss233ace52009-03-30 20:46:47 +0000197
David Reiss5e6637b2010-08-30 22:05:18 +0000198-spec skip(#protocol{}, term()) -> ok.
David Reissac549552008-06-10 22:56:59 +0000199
David Reissf32d0fb2010-08-30 22:05:00 +0000200skip(Proto, struct) ->
201 ok = read(Proto, struct_begin),
202 ok = skip_struct_loop(Proto),
203 ok = read(Proto, struct_end);
204
205skip(Proto, map) ->
206 Map = read(Proto, map_begin),
207 ok = skip_map_loop(Proto, Map),
208 ok = read(Proto, map_end);
209
210skip(Proto, set) ->
211 Set = read(Proto, set_begin),
212 ok = skip_set_loop(Proto, Set),
213 ok = read(Proto, set_end);
214
215skip(Proto, list) ->
216 List = read(Proto, list_begin),
217 ok = skip_list_loop(Proto, List),
218 ok = read(Proto, list_end);
219
220skip(Proto, Type) when is_atom(Type) ->
221 _Ignore = read(Proto, Type),
222 ok.
223
224
225skip_struct_loop(Proto) ->
226 #protocol_field_begin{type = Type} = read(Proto, field_begin),
David Reissac549552008-06-10 22:56:59 +0000227 case Type of
228 ?tType_STOP ->
David Reissf32d0fb2010-08-30 22:05:00 +0000229 ok;
David Reissac549552008-06-10 22:56:59 +0000230 _Else ->
David Reissf32d0fb2010-08-30 22:05:00 +0000231 skip(Proto, Type),
232 ok = read(Proto, field_end),
233 skip_struct_loop(Proto)
David Reissac549552008-06-10 22:56:59 +0000234 end.
235
David Reissf32d0fb2010-08-30 22:05:00 +0000236skip_map_loop(Proto, Map = #protocol_map_begin{ktype = Ktype,
237 vtype = Vtype,
238 size = Size}) ->
David Reissac549552008-06-10 22:56:59 +0000239 case Size of
240 N when N > 0 ->
David Reissf32d0fb2010-08-30 22:05:00 +0000241 skip(Proto, Ktype),
242 skip(Proto, Vtype),
243 skip_map_loop(Proto,
David Reissac549552008-06-10 22:56:59 +0000244 Map#protocol_map_begin{size = Size - 1});
David Reissf32d0fb2010-08-30 22:05:00 +0000245 0 -> ok
David Reissac549552008-06-10 22:56:59 +0000246 end.
247
David Reissf32d0fb2010-08-30 22:05:00 +0000248skip_set_loop(Proto, Map = #protocol_set_begin{etype = Etype,
249 size = Size}) ->
David Reissac549552008-06-10 22:56:59 +0000250 case Size of
251 N when N > 0 ->
David Reissf32d0fb2010-08-30 22:05:00 +0000252 skip(Proto, Etype),
253 skip_set_loop(Proto,
David Reissac549552008-06-10 22:56:59 +0000254 Map#protocol_set_begin{size = Size - 1});
David Reissf32d0fb2010-08-30 22:05:00 +0000255 0 -> ok
David Reissac549552008-06-10 22:56:59 +0000256 end.
257
David Reissf32d0fb2010-08-30 22:05:00 +0000258skip_list_loop(Proto, Map = #protocol_list_begin{etype = Etype,
259 size = Size}) ->
David Reissac549552008-06-10 22:56:59 +0000260 case Size of
261 N when N > 0 ->
David Reissf32d0fb2010-08-30 22:05:00 +0000262 skip(Proto, Etype),
263 skip_list_loop(Proto,
David Reissac549552008-06-10 22:56:59 +0000264 Map#protocol_list_begin{size = Size - 1});
David Reissf32d0fb2010-08-30 22:05:00 +0000265 0 -> ok
David Reissac549552008-06-10 22:56:59 +0000266 end.
David Reissae756f42008-06-10 22:57:11 +0000267
268
269%%--------------------------------------------------------------------
270%% Function: write(OProto, {Type, Data}) -> ok
David Reiss6b3e40f2008-06-11 00:59:03 +0000271%%
David Reissae756f42008-06-10 22:57:11 +0000272%% Type = {struct, StructDef} |
273%% {list, Type} |
274%% {map, KeyType, ValType} |
275%% {set, Type} |
276%% BaseType
277%%
278%% Data =
279%% tuple() -- for struct
280%% | list() -- for list
281%% | dictionary() -- for map
282%% | set() -- for set
David Reissf32d0fb2010-08-30 22:05:00 +0000283%% | term() -- for base types
David Reissae756f42008-06-10 22:57:11 +0000284%%
David Reiss6b3e40f2008-06-11 00:59:03 +0000285%% Description:
David Reissae756f42008-06-10 22:57:11 +0000286%%--------------------------------------------------------------------
David Reiss5e6637b2010-08-30 22:05:18 +0000287-spec write(#protocol{}, term()) -> ok | {error, _Reason}.
288
David Reissf32d0fb2010-08-30 22:05:00 +0000289write(Proto, {{struct, StructDef}, Data})
David Reiss76f0d112008-06-10 22:57:35 +0000290 when is_list(StructDef), is_tuple(Data), length(StructDef) == size(Data) - 1 ->
291
292 [StructName | Elems] = tuple_to_list(Data),
David Reissf32d0fb2010-08-30 22:05:00 +0000293 ok = write(Proto, #protocol_struct_begin{name = StructName}),
294 ok = struct_write_loop(Proto, StructDef, Elems),
295 ok = write(Proto, struct_end),
296 ok;
David Reissae756f42008-06-10 22:57:11 +0000297
David Reiss76f0d112008-06-10 22:57:35 +0000298write(Proto, {{struct, {Module, StructureName}}, Data})
299 when is_atom(Module),
300 is_atom(StructureName),
301 element(1, Data) =:= StructureName ->
David Reissf32d0fb2010-08-30 22:05:00 +0000302 StructType = Module:struct_info(StructureName),
David Reiss76f0d112008-06-10 22:57:35 +0000303 write(Proto, {Module:struct_info(StructureName), Data});
304
David Reissf32d0fb2010-08-30 22:05:00 +0000305write(Proto, {{list, Type}, Data})
David Reissae756f42008-06-10 22:57:11 +0000306 when is_list(Data) ->
David Reissf32d0fb2010-08-30 22:05:00 +0000307 ok = write(Proto,
David Reissae756f42008-06-10 22:57:11 +0000308 #protocol_list_begin{
309 etype = term_to_typeid(Type),
310 size = length(Data)
311 }),
David Reissf32d0fb2010-08-30 22:05:00 +0000312 lists:foreach(fun(Elem) ->
313 ok = write(Proto, {Type, Elem})
314 end,
315 Data),
316 ok = write(Proto, list_end),
317 ok;
David Reissae756f42008-06-10 22:57:11 +0000318
David Reissf32d0fb2010-08-30 22:05:00 +0000319write(Proto, {{map, KeyType, ValType}, Data}) ->
320 ok = write(Proto,
321 #protocol_map_begin{
322 ktype = term_to_typeid(KeyType),
323 vtype = term_to_typeid(ValType),
324 size = dict:size(Data)
325 }),
326 dict:fold(fun(KeyData, ValData, _Acc) ->
327 ok = write(Proto, {KeyType, KeyData}),
328 ok = write(Proto, {ValType, ValData})
329 end,
330 _AccO = ok,
331 Data),
332 ok = write(Proto, map_end),
333 ok;
David Reissae756f42008-06-10 22:57:11 +0000334
David Reissf32d0fb2010-08-30 22:05:00 +0000335write(Proto, {{set, Type}, Data}) ->
David Reissae756f42008-06-10 22:57:11 +0000336 true = sets:is_set(Data),
David Reissf32d0fb2010-08-30 22:05:00 +0000337 ok = write(Proto,
338 #protocol_set_begin{
339 etype = term_to_typeid(Type),
340 size = sets:size(Data)
341 }),
342 sets:fold(fun(Elem, _Acc) ->
343 ok = write(Proto, {Type, Elem})
344 end,
345 _Acc0 = ok,
346 Data),
347 ok = write(Proto, set_end),
348 ok;
David Reissae756f42008-06-10 22:57:11 +0000349
David Reissf32d0fb2010-08-30 22:05:00 +0000350write(#protocol{module = Module,
351 data = ModuleData}, Data) ->
352 Module:write(ModuleData, Data).
David Reissae756f42008-06-10 22:57:11 +0000353
David Reissf32d0fb2010-08-30 22:05:00 +0000354struct_write_loop(Proto, [{Fid, Type} | RestStructDef], [Data | RestData]) ->
355 case Data of
356 undefined ->
357 % null fields are skipped in response
358 skip;
359 _ ->
360 ok = write(Proto,
361 #protocol_field_begin{
362 type = term_to_typeid(Type),
363 id = Fid
364 }),
365 ok = write(Proto, {Type, Data}),
366 ok = write(Proto, field_end)
367 end,
368 struct_write_loop(Proto, RestStructDef, RestData);
David Reissae756f42008-06-10 22:57:11 +0000369struct_write_loop(Proto, [], []) ->
David Reissf32d0fb2010-08-30 22:05:00 +0000370 ok = write(Proto, field_stop),
371 ok.