blob: 193b07af8226e58170da6ed9d1c048fed2217185 [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
David Reisscf7f3972010-08-30 22:05:55 +0000120%% NOTE: Keep this in sync with thrift_protocol_behaviour:read
David Reiss1cb979b2010-08-30 22:05:25 +0000121-spec read
David Reiss6c187532010-08-30 22:05:33 +0000122 (#protocol{}, {struct, _Info}) -> {#protocol{}, {ok, tuple()} | {error, _Reason}};
David Reissf4494ee2010-08-30 22:06:03 +0000123 (#protocol{}, tprot_cont_tag()) -> {#protocol{}, {ok, any()} | {error, _Reason}};
David Reiss6c187532010-08-30 22:05:33 +0000124 (#protocol{}, tprot_empty_tag()) -> {#protocol{}, ok | {error, _Reason}};
125 (#protocol{}, tprot_header_tag()) -> {#protocol{}, tprot_header_val() | {error, _Reason}};
David Reissf4494ee2010-08-30 22:06:03 +0000126 (#protocol{}, tprot_data_tag()) -> {#protocol{}, {ok, any()} | {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),
David Reissb4ab0082010-08-30 22:05:58 +0000138 {EType, EType} = {term_to_typeid(Type), EType},
David Reiss6c187532010-08-30 22:05:33 +0000139 {List, IProto2} = lists:mapfoldl(fun(_, ProtoS0) ->
140 {ProtoS1, {ok, Item}} = read(ProtoS0, Type),
141 {Item, ProtoS1}
142 end,
143 IProto1,
144 lists:duplicate(Size, 0)),
145 {IProto3, ok} = read(IProto2, list_end),
146 {IProto3, {ok, List}};
David Reissae756f42008-06-10 22:57:11 +0000147
David Reiss6c187532010-08-30 22:05:33 +0000148read(IProto0, {map, KeyType, ValType}) ->
David Reissb4ab0082010-08-30 22:05:58 +0000149 {IProto1, #protocol_map_begin{size = Size, ktype = KType, vtype = VType}} =
David Reiss6c187532010-08-30 22:05:33 +0000150 read(IProto0, map_begin),
David Reissb4ab0082010-08-30 22:05:58 +0000151 {KType, KType} = {term_to_typeid(KeyType), KType},
152 {VType, VType} = {term_to_typeid(ValType), VType},
David Reiss6c187532010-08-30 22:05:33 +0000153 {List, IProto2} = lists:mapfoldl(fun(_, ProtoS0) ->
154 {ProtoS1, {ok, Key}} = read(ProtoS0, KeyType),
155 {ProtoS2, {ok, Val}} = read(ProtoS1, ValType),
156 {{Key, Val}, ProtoS2}
157 end,
158 IProto1,
159 lists:duplicate(Size, 0)),
160 {IProto3, ok} = read(IProto2, map_end),
161 {IProto3, {ok, dict:from_list(List)}};
David Reissae756f42008-06-10 22:57:11 +0000162
David Reiss6c187532010-08-30 22:05:33 +0000163read(IProto0, {set, Type}) ->
164 {IProto1, #protocol_set_begin{etype = EType, size = Size}} =
165 read(IProto0, set_begin),
David Reissb4ab0082010-08-30 22:05:58 +0000166 {EType, EType} = {term_to_typeid(Type), EType},
David Reiss6c187532010-08-30 22:05:33 +0000167 {List, IProto2} = lists:mapfoldl(fun(_, ProtoS0) ->
168 {ProtoS1, {ok, Item}} = read(ProtoS0, Type),
169 {Item, ProtoS1}
170 end,
171 IProto1,
172 lists:duplicate(Size, 0)),
173 {IProto3, ok} = read(IProto2, set_end),
174 {IProto3, {ok, sets:from_list(List)}};
David Reissae756f42008-06-10 22:57:11 +0000175
David Reiss480d5ab2010-08-30 22:05:26 +0000176read(Protocol, ProtocolType) ->
177 read_specific(Protocol, ProtocolType).
178
David Reisscf7f3972010-08-30 22:05:55 +0000179%% NOTE: Keep this in sync with thrift_protocol_behaviour:read
David Reiss480d5ab2010-08-30 22:05:26 +0000180-spec read_specific
David Reiss6c187532010-08-30 22:05:33 +0000181 (#protocol{}, tprot_empty_tag()) -> {#protocol{}, ok | {error, _Reason}};
182 (#protocol{}, tprot_header_tag()) -> {#protocol{}, tprot_header_val() | {error, _Reason}};
David Reissf4494ee2010-08-30 22:06:03 +0000183 (#protocol{}, tprot_data_tag()) -> {#protocol{}, {ok, any()} | {error, _Reason}}.
David Reiss6c187532010-08-30 22:05:33 +0000184read_specific(Proto = #protocol{module = Module,
185 data = ModuleData}, ProtocolType) ->
186 {NewData, Result} = Module:read(ModuleData, ProtocolType),
187 {Proto#protocol{data = NewData}, Result}.
David Reissac549552008-06-10 22:56:59 +0000188
David Reiss6c187532010-08-30 22:05:33 +0000189read_struct_loop(IProto0, SDict, RTuple) ->
David Reiss5ed313d2010-08-30 22:05:57 +0000190 {IProto1, #protocol_field_begin{type = FType, id = Fid}} =
David Reiss6c187532010-08-30 22:05:33 +0000191 thrift_protocol:read(IProto0, field_begin),
David Reissae756f42008-06-10 22:57:11 +0000192 case {FType, Fid} of
193 {?tType_STOP, _} ->
David Reiss6c187532010-08-30 22:05:33 +0000194 {IProto1, RTuple};
David Reissae756f42008-06-10 22:57:11 +0000195 _Else ->
196 case dict:find(Fid, SDict) of
197 {ok, {Type, Index}} ->
David Reiss233ace52009-03-30 20:46:47 +0000198 case term_to_typeid(Type) of
199 FType ->
David Reiss6c187532010-08-30 22:05:33 +0000200 {IProto2, {ok, Val}} = read(IProto1, Type),
201 {IProto3, ok} = thrift_protocol:read(IProto2, field_end),
David Reiss233ace52009-03-30 20:46:47 +0000202 NewRTuple = setelement(Index, RTuple, Val),
David Reiss6c187532010-08-30 22:05:33 +0000203 read_struct_loop(IProto3, SDict, NewRTuple);
David Reiss233ace52009-03-30 20:46:47 +0000204 Expected ->
205 error_logger:info_msg(
206 "Skipping field ~p with wrong type (~p != ~p)~n",
207 [Fid, FType, Expected]),
David Reiss6c187532010-08-30 22:05:33 +0000208 skip_field(FType, IProto1, SDict, RTuple)
David Reiss233ace52009-03-30 20:46:47 +0000209 end;
David Reissae756f42008-06-10 22:57:11 +0000210 _Else2 ->
David Reiss233ace52009-03-30 20:46:47 +0000211 error_logger:info_msg("Skipping field ~p with unknown fid~n", [Fid]),
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).