blob: 1caba1b1423af5d0cb0af3ed1916cd3df6effe9e [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 Reissc4657992010-08-30 22:05:31 +000052-spec flush_transport(#protocol{}) -> {#protocol{}, ok}.
53flush_transport(Proto = #protocol{module = Module,
54 data = Data}) ->
55 {NewData, Result} = Module:flush_transport(Data),
56 {Proto#protocol{data = NewData}, Result}.
David Reiss90b40832008-06-10 22:58:52 +000057
David Reiss5e6637b2010-08-30 22:05:18 +000058-spec close_transport(#protocol{}) -> ok.
David Reissc11734e2008-06-11 00:59:48 +000059close_transport(#protocol{module = Module,
60 data = Data}) ->
61 Module:close_transport(Data).
62
David Reissac549552008-06-10 22:56:59 +000063typeid_to_atom(?tType_STOP) -> field_stop;
64typeid_to_atom(?tType_VOID) -> void;
65typeid_to_atom(?tType_BOOL) -> bool;
66typeid_to_atom(?tType_BYTE) -> byte;
67typeid_to_atom(?tType_DOUBLE) -> double;
68typeid_to_atom(?tType_I16) -> i16;
69typeid_to_atom(?tType_I32) -> i32;
70typeid_to_atom(?tType_I64) -> i64;
71typeid_to_atom(?tType_STRING) -> string;
72typeid_to_atom(?tType_STRUCT) -> struct;
73typeid_to_atom(?tType_MAP) -> map;
74typeid_to_atom(?tType_SET) -> set;
75typeid_to_atom(?tType_LIST) -> list.
David Reissae756f42008-06-10 22:57:11 +000076
77term_to_typeid(void) -> ?tType_VOID;
78term_to_typeid(bool) -> ?tType_BOOL;
79term_to_typeid(byte) -> ?tType_BYTE;
80term_to_typeid(double) -> ?tType_DOUBLE;
81term_to_typeid(i16) -> ?tType_I16;
82term_to_typeid(i32) -> ?tType_I32;
83term_to_typeid(i64) -> ?tType_I64;
84term_to_typeid(string) -> ?tType_STRING;
85term_to_typeid({struct, _}) -> ?tType_STRUCT;
86term_to_typeid({map, _, _}) -> ?tType_MAP;
87term_to_typeid({set, _}) -> ?tType_SET;
88term_to_typeid({list, _}) -> ?tType_LIST.
89
David Reissae756f42008-06-10 22:57:11 +000090%% Structure is like:
91%% [{Fid, Type}, ...]
David Reiss5e6637b2010-08-30 22:05:18 +000092-spec read(#protocol{}, {struct, _StructDef}, atom()) -> {ok, tuple()}.
David Reissf32d0fb2010-08-30 22:05:00 +000093read(IProto, {struct, Structure}, Tag)
David Reiss58a961a2008-06-11 01:13:19 +000094 when is_list(Structure), is_atom(Tag) ->
95
96 % If we want a tagged tuple, we need to offset all the tuple indices
97 % by 1 to avoid overwriting the tag.
98 Offset = if Tag =/= undefined -> 1; true -> 0 end,
David Reisseea82982008-06-10 22:58:21 +000099 IndexList = case length(Structure) of
David Reiss58a961a2008-06-11 01:13:19 +0000100 N when N > 0 -> lists:seq(1 + Offset, N + Offset);
David Reisseea82982008-06-10 22:58:21 +0000101 _ -> []
102 end,
103
David Reissae756f42008-06-10 22:57:11 +0000104 SWithIndices = [{Fid, {Type, Index}} ||
105 {{Fid, Type}, Index} <-
David Reisseea82982008-06-10 22:58:21 +0000106 lists:zip(Structure, IndexList)],
David Reissae756f42008-06-10 22:57:11 +0000107 % Fid -> {Type, Index}
108 SDict = dict:from_list(SWithIndices),
109
David Reissf32d0fb2010-08-30 22:05:00 +0000110 ok = read(IProto, struct_begin),
David Reiss58a961a2008-06-11 01:13:19 +0000111 RTuple0 = erlang:make_tuple(length(Structure) + Offset, undefined),
112 RTuple1 = if Tag =/= undefined -> setelement(1, RTuple0, Tag);
113 true -> RTuple0
114 end,
David Reissae756f42008-06-10 22:57:11 +0000115
David Reissf32d0fb2010-08-30 22:05:00 +0000116 RTuple2 = read_struct_loop(IProto, SDict, RTuple1),
117 {ok, RTuple2}.
David Reissae756f42008-06-10 22:57:11 +0000118
David Reiss1cb979b2010-08-30 22:05:25 +0000119
120%% NOTE: Keep this in sync with thrift_protocol_impl:read
121-spec read
122 (#protocol{}, {struct, _Info}) -> {ok, tuple()} | {error, _Reason};
123 (#protocol{}, tprot_cont_tag()) -> {ok, term()} | {error, _Reason};
124 (#protocol{}, tprot_empty_tag()) -> ok | {error, _Reason};
125 (#protocol{}, tprot_header_tag()) -> tprot_header_val() | {error, _Reason};
126 (#protocol{}, tprot_data_tag()) -> {ok, term()} | {error, _Reason}.
David Reiss5e6637b2010-08-30 22:05:18 +0000127
David Reiss76f0d112008-06-10 22:57:35 +0000128read(IProto, {struct, {Module, StructureName}}) when is_atom(Module),
129 is_atom(StructureName) ->
David Reiss58a961a2008-06-11 01:13:19 +0000130 read(IProto, Module:struct_info(StructureName), StructureName);
131
132read(IProto, S={struct, Structure}) when is_list(Structure) ->
133 read(IProto, S, undefined);
David Reiss76f0d112008-06-10 22:57:35 +0000134
David Reissf32d0fb2010-08-30 22:05:00 +0000135read(IProto, {list, Type}) ->
136 #protocol_list_begin{etype = EType, size = Size} =
137 read(IProto, list_begin),
138 List = [Result || {ok, Result} <-
139 [read(IProto, Type) || _X <- lists:duplicate(Size, 0)]],
140 ok = read(IProto, list_end),
141 {ok, List};
David Reissae756f42008-06-10 22:57:11 +0000142
David Reissf32d0fb2010-08-30 22:05:00 +0000143read(IProto, {map, KeyType, ValType}) ->
144 #protocol_map_begin{size = Size} =
145 read(IProto, map_begin),
David Reissae756f42008-06-10 22:57:11 +0000146
David Reissf32d0fb2010-08-30 22:05:00 +0000147 List = [{Key, Val} || {{ok, Key}, {ok, Val}} <-
148 [{read(IProto, KeyType),
149 read(IProto, ValType)} || _X <- lists:duplicate(Size, 0)]],
150 ok = read(IProto, map_end),
151 {ok, dict:from_list(List)};
David Reissae756f42008-06-10 22:57:11 +0000152
David Reissf32d0fb2010-08-30 22:05:00 +0000153read(IProto, {set, Type}) ->
154 #protocol_set_begin{etype = _EType,
155 size = Size} =
156 read(IProto, set_begin),
157 List = [Result || {ok, Result} <-
158 [read(IProto, Type) || _X <- lists:duplicate(Size, 0)]],
159 ok = read(IProto, set_end),
160 {ok, sets:from_list(List)};
David Reissae756f42008-06-10 22:57:11 +0000161
David Reiss480d5ab2010-08-30 22:05:26 +0000162read(Protocol, ProtocolType) ->
163 read_specific(Protocol, ProtocolType).
164
165%% NOTE: Keep this in sync with thrift_protocol_impl:read
166-spec read_specific
167 (#protocol{}, tprot_empty_tag()) -> ok | {error, _Reason};
168 (#protocol{}, tprot_header_tag()) -> tprot_header_val() | {error, _Reason};
169 (#protocol{}, tprot_data_tag()) -> {ok, term()} | {error, _Reason}.
170read_specific(#protocol{module = Module,
171 data = ModuleData}, ProtocolType) ->
David Reissf32d0fb2010-08-30 22:05:00 +0000172 Module:read(ModuleData, ProtocolType).
David Reissac549552008-06-10 22:56:59 +0000173
David Reissf32d0fb2010-08-30 22:05:00 +0000174read_struct_loop(IProto, SDict, RTuple) ->
175 #protocol_field_begin{type = FType, id = Fid, name = Name} =
176 thrift_protocol:read(IProto, field_begin),
David Reissae756f42008-06-10 22:57:11 +0000177 case {FType, Fid} of
178 {?tType_STOP, _} ->
David Reissf32d0fb2010-08-30 22:05:00 +0000179 RTuple;
David Reissae756f42008-06-10 22:57:11 +0000180 _Else ->
181 case dict:find(Fid, SDict) of
182 {ok, {Type, Index}} ->
David Reiss233ace52009-03-30 20:46:47 +0000183 case term_to_typeid(Type) of
184 FType ->
David Reissf32d0fb2010-08-30 22:05:00 +0000185 {ok, Val} = read(IProto, Type),
186 thrift_protocol:read(IProto, field_end),
David Reiss233ace52009-03-30 20:46:47 +0000187 NewRTuple = setelement(Index, RTuple, Val),
David Reissf32d0fb2010-08-30 22:05:00 +0000188 read_struct_loop(IProto, SDict, NewRTuple);
David Reiss233ace52009-03-30 20:46:47 +0000189 Expected ->
190 error_logger:info_msg(
191 "Skipping field ~p with wrong type (~p != ~p)~n",
192 [Fid, FType, Expected]),
David Reissf32d0fb2010-08-30 22:05:00 +0000193 skip_field(FType, IProto, SDict, RTuple)
David Reiss233ace52009-03-30 20:46:47 +0000194 end;
David Reissae756f42008-06-10 22:57:11 +0000195 _Else2 ->
David Reiss233ace52009-03-30 20:46:47 +0000196 error_logger:info_msg("Skipping field ~p with unknown fid~n", [Fid]),
David Reissf32d0fb2010-08-30 22:05:00 +0000197 skip_field(FType, IProto, SDict, RTuple)
David Reissae756f42008-06-10 22:57:11 +0000198 end
199 end.
David Reissac549552008-06-10 22:56:59 +0000200
David Reissf32d0fb2010-08-30 22:05:00 +0000201skip_field(FType, IProto, SDict, RTuple) ->
David Reiss233ace52009-03-30 20:46:47 +0000202 FTypeAtom = thrift_protocol:typeid_to_atom(FType),
David Reissf32d0fb2010-08-30 22:05:00 +0000203 thrift_protocol:skip(IProto, FTypeAtom),
204 read(IProto, field_end),
205 read_struct_loop(IProto, SDict, RTuple).
David Reiss233ace52009-03-30 20:46:47 +0000206
David Reiss5e6637b2010-08-30 22:05:18 +0000207-spec skip(#protocol{}, term()) -> ok.
David Reissac549552008-06-10 22:56:59 +0000208
David Reissf32d0fb2010-08-30 22:05:00 +0000209skip(Proto, struct) ->
210 ok = read(Proto, struct_begin),
211 ok = skip_struct_loop(Proto),
212 ok = read(Proto, struct_end);
213
214skip(Proto, map) ->
215 Map = read(Proto, map_begin),
216 ok = skip_map_loop(Proto, Map),
217 ok = read(Proto, map_end);
218
219skip(Proto, set) ->
220 Set = read(Proto, set_begin),
221 ok = skip_set_loop(Proto, Set),
222 ok = read(Proto, set_end);
223
224skip(Proto, list) ->
225 List = read(Proto, list_begin),
226 ok = skip_list_loop(Proto, List),
227 ok = read(Proto, list_end);
228
229skip(Proto, Type) when is_atom(Type) ->
230 _Ignore = read(Proto, Type),
231 ok.
232
233
234skip_struct_loop(Proto) ->
235 #protocol_field_begin{type = Type} = read(Proto, field_begin),
David Reissac549552008-06-10 22:56:59 +0000236 case Type of
237 ?tType_STOP ->
David Reissf32d0fb2010-08-30 22:05:00 +0000238 ok;
David Reissac549552008-06-10 22:56:59 +0000239 _Else ->
David Reissf32d0fb2010-08-30 22:05:00 +0000240 skip(Proto, Type),
241 ok = read(Proto, field_end),
242 skip_struct_loop(Proto)
David Reissac549552008-06-10 22:56:59 +0000243 end.
244
David Reissf32d0fb2010-08-30 22:05:00 +0000245skip_map_loop(Proto, Map = #protocol_map_begin{ktype = Ktype,
246 vtype = Vtype,
247 size = Size}) ->
David Reissac549552008-06-10 22:56:59 +0000248 case Size of
249 N when N > 0 ->
David Reissf32d0fb2010-08-30 22:05:00 +0000250 skip(Proto, Ktype),
251 skip(Proto, Vtype),
252 skip_map_loop(Proto,
David Reissac549552008-06-10 22:56:59 +0000253 Map#protocol_map_begin{size = Size - 1});
David Reissf32d0fb2010-08-30 22:05:00 +0000254 0 -> ok
David Reissac549552008-06-10 22:56:59 +0000255 end.
256
David Reissf32d0fb2010-08-30 22:05:00 +0000257skip_set_loop(Proto, Map = #protocol_set_begin{etype = Etype,
258 size = Size}) ->
David Reissac549552008-06-10 22:56:59 +0000259 case Size of
260 N when N > 0 ->
David Reissf32d0fb2010-08-30 22:05:00 +0000261 skip(Proto, Etype),
262 skip_set_loop(Proto,
David Reissac549552008-06-10 22:56:59 +0000263 Map#protocol_set_begin{size = Size - 1});
David Reissf32d0fb2010-08-30 22:05:00 +0000264 0 -> ok
David Reissac549552008-06-10 22:56:59 +0000265 end.
266
David Reissf32d0fb2010-08-30 22:05:00 +0000267skip_list_loop(Proto, Map = #protocol_list_begin{etype = Etype,
268 size = Size}) ->
David Reissac549552008-06-10 22:56:59 +0000269 case Size of
270 N when N > 0 ->
David Reissf32d0fb2010-08-30 22:05:00 +0000271 skip(Proto, Etype),
272 skip_list_loop(Proto,
David Reissac549552008-06-10 22:56:59 +0000273 Map#protocol_list_begin{size = Size - 1});
David Reissf32d0fb2010-08-30 22:05:00 +0000274 0 -> ok
David Reissac549552008-06-10 22:56:59 +0000275 end.
David Reissae756f42008-06-10 22:57:11 +0000276
277
278%%--------------------------------------------------------------------
279%% Function: write(OProto, {Type, Data}) -> ok
David Reiss6b3e40f2008-06-11 00:59:03 +0000280%%
David Reissae756f42008-06-10 22:57:11 +0000281%% Type = {struct, StructDef} |
282%% {list, Type} |
283%% {map, KeyType, ValType} |
284%% {set, Type} |
285%% BaseType
286%%
287%% Data =
288%% tuple() -- for struct
289%% | list() -- for list
290%% | dictionary() -- for map
291%% | set() -- for set
David Reissf32d0fb2010-08-30 22:05:00 +0000292%% | term() -- for base types
David Reissae756f42008-06-10 22:57:11 +0000293%%
David Reiss6b3e40f2008-06-11 00:59:03 +0000294%% Description:
David Reissae756f42008-06-10 22:57:11 +0000295%%--------------------------------------------------------------------
David Reissc4657992010-08-30 22:05:31 +0000296-spec write(#protocol{}, term()) -> {#protocol{}, ok | {error, _Reason}}.
David Reiss5e6637b2010-08-30 22:05:18 +0000297
David Reissc4657992010-08-30 22:05:31 +0000298write(Proto0, {{struct, StructDef}, Data})
David Reiss76f0d112008-06-10 22:57:35 +0000299 when is_list(StructDef), is_tuple(Data), length(StructDef) == size(Data) - 1 ->
300
301 [StructName | Elems] = tuple_to_list(Data),
David Reissc4657992010-08-30 22:05:31 +0000302 {Proto1, ok} = write(Proto0, #protocol_struct_begin{name = StructName}),
303 {Proto2, ok} = struct_write_loop(Proto1, StructDef, Elems),
304 {Proto3, ok} = write(Proto2, struct_end),
305 {Proto3, ok};
David Reissae756f42008-06-10 22:57:11 +0000306
David Reiss76f0d112008-06-10 22:57:35 +0000307write(Proto, {{struct, {Module, StructureName}}, Data})
308 when is_atom(Module),
309 is_atom(StructureName),
310 element(1, Data) =:= StructureName ->
David Reissf32d0fb2010-08-30 22:05:00 +0000311 StructType = Module:struct_info(StructureName),
David Reiss76f0d112008-06-10 22:57:35 +0000312 write(Proto, {Module:struct_info(StructureName), Data});
313
David Reissc4657992010-08-30 22:05:31 +0000314write(Proto0, {{list, Type}, Data})
David Reissae756f42008-06-10 22:57:11 +0000315 when is_list(Data) ->
David Reissc4657992010-08-30 22:05:31 +0000316 {Proto1, ok} = write(Proto0,
David Reissae756f42008-06-10 22:57:11 +0000317 #protocol_list_begin{
318 etype = term_to_typeid(Type),
319 size = length(Data)
320 }),
David Reissc4657992010-08-30 22:05:31 +0000321 Proto2 = lists:foldl(fun(Elem, ProtoIn) ->
322 {ProtoOut, ok} = write(ProtoIn, {Type, Elem}),
323 ProtoOut
324 end,
325 Proto1,
326 Data),
327 {Proto3, ok} = write(Proto2, list_end),
328 {Proto3, ok};
David Reissae756f42008-06-10 22:57:11 +0000329
David Reissc4657992010-08-30 22:05:31 +0000330write(Proto0, {{map, KeyType, ValType}, Data}) ->
331 {Proto1, ok} = write(Proto0,
332 #protocol_map_begin{
333 ktype = term_to_typeid(KeyType),
334 vtype = term_to_typeid(ValType),
335 size = dict:size(Data)
336 }),
337 Proto2 = dict:fold(fun(KeyData, ValData, ProtoS0) ->
338 {ProtoS1, ok} = write(ProtoS0, {KeyType, KeyData}),
339 {ProtoS2, ok} = write(ProtoS1, {ValType, ValData}),
340 ProtoS2
341 end,
342 Proto1,
343 Data),
344 {Proto3, ok} = write(Proto2, map_end),
345 {Proto3, ok};
David Reissae756f42008-06-10 22:57:11 +0000346
David Reissc4657992010-08-30 22:05:31 +0000347write(Proto0, {{set, Type}, Data}) ->
David Reissae756f42008-06-10 22:57:11 +0000348 true = sets:is_set(Data),
David Reissc4657992010-08-30 22:05:31 +0000349 {Proto1, ok} = write(Proto0,
350 #protocol_set_begin{
351 etype = term_to_typeid(Type),
352 size = sets:size(Data)
353 }),
354 Proto2 = sets:fold(fun(Elem, ProtoIn) ->
355 {ProtoOut, ok} = write(ProtoIn, {Type, Elem}),
356 ProtoOut
357 end,
358 Proto1,
359 Data),
360 {Proto3, ok} = write(Proto2, set_end),
361 {Proto3, ok};
David Reissae756f42008-06-10 22:57:11 +0000362
David Reissc4657992010-08-30 22:05:31 +0000363write(Proto = #protocol{module = Module,
364 data = ModuleData}, Data) ->
365 {NewData, Result} = Module:write(ModuleData, Data),
366 {Proto#protocol{data = NewData}, Result}.
David Reissae756f42008-06-10 22:57:11 +0000367
David Reissc4657992010-08-30 22:05:31 +0000368struct_write_loop(Proto0, [{Fid, Type} | RestStructDef], [Data | RestData]) ->
369 NewProto = case Data of
370 undefined ->
371 Proto0; % null fields are skipped in response
372 _ ->
373 {Proto1, ok} = write(Proto0,
374 #protocol_field_begin{
375 type = term_to_typeid(Type),
376 id = Fid
377 }),
378 {Proto2, ok} = write(Proto1, {Type, Data}),
379 {Proto3, ok} = write(Proto2, field_end),
380 Proto3
381 end,
382 struct_write_loop(NewProto, RestStructDef, RestData);
David Reissae756f42008-06-10 22:57:11 +0000383struct_write_loop(Proto, [], []) ->
David Reissc4657992010-08-30 22:05:31 +0000384 write(Proto, field_stop).