blob: b63f9514333083a44faac79a4449a5b47ff49aea [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;
David Reissac549552008-06-10 22:56:59 +000066typeid_to_atom(?tType_DOUBLE) -> double;
Jens Geyer40c28d32015-10-20 23:13:02 +020067typeid_to_atom(?tType_I8) -> byte;
David Reissac549552008-06-10 22:56:59 +000068typeid_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;
Jens Geyer40c28d32015-10-20 23:13:02 +020079term_to_typeid(byte) -> ?tType_I8;
David Reissae756f42008-06-10 22:57:11 +000080term_to_typeid(double) -> ?tType_DOUBLE;
Jens Geyer40c28d32015-10-20 23:13:02 +020081term_to_typeid(i8) -> ?tType_I8;
David Reissae756f42008-06-10 22:57:11 +000082term_to_typeid(i16) -> ?tType_I16;
83term_to_typeid(i32) -> ?tType_I32;
84term_to_typeid(i64) -> ?tType_I64;
85term_to_typeid(string) -> ?tType_STRING;
86term_to_typeid({struct, _}) -> ?tType_STRUCT;
87term_to_typeid({map, _, _}) -> ?tType_MAP;
88term_to_typeid({set, _}) -> ?tType_SET;
89term_to_typeid({list, _}) -> ?tType_LIST.
90
David Reissae756f42008-06-10 22:57:11 +000091%% Structure is like:
92%% [{Fid, Type}, ...]
David Reiss6c187532010-08-30 22:05:33 +000093-spec read(#protocol{}, {struct, _StructDef}, atom()) -> {#protocol{}, {ok, tuple()}}.
94read(IProto0, {struct, Structure}, Tag)
David Reiss58a961a2008-06-11 01:13:19 +000095 when is_list(Structure), is_atom(Tag) ->
96
97 % If we want a tagged tuple, we need to offset all the tuple indices
98 % by 1 to avoid overwriting the tag.
99 Offset = if Tag =/= undefined -> 1; true -> 0 end,
David Reisseea82982008-06-10 22:58:21 +0000100 IndexList = case length(Structure) of
David Reiss58a961a2008-06-11 01:13:19 +0000101 N when N > 0 -> lists:seq(1 + Offset, N + Offset);
David Reisseea82982008-06-10 22:58:21 +0000102 _ -> []
103 end,
104
David Reissae756f42008-06-10 22:57:11 +0000105 SWithIndices = [{Fid, {Type, Index}} ||
106 {{Fid, Type}, Index} <-
David Reisseea82982008-06-10 22:58:21 +0000107 lists:zip(Structure, IndexList)],
David Reissae756f42008-06-10 22:57:11 +0000108 % Fid -> {Type, Index}
109 SDict = dict:from_list(SWithIndices),
110
David Reiss6c187532010-08-30 22:05:33 +0000111 {IProto1, ok} = read(IProto0, struct_begin),
David Reiss58a961a2008-06-11 01:13:19 +0000112 RTuple0 = erlang:make_tuple(length(Structure) + Offset, undefined),
113 RTuple1 = if Tag =/= undefined -> setelement(1, RTuple0, Tag);
114 true -> RTuple0
115 end,
David Reissae756f42008-06-10 22:57:11 +0000116
David Reiss6c187532010-08-30 22:05:33 +0000117 {IProto2, RTuple2} = read_struct_loop(IProto1, SDict, RTuple1),
118 {IProto2, {ok, RTuple2}}.
David Reissae756f42008-06-10 22:57:11 +0000119
David Reiss1cb979b2010-08-30 22:05:25 +0000120
David Reisscf7f3972010-08-30 22:05:55 +0000121%% NOTE: Keep this in sync with thrift_protocol_behaviour:read
David Reiss1cb979b2010-08-30 22:05:25 +0000122-spec read
David Reiss6c187532010-08-30 22:05:33 +0000123 (#protocol{}, {struct, _Info}) -> {#protocol{}, {ok, tuple()} | {error, _Reason}};
David Reissf4494ee2010-08-30 22:06:03 +0000124 (#protocol{}, tprot_cont_tag()) -> {#protocol{}, {ok, any()} | {error, _Reason}};
David Reiss6c187532010-08-30 22:05:33 +0000125 (#protocol{}, tprot_empty_tag()) -> {#protocol{}, ok | {error, _Reason}};
126 (#protocol{}, tprot_header_tag()) -> {#protocol{}, tprot_header_val() | {error, _Reason}};
David Reissf4494ee2010-08-30 22:06:03 +0000127 (#protocol{}, tprot_data_tag()) -> {#protocol{}, {ok, any()} | {error, _Reason}}.
David Reiss5e6637b2010-08-30 22:05:18 +0000128
David Reiss76f0d112008-06-10 22:57:35 +0000129read(IProto, {struct, {Module, StructureName}}) when is_atom(Module),
130 is_atom(StructureName) ->
David Reiss58a961a2008-06-11 01:13:19 +0000131 read(IProto, Module:struct_info(StructureName), StructureName);
132
133read(IProto, S={struct, Structure}) when is_list(Structure) ->
134 read(IProto, S, undefined);
David Reiss76f0d112008-06-10 22:57:35 +0000135
David Reiss6c187532010-08-30 22:05:33 +0000136read(IProto0, {list, Type}) ->
137 {IProto1, #protocol_list_begin{etype = EType, size = Size}} =
138 read(IProto0, list_begin),
David Reissb4ab0082010-08-30 22:05:58 +0000139 {EType, EType} = {term_to_typeid(Type), EType},
David Reiss6c187532010-08-30 22:05:33 +0000140 {List, IProto2} = lists:mapfoldl(fun(_, ProtoS0) ->
141 {ProtoS1, {ok, Item}} = read(ProtoS0, Type),
142 {Item, ProtoS1}
143 end,
144 IProto1,
145 lists:duplicate(Size, 0)),
146 {IProto3, ok} = read(IProto2, list_end),
147 {IProto3, {ok, List}};
David Reissae756f42008-06-10 22:57:11 +0000148
David Reiss6c187532010-08-30 22:05:33 +0000149read(IProto0, {map, KeyType, ValType}) ->
David Reissb4ab0082010-08-30 22:05:58 +0000150 {IProto1, #protocol_map_begin{size = Size, ktype = KType, vtype = VType}} =
David Reiss6c187532010-08-30 22:05:33 +0000151 read(IProto0, map_begin),
David Reissb4ab0082010-08-30 22:05:58 +0000152 {KType, KType} = {term_to_typeid(KeyType), KType},
153 {VType, VType} = {term_to_typeid(ValType), VType},
David Reiss6c187532010-08-30 22:05:33 +0000154 {List, IProto2} = lists:mapfoldl(fun(_, ProtoS0) ->
155 {ProtoS1, {ok, Key}} = read(ProtoS0, KeyType),
156 {ProtoS2, {ok, Val}} = read(ProtoS1, ValType),
157 {{Key, Val}, ProtoS2}
158 end,
159 IProto1,
160 lists:duplicate(Size, 0)),
161 {IProto3, ok} = read(IProto2, map_end),
162 {IProto3, {ok, dict:from_list(List)}};
David Reissae756f42008-06-10 22:57:11 +0000163
David Reiss6c187532010-08-30 22:05:33 +0000164read(IProto0, {set, Type}) ->
165 {IProto1, #protocol_set_begin{etype = EType, size = Size}} =
166 read(IProto0, set_begin),
David Reissb4ab0082010-08-30 22:05:58 +0000167 {EType, EType} = {term_to_typeid(Type), EType},
David Reiss6c187532010-08-30 22:05:33 +0000168 {List, IProto2} = lists:mapfoldl(fun(_, ProtoS0) ->
169 {ProtoS1, {ok, Item}} = read(ProtoS0, Type),
170 {Item, ProtoS1}
171 end,
172 IProto1,
173 lists:duplicate(Size, 0)),
174 {IProto3, ok} = read(IProto2, set_end),
175 {IProto3, {ok, sets:from_list(List)}};
David Reissae756f42008-06-10 22:57:11 +0000176
David Reiss480d5ab2010-08-30 22:05:26 +0000177read(Protocol, ProtocolType) ->
178 read_specific(Protocol, ProtocolType).
179
David Reisscf7f3972010-08-30 22:05:55 +0000180%% NOTE: Keep this in sync with thrift_protocol_behaviour:read
David Reiss480d5ab2010-08-30 22:05:26 +0000181-spec read_specific
David Reiss6c187532010-08-30 22:05:33 +0000182 (#protocol{}, tprot_empty_tag()) -> {#protocol{}, ok | {error, _Reason}};
183 (#protocol{}, tprot_header_tag()) -> {#protocol{}, tprot_header_val() | {error, _Reason}};
David Reissf4494ee2010-08-30 22:06:03 +0000184 (#protocol{}, tprot_data_tag()) -> {#protocol{}, {ok, any()} | {error, _Reason}}.
David Reiss6c187532010-08-30 22:05:33 +0000185read_specific(Proto = #protocol{module = Module,
186 data = ModuleData}, ProtocolType) ->
187 {NewData, Result} = Module:read(ModuleData, ProtocolType),
188 {Proto#protocol{data = NewData}, Result}.
David Reissac549552008-06-10 22:56:59 +0000189
David Reiss6c187532010-08-30 22:05:33 +0000190read_struct_loop(IProto0, SDict, RTuple) ->
David Reiss5ed313d2010-08-30 22:05:57 +0000191 {IProto1, #protocol_field_begin{type = FType, id = Fid}} =
David Reiss6c187532010-08-30 22:05:33 +0000192 thrift_protocol:read(IProto0, field_begin),
David Reissae756f42008-06-10 22:57:11 +0000193 case {FType, Fid} of
194 {?tType_STOP, _} ->
David Reiss6c187532010-08-30 22:05:33 +0000195 {IProto1, RTuple};
David Reissae756f42008-06-10 22:57:11 +0000196 _Else ->
197 case dict:find(Fid, SDict) of
198 {ok, {Type, Index}} ->
David Reiss233ace52009-03-30 20:46:47 +0000199 case term_to_typeid(Type) of
200 FType ->
David Reiss6c187532010-08-30 22:05:33 +0000201 {IProto2, {ok, Val}} = read(IProto1, Type),
202 {IProto3, ok} = thrift_protocol:read(IProto2, field_end),
David Reiss233ace52009-03-30 20:46:47 +0000203 NewRTuple = setelement(Index, RTuple, Val),
David Reiss6c187532010-08-30 22:05:33 +0000204 read_struct_loop(IProto3, SDict, NewRTuple);
David Reiss233ace52009-03-30 20:46:47 +0000205 Expected ->
206 error_logger:info_msg(
207 "Skipping field ~p with wrong type (~p != ~p)~n",
208 [Fid, FType, Expected]),
David Reiss6c187532010-08-30 22:05:33 +0000209 skip_field(FType, IProto1, SDict, RTuple)
David Reiss233ace52009-03-30 20:46:47 +0000210 end;
David Reissae756f42008-06-10 22:57:11 +0000211 _Else2 ->
David Reiss6c187532010-08-30 22:05:33 +0000212 skip_field(FType, IProto1, SDict, RTuple)
David Reissae756f42008-06-10 22:57:11 +0000213 end
214 end.
David Reissac549552008-06-10 22:56:59 +0000215
David Reiss6c187532010-08-30 22:05:33 +0000216skip_field(FType, IProto0, SDict, RTuple) ->
David Reiss233ace52009-03-30 20:46:47 +0000217 FTypeAtom = thrift_protocol:typeid_to_atom(FType),
David Reiss6c187532010-08-30 22:05:33 +0000218 {IProto1, ok} = thrift_protocol:skip(IProto0, FTypeAtom),
219 {IProto2, ok} = read(IProto1, field_end),
220 read_struct_loop(IProto2, SDict, RTuple).
David Reiss233ace52009-03-30 20:46:47 +0000221
David Reissf4494ee2010-08-30 22:06:03 +0000222-spec skip(#protocol{}, any()) -> {#protocol{}, ok}.
David Reissac549552008-06-10 22:56:59 +0000223
David Reiss6c187532010-08-30 22:05:33 +0000224skip(Proto0, struct) ->
225 {Proto1, ok} = read(Proto0, struct_begin),
226 {Proto2, ok} = skip_struct_loop(Proto1),
227 {Proto3, ok} = read(Proto2, struct_end),
228 {Proto3, ok};
David Reissf32d0fb2010-08-30 22:05:00 +0000229
David Reiss6c187532010-08-30 22:05:33 +0000230skip(Proto0, map) ->
231 {Proto1, Map} = read(Proto0, map_begin),
232 {Proto2, ok} = skip_map_loop(Proto1, Map),
233 {Proto3, ok} = read(Proto2, map_end),
234 {Proto3, ok};
David Reissf32d0fb2010-08-30 22:05:00 +0000235
David Reiss6c187532010-08-30 22:05:33 +0000236skip(Proto0, set) ->
237 {Proto1, Set} = read(Proto0, set_begin),
238 {Proto2, ok} = skip_set_loop(Proto1, Set),
239 {Proto3, ok} = read(Proto2, set_end),
240 {Proto3, ok};
David Reissf32d0fb2010-08-30 22:05:00 +0000241
David Reiss6c187532010-08-30 22:05:33 +0000242skip(Proto0, list) ->
243 {Proto1, List} = read(Proto0, list_begin),
244 {Proto2, ok} = skip_list_loop(Proto1, List),
245 {Proto3, ok} = read(Proto2, list_end),
246 {Proto3, ok};
David Reissf32d0fb2010-08-30 22:05:00 +0000247
David Reiss6c187532010-08-30 22:05:33 +0000248skip(Proto0, Type) when is_atom(Type) ->
249 {Proto1, _Ignore} = read(Proto0, Type),
250 {Proto1, ok}.
David Reissf32d0fb2010-08-30 22:05:00 +0000251
252
David Reiss6c187532010-08-30 22:05:33 +0000253skip_struct_loop(Proto0) ->
254 {Proto1, #protocol_field_begin{type = Type}} = read(Proto0, field_begin),
David Reissac549552008-06-10 22:56:59 +0000255 case Type of
256 ?tType_STOP ->
David Reiss6c187532010-08-30 22:05:33 +0000257 {Proto1, ok};
David Reissac549552008-06-10 22:56:59 +0000258 _Else ->
David Reiss6c187532010-08-30 22:05:33 +0000259 {Proto2, ok} = skip(Proto1, Type),
260 {Proto3, ok} = read(Proto2, field_end),
261 skip_struct_loop(Proto3)
David Reissac549552008-06-10 22:56:59 +0000262 end.
263
David Reiss6c187532010-08-30 22:05:33 +0000264skip_map_loop(Proto0, Map = #protocol_map_begin{ktype = Ktype,
265 vtype = Vtype,
266 size = Size}) ->
David Reissac549552008-06-10 22:56:59 +0000267 case Size of
268 N when N > 0 ->
David Reiss6c187532010-08-30 22:05:33 +0000269 {Proto1, ok} = skip(Proto0, Ktype),
270 {Proto2, ok} = skip(Proto1, Vtype),
271 skip_map_loop(Proto2,
David Reissac549552008-06-10 22:56:59 +0000272 Map#protocol_map_begin{size = Size - 1});
David Reiss6c187532010-08-30 22:05:33 +0000273 0 -> {Proto0, ok}
David Reissac549552008-06-10 22:56:59 +0000274 end.
275
David Reiss6c187532010-08-30 22:05:33 +0000276skip_set_loop(Proto0, Map = #protocol_set_begin{etype = Etype,
277 size = Size}) ->
David Reissac549552008-06-10 22:56:59 +0000278 case Size of
279 N when N > 0 ->
David Reiss6c187532010-08-30 22:05:33 +0000280 {Proto1, ok} = skip(Proto0, Etype),
281 skip_set_loop(Proto1,
David Reissac549552008-06-10 22:56:59 +0000282 Map#protocol_set_begin{size = Size - 1});
David Reiss6c187532010-08-30 22:05:33 +0000283 0 -> {Proto0, ok}
David Reissac549552008-06-10 22:56:59 +0000284 end.
285
David Reiss6c187532010-08-30 22:05:33 +0000286skip_list_loop(Proto0, Map = #protocol_list_begin{etype = Etype,
287 size = Size}) ->
David Reissac549552008-06-10 22:56:59 +0000288 case Size of
289 N when N > 0 ->
David Reiss6c187532010-08-30 22:05:33 +0000290 {Proto1, ok} = skip(Proto0, Etype),
291 skip_list_loop(Proto1,
David Reissac549552008-06-10 22:56:59 +0000292 Map#protocol_list_begin{size = Size - 1});
David Reiss6c187532010-08-30 22:05:33 +0000293 0 -> {Proto0, ok}
David Reissac549552008-06-10 22:56:59 +0000294 end.
David Reissae756f42008-06-10 22:57:11 +0000295
296
297%%--------------------------------------------------------------------
298%% Function: write(OProto, {Type, Data}) -> ok
David Reiss6b3e40f2008-06-11 00:59:03 +0000299%%
David Reissae756f42008-06-10 22:57:11 +0000300%% Type = {struct, StructDef} |
301%% {list, Type} |
302%% {map, KeyType, ValType} |
303%% {set, Type} |
304%% BaseType
305%%
306%% Data =
307%% tuple() -- for struct
308%% | list() -- for list
309%% | dictionary() -- for map
310%% | set() -- for set
David Reissf4494ee2010-08-30 22:06:03 +0000311%% | any() -- for base types
David Reissae756f42008-06-10 22:57:11 +0000312%%
David Reiss6b3e40f2008-06-11 00:59:03 +0000313%% Description:
David Reissae756f42008-06-10 22:57:11 +0000314%%--------------------------------------------------------------------
David Reissf4494ee2010-08-30 22:06:03 +0000315-spec write(#protocol{}, any()) -> {#protocol{}, ok | {error, _Reason}}.
David Reiss5e6637b2010-08-30 22:05:18 +0000316
David Reissc4657992010-08-30 22:05:31 +0000317write(Proto0, {{struct, StructDef}, Data})
David Reiss76f0d112008-06-10 22:57:35 +0000318 when is_list(StructDef), is_tuple(Data), length(StructDef) == size(Data) - 1 ->
319
320 [StructName | Elems] = tuple_to_list(Data),
David Reissc4657992010-08-30 22:05:31 +0000321 {Proto1, ok} = write(Proto0, #protocol_struct_begin{name = StructName}),
322 {Proto2, ok} = struct_write_loop(Proto1, StructDef, Elems),
323 {Proto3, ok} = write(Proto2, struct_end),
324 {Proto3, ok};
David Reissae756f42008-06-10 22:57:11 +0000325
David Reiss76f0d112008-06-10 22:57:35 +0000326write(Proto, {{struct, {Module, StructureName}}, Data})
327 when is_atom(Module),
328 is_atom(StructureName),
329 element(1, Data) =:= StructureName ->
David Reiss76f0d112008-06-10 22:57:35 +0000330 write(Proto, {Module:struct_info(StructureName), Data});
331
Anthony F. Molinaroe49a44a2011-06-18 06:04:01 +0000332write(_, {{struct, {Module, StructureName}}, Data})
333 when is_atom(Module),
334 is_atom(StructureName) ->
Anthony F. Molinaro76bd0052011-06-20 22:03:40 +0000335 erlang:error(struct_unmatched, {{provided, element(1, Data)},
Anthony F. Molinaroe49a44a2011-06-18 06:04:01 +0000336 {expected, StructureName}});
337
David Reissc4657992010-08-30 22:05:31 +0000338write(Proto0, {{list, Type}, Data})
David Reissae756f42008-06-10 22:57:11 +0000339 when is_list(Data) ->
David Reissc4657992010-08-30 22:05:31 +0000340 {Proto1, ok} = write(Proto0,
David Reissae756f42008-06-10 22:57:11 +0000341 #protocol_list_begin{
342 etype = term_to_typeid(Type),
343 size = length(Data)
344 }),
David Reissc4657992010-08-30 22:05:31 +0000345 Proto2 = lists:foldl(fun(Elem, ProtoIn) ->
346 {ProtoOut, ok} = write(ProtoIn, {Type, Elem}),
347 ProtoOut
348 end,
349 Proto1,
350 Data),
351 {Proto3, ok} = write(Proto2, list_end),
352 {Proto3, ok};
David Reissae756f42008-06-10 22:57:11 +0000353
David Reissc4657992010-08-30 22:05:31 +0000354write(Proto0, {{map, KeyType, ValType}, Data}) ->
355 {Proto1, ok} = write(Proto0,
356 #protocol_map_begin{
357 ktype = term_to_typeid(KeyType),
358 vtype = term_to_typeid(ValType),
359 size = dict:size(Data)
360 }),
361 Proto2 = dict:fold(fun(KeyData, ValData, ProtoS0) ->
362 {ProtoS1, ok} = write(ProtoS0, {KeyType, KeyData}),
363 {ProtoS2, ok} = write(ProtoS1, {ValType, ValData}),
364 ProtoS2
365 end,
366 Proto1,
367 Data),
368 {Proto3, ok} = write(Proto2, map_end),
369 {Proto3, ok};
David Reissae756f42008-06-10 22:57:11 +0000370
David Reissc4657992010-08-30 22:05:31 +0000371write(Proto0, {{set, Type}, Data}) ->
David Reissae756f42008-06-10 22:57:11 +0000372 true = sets:is_set(Data),
David Reissc4657992010-08-30 22:05:31 +0000373 {Proto1, ok} = write(Proto0,
374 #protocol_set_begin{
375 etype = term_to_typeid(Type),
376 size = sets:size(Data)
377 }),
378 Proto2 = sets:fold(fun(Elem, ProtoIn) ->
379 {ProtoOut, ok} = write(ProtoIn, {Type, Elem}),
380 ProtoOut
381 end,
382 Proto1,
383 Data),
384 {Proto3, ok} = write(Proto2, set_end),
385 {Proto3, ok};
David Reissae756f42008-06-10 22:57:11 +0000386
David Reissc4657992010-08-30 22:05:31 +0000387write(Proto = #protocol{module = Module,
388 data = ModuleData}, Data) ->
389 {NewData, Result} = Module:write(ModuleData, Data),
390 {Proto#protocol{data = NewData}, Result}.
David Reissae756f42008-06-10 22:57:11 +0000391
David Reissc4657992010-08-30 22:05:31 +0000392struct_write_loop(Proto0, [{Fid, Type} | RestStructDef], [Data | RestData]) ->
393 NewProto = case Data of
394 undefined ->
395 Proto0; % null fields are skipped in response
396 _ ->
397 {Proto1, ok} = write(Proto0,
398 #protocol_field_begin{
399 type = term_to_typeid(Type),
400 id = Fid
401 }),
402 {Proto2, ok} = write(Proto1, {Type, Data}),
403 {Proto3, ok} = write(Proto2, field_end),
404 Proto3
405 end,
406 struct_write_loop(NewProto, RestStructDef, RestData);
David Reissae756f42008-06-10 22:57:11 +0000407struct_write_loop(Proto, [], []) ->
David Reissc4657992010-08-30 22:05:31 +0000408 write(Proto, field_stop).