blob: e82858a8473990463720af95bd55b749db77fd90 [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 Reiss6c187532010-08-30 22:05:33 +000092-spec read(#protocol{}, {struct, _StructDef}, atom()) -> {#protocol{}, {ok, tuple()}}.
93read(IProto0, {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 Reiss6c187532010-08-30 22:05:33 +0000110 {IProto1, ok} = read(IProto0, 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 Reiss6c187532010-08-30 22:05:33 +0000116 {IProto2, RTuple2} = read_struct_loop(IProto1, SDict, RTuple1),
117 {IProto2, {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
David Reiss6c187532010-08-30 22:05:33 +0000122 (#protocol{}, {struct, _Info}) -> {#protocol{}, {ok, tuple()} | {error, _Reason}};
123 (#protocol{}, tprot_cont_tag()) -> {#protocol{}, {ok, term()} | {error, _Reason}};
124 (#protocol{}, tprot_empty_tag()) -> {#protocol{}, ok | {error, _Reason}};
125 (#protocol{}, tprot_header_tag()) -> {#protocol{}, tprot_header_val() | {error, _Reason}};
126 (#protocol{}, tprot_data_tag()) -> {#protocol{}, {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 Reiss6c187532010-08-30 22:05:33 +0000135read(IProto0, {list, Type}) ->
136 {IProto1, #protocol_list_begin{etype = EType, size = Size}} =
137 read(IProto0, list_begin),
138 {List, IProto2} = lists:mapfoldl(fun(_, ProtoS0) ->
139 {ProtoS1, {ok, Item}} = read(ProtoS0, Type),
140 {Item, ProtoS1}
141 end,
142 IProto1,
143 lists:duplicate(Size, 0)),
144 {IProto3, ok} = read(IProto2, list_end),
145 {IProto3, {ok, List}};
David Reissae756f42008-06-10 22:57:11 +0000146
David Reiss6c187532010-08-30 22:05:33 +0000147read(IProto0, {map, KeyType, ValType}) ->
148 {IProto1, #protocol_map_begin{size = Size}} =
149 read(IProto0, map_begin),
150 {List, IProto2} = lists:mapfoldl(fun(_, ProtoS0) ->
151 {ProtoS1, {ok, Key}} = read(ProtoS0, KeyType),
152 {ProtoS2, {ok, Val}} = read(ProtoS1, ValType),
153 {{Key, Val}, ProtoS2}
154 end,
155 IProto1,
156 lists:duplicate(Size, 0)),
157 {IProto3, ok} = read(IProto2, map_end),
158 {IProto3, {ok, dict:from_list(List)}};
David Reissae756f42008-06-10 22:57:11 +0000159
David Reiss6c187532010-08-30 22:05:33 +0000160read(IProto0, {set, Type}) ->
161 {IProto1, #protocol_set_begin{etype = EType, size = Size}} =
162 read(IProto0, set_begin),
163 {List, IProto2} = lists:mapfoldl(fun(_, ProtoS0) ->
164 {ProtoS1, {ok, Item}} = read(ProtoS0, Type),
165 {Item, ProtoS1}
166 end,
167 IProto1,
168 lists:duplicate(Size, 0)),
169 {IProto3, ok} = read(IProto2, set_end),
170 {IProto3, {ok, sets:from_list(List)}};
David Reissae756f42008-06-10 22:57:11 +0000171
David Reiss480d5ab2010-08-30 22:05:26 +0000172read(Protocol, ProtocolType) ->
173 read_specific(Protocol, ProtocolType).
174
175%% NOTE: Keep this in sync with thrift_protocol_impl:read
176-spec read_specific
David Reiss6c187532010-08-30 22:05:33 +0000177 (#protocol{}, tprot_empty_tag()) -> {#protocol{}, ok | {error, _Reason}};
178 (#protocol{}, tprot_header_tag()) -> {#protocol{}, tprot_header_val() | {error, _Reason}};
179 (#protocol{}, tprot_data_tag()) -> {#protocol{}, {ok, term()} | {error, _Reason}}.
180read_specific(Proto = #protocol{module = Module,
181 data = ModuleData}, ProtocolType) ->
182 {NewData, Result} = Module:read(ModuleData, ProtocolType),
183 {Proto#protocol{data = NewData}, Result}.
David Reissac549552008-06-10 22:56:59 +0000184
David Reiss6c187532010-08-30 22:05:33 +0000185read_struct_loop(IProto0, SDict, RTuple) ->
186 {IProto1, #protocol_field_begin{type = FType, id = Fid, name = Name}} =
187 thrift_protocol:read(IProto0, field_begin),
David Reissae756f42008-06-10 22:57:11 +0000188 case {FType, Fid} of
189 {?tType_STOP, _} ->
David Reiss6c187532010-08-30 22:05:33 +0000190 {IProto1, RTuple};
David Reissae756f42008-06-10 22:57:11 +0000191 _Else ->
192 case dict:find(Fid, SDict) of
193 {ok, {Type, Index}} ->
David Reiss233ace52009-03-30 20:46:47 +0000194 case term_to_typeid(Type) of
195 FType ->
David Reiss6c187532010-08-30 22:05:33 +0000196 {IProto2, {ok, Val}} = read(IProto1, Type),
197 {IProto3, ok} = thrift_protocol:read(IProto2, field_end),
David Reiss233ace52009-03-30 20:46:47 +0000198 NewRTuple = setelement(Index, RTuple, Val),
David Reiss6c187532010-08-30 22:05:33 +0000199 read_struct_loop(IProto3, SDict, NewRTuple);
David Reiss233ace52009-03-30 20:46:47 +0000200 Expected ->
201 error_logger:info_msg(
202 "Skipping field ~p with wrong type (~p != ~p)~n",
203 [Fid, FType, Expected]),
David Reiss6c187532010-08-30 22:05:33 +0000204 skip_field(FType, IProto1, SDict, RTuple)
David Reiss233ace52009-03-30 20:46:47 +0000205 end;
David Reissae756f42008-06-10 22:57:11 +0000206 _Else2 ->
David Reiss233ace52009-03-30 20:46:47 +0000207 error_logger:info_msg("Skipping field ~p with unknown fid~n", [Fid]),
David Reiss6c187532010-08-30 22:05:33 +0000208 skip_field(FType, IProto1, SDict, RTuple)
David Reissae756f42008-06-10 22:57:11 +0000209 end
210 end.
David Reissac549552008-06-10 22:56:59 +0000211
David Reiss6c187532010-08-30 22:05:33 +0000212skip_field(FType, IProto0, SDict, RTuple) ->
David Reiss233ace52009-03-30 20:46:47 +0000213 FTypeAtom = thrift_protocol:typeid_to_atom(FType),
David Reiss6c187532010-08-30 22:05:33 +0000214 {IProto1, ok} = thrift_protocol:skip(IProto0, FTypeAtom),
215 {IProto2, ok} = read(IProto1, field_end),
216 read_struct_loop(IProto2, SDict, RTuple).
David Reiss233ace52009-03-30 20:46:47 +0000217
David Reiss6c187532010-08-30 22:05:33 +0000218-spec skip(#protocol{}, term()) -> {#protocol{}, ok}.
David Reissac549552008-06-10 22:56:59 +0000219
David Reiss6c187532010-08-30 22:05:33 +0000220skip(Proto0, struct) ->
221 {Proto1, ok} = read(Proto0, struct_begin),
222 {Proto2, ok} = skip_struct_loop(Proto1),
223 {Proto3, ok} = read(Proto2, struct_end),
224 {Proto3, ok};
David Reissf32d0fb2010-08-30 22:05:00 +0000225
David Reiss6c187532010-08-30 22:05:33 +0000226skip(Proto0, map) ->
227 {Proto1, Map} = read(Proto0, map_begin),
228 {Proto2, ok} = skip_map_loop(Proto1, Map),
229 {Proto3, ok} = read(Proto2, map_end),
230 {Proto3, ok};
David Reissf32d0fb2010-08-30 22:05:00 +0000231
David Reiss6c187532010-08-30 22:05:33 +0000232skip(Proto0, set) ->
233 {Proto1, Set} = read(Proto0, set_begin),
234 {Proto2, ok} = skip_set_loop(Proto1, Set),
235 {Proto3, ok} = read(Proto2, set_end),
236 {Proto3, ok};
David Reissf32d0fb2010-08-30 22:05:00 +0000237
David Reiss6c187532010-08-30 22:05:33 +0000238skip(Proto0, list) ->
239 {Proto1, List} = read(Proto0, list_begin),
240 {Proto2, ok} = skip_list_loop(Proto1, List),
241 {Proto3, ok} = read(Proto2, list_end),
242 {Proto3, ok};
David Reissf32d0fb2010-08-30 22:05:00 +0000243
David Reiss6c187532010-08-30 22:05:33 +0000244skip(Proto0, Type) when is_atom(Type) ->
245 {Proto1, _Ignore} = read(Proto0, Type),
246 {Proto1, ok}.
David Reissf32d0fb2010-08-30 22:05:00 +0000247
248
David Reiss6c187532010-08-30 22:05:33 +0000249skip_struct_loop(Proto0) ->
250 {Proto1, #protocol_field_begin{type = Type}} = read(Proto0, field_begin),
David Reissac549552008-06-10 22:56:59 +0000251 case Type of
252 ?tType_STOP ->
David Reiss6c187532010-08-30 22:05:33 +0000253 {Proto1, ok};
David Reissac549552008-06-10 22:56:59 +0000254 _Else ->
David Reiss6c187532010-08-30 22:05:33 +0000255 {Proto2, ok} = skip(Proto1, Type),
256 {Proto3, ok} = read(Proto2, field_end),
257 skip_struct_loop(Proto3)
David Reissac549552008-06-10 22:56:59 +0000258 end.
259
David Reiss6c187532010-08-30 22:05:33 +0000260skip_map_loop(Proto0, Map = #protocol_map_begin{ktype = Ktype,
261 vtype = Vtype,
262 size = Size}) ->
David Reissac549552008-06-10 22:56:59 +0000263 case Size of
264 N when N > 0 ->
David Reiss6c187532010-08-30 22:05:33 +0000265 {Proto1, ok} = skip(Proto0, Ktype),
266 {Proto2, ok} = skip(Proto1, Vtype),
267 skip_map_loop(Proto2,
David Reissac549552008-06-10 22:56:59 +0000268 Map#protocol_map_begin{size = Size - 1});
David Reiss6c187532010-08-30 22:05:33 +0000269 0 -> {Proto0, ok}
David Reissac549552008-06-10 22:56:59 +0000270 end.
271
David Reiss6c187532010-08-30 22:05:33 +0000272skip_set_loop(Proto0, Map = #protocol_set_begin{etype = Etype,
273 size = Size}) ->
David Reissac549552008-06-10 22:56:59 +0000274 case Size of
275 N when N > 0 ->
David Reiss6c187532010-08-30 22:05:33 +0000276 {Proto1, ok} = skip(Proto0, Etype),
277 skip_set_loop(Proto1,
David Reissac549552008-06-10 22:56:59 +0000278 Map#protocol_set_begin{size = Size - 1});
David Reiss6c187532010-08-30 22:05:33 +0000279 0 -> {Proto0, ok}
David Reissac549552008-06-10 22:56:59 +0000280 end.
281
David Reiss6c187532010-08-30 22:05:33 +0000282skip_list_loop(Proto0, Map = #protocol_list_begin{etype = Etype,
283 size = Size}) ->
David Reissac549552008-06-10 22:56:59 +0000284 case Size of
285 N when N > 0 ->
David Reiss6c187532010-08-30 22:05:33 +0000286 {Proto1, ok} = skip(Proto0, Etype),
287 skip_list_loop(Proto1,
David Reissac549552008-06-10 22:56:59 +0000288 Map#protocol_list_begin{size = Size - 1});
David Reiss6c187532010-08-30 22:05:33 +0000289 0 -> {Proto0, ok}
David Reissac549552008-06-10 22:56:59 +0000290 end.
David Reissae756f42008-06-10 22:57:11 +0000291
292
293%%--------------------------------------------------------------------
294%% Function: write(OProto, {Type, Data}) -> ok
David Reiss6b3e40f2008-06-11 00:59:03 +0000295%%
David Reissae756f42008-06-10 22:57:11 +0000296%% Type = {struct, StructDef} |
297%% {list, Type} |
298%% {map, KeyType, ValType} |
299%% {set, Type} |
300%% BaseType
301%%
302%% Data =
303%% tuple() -- for struct
304%% | list() -- for list
305%% | dictionary() -- for map
306%% | set() -- for set
David Reissf32d0fb2010-08-30 22:05:00 +0000307%% | term() -- for base types
David Reissae756f42008-06-10 22:57:11 +0000308%%
David Reiss6b3e40f2008-06-11 00:59:03 +0000309%% Description:
David Reissae756f42008-06-10 22:57:11 +0000310%%--------------------------------------------------------------------
David Reissc4657992010-08-30 22:05:31 +0000311-spec write(#protocol{}, term()) -> {#protocol{}, ok | {error, _Reason}}.
David Reiss5e6637b2010-08-30 22:05:18 +0000312
David Reissc4657992010-08-30 22:05:31 +0000313write(Proto0, {{struct, StructDef}, Data})
David Reiss76f0d112008-06-10 22:57:35 +0000314 when is_list(StructDef), is_tuple(Data), length(StructDef) == size(Data) - 1 ->
315
316 [StructName | Elems] = tuple_to_list(Data),
David Reissc4657992010-08-30 22:05:31 +0000317 {Proto1, ok} = write(Proto0, #protocol_struct_begin{name = StructName}),
318 {Proto2, ok} = struct_write_loop(Proto1, StructDef, Elems),
319 {Proto3, ok} = write(Proto2, struct_end),
320 {Proto3, ok};
David Reissae756f42008-06-10 22:57:11 +0000321
David Reiss76f0d112008-06-10 22:57:35 +0000322write(Proto, {{struct, {Module, StructureName}}, Data})
323 when is_atom(Module),
324 is_atom(StructureName),
325 element(1, Data) =:= StructureName ->
David Reissf32d0fb2010-08-30 22:05:00 +0000326 StructType = Module:struct_info(StructureName),
David Reiss76f0d112008-06-10 22:57:35 +0000327 write(Proto, {Module:struct_info(StructureName), Data});
328
David Reissc4657992010-08-30 22:05:31 +0000329write(Proto0, {{list, Type}, Data})
David Reissae756f42008-06-10 22:57:11 +0000330 when is_list(Data) ->
David Reissc4657992010-08-30 22:05:31 +0000331 {Proto1, ok} = write(Proto0,
David Reissae756f42008-06-10 22:57:11 +0000332 #protocol_list_begin{
333 etype = term_to_typeid(Type),
334 size = length(Data)
335 }),
David Reissc4657992010-08-30 22:05:31 +0000336 Proto2 = lists:foldl(fun(Elem, ProtoIn) ->
337 {ProtoOut, ok} = write(ProtoIn, {Type, Elem}),
338 ProtoOut
339 end,
340 Proto1,
341 Data),
342 {Proto3, ok} = write(Proto2, list_end),
343 {Proto3, ok};
David Reissae756f42008-06-10 22:57:11 +0000344
David Reissc4657992010-08-30 22:05:31 +0000345write(Proto0, {{map, KeyType, ValType}, Data}) ->
346 {Proto1, ok} = write(Proto0,
347 #protocol_map_begin{
348 ktype = term_to_typeid(KeyType),
349 vtype = term_to_typeid(ValType),
350 size = dict:size(Data)
351 }),
352 Proto2 = dict:fold(fun(KeyData, ValData, ProtoS0) ->
353 {ProtoS1, ok} = write(ProtoS0, {KeyType, KeyData}),
354 {ProtoS2, ok} = write(ProtoS1, {ValType, ValData}),
355 ProtoS2
356 end,
357 Proto1,
358 Data),
359 {Proto3, ok} = write(Proto2, map_end),
360 {Proto3, ok};
David Reissae756f42008-06-10 22:57:11 +0000361
David Reissc4657992010-08-30 22:05:31 +0000362write(Proto0, {{set, Type}, Data}) ->
David Reissae756f42008-06-10 22:57:11 +0000363 true = sets:is_set(Data),
David Reissc4657992010-08-30 22:05:31 +0000364 {Proto1, ok} = write(Proto0,
365 #protocol_set_begin{
366 etype = term_to_typeid(Type),
367 size = sets:size(Data)
368 }),
369 Proto2 = sets:fold(fun(Elem, ProtoIn) ->
370 {ProtoOut, ok} = write(ProtoIn, {Type, Elem}),
371 ProtoOut
372 end,
373 Proto1,
374 Data),
375 {Proto3, ok} = write(Proto2, set_end),
376 {Proto3, ok};
David Reissae756f42008-06-10 22:57:11 +0000377
David Reissc4657992010-08-30 22:05:31 +0000378write(Proto = #protocol{module = Module,
379 data = ModuleData}, Data) ->
380 {NewData, Result} = Module:write(ModuleData, Data),
381 {Proto#protocol{data = NewData}, Result}.
David Reissae756f42008-06-10 22:57:11 +0000382
David Reissc4657992010-08-30 22:05:31 +0000383struct_write_loop(Proto0, [{Fid, Type} | RestStructDef], [Data | RestData]) ->
384 NewProto = case Data of
385 undefined ->
386 Proto0; % null fields are skipped in response
387 _ ->
388 {Proto1, ok} = write(Proto0,
389 #protocol_field_begin{
390 type = term_to_typeid(Type),
391 id = Fid
392 }),
393 {Proto2, ok} = write(Proto1, {Type, Data}),
394 {Proto3, ok} = write(Proto2, field_end),
395 Proto3
396 end,
397 struct_write_loop(NewProto, RestStructDef, RestData);
David Reissae756f42008-06-10 22:57:11 +0000398struct_write_loop(Proto, [], []) ->
David Reissc4657992010-08-30 22:05:31 +0000399 write(Proto, field_stop).