blob: 2fe10d6a25dea08a9d0e5b5f4750fbf9c658ba21 [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),
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +0900152 _ = case Size of
153 0 -> 0;
154 _ ->
155 {KType, KType} = {term_to_typeid(KeyType), KType},
156 {VType, VType} = {term_to_typeid(ValType), VType}
157 end,
David Reiss6c187532010-08-30 22:05:33 +0000158 {List, IProto2} = lists:mapfoldl(fun(_, ProtoS0) ->
159 {ProtoS1, {ok, Key}} = read(ProtoS0, KeyType),
160 {ProtoS2, {ok, Val}} = read(ProtoS1, ValType),
161 {{Key, Val}, ProtoS2}
162 end,
163 IProto1,
164 lists:duplicate(Size, 0)),
165 {IProto3, ok} = read(IProto2, map_end),
166 {IProto3, {ok, dict:from_list(List)}};
David Reissae756f42008-06-10 22:57:11 +0000167
David Reiss6c187532010-08-30 22:05:33 +0000168read(IProto0, {set, Type}) ->
169 {IProto1, #protocol_set_begin{etype = EType, size = Size}} =
170 read(IProto0, set_begin),
David Reissb4ab0082010-08-30 22:05:58 +0000171 {EType, EType} = {term_to_typeid(Type), EType},
David Reiss6c187532010-08-30 22:05:33 +0000172 {List, IProto2} = lists:mapfoldl(fun(_, ProtoS0) ->
173 {ProtoS1, {ok, Item}} = read(ProtoS0, Type),
174 {Item, ProtoS1}
175 end,
176 IProto1,
177 lists:duplicate(Size, 0)),
178 {IProto3, ok} = read(IProto2, set_end),
179 {IProto3, {ok, sets:from_list(List)}};
David Reissae756f42008-06-10 22:57:11 +0000180
David Reiss480d5ab2010-08-30 22:05:26 +0000181read(Protocol, ProtocolType) ->
182 read_specific(Protocol, ProtocolType).
183
David Reisscf7f3972010-08-30 22:05:55 +0000184%% NOTE: Keep this in sync with thrift_protocol_behaviour:read
David Reiss480d5ab2010-08-30 22:05:26 +0000185-spec read_specific
David Reiss6c187532010-08-30 22:05:33 +0000186 (#protocol{}, tprot_empty_tag()) -> {#protocol{}, ok | {error, _Reason}};
187 (#protocol{}, tprot_header_tag()) -> {#protocol{}, tprot_header_val() | {error, _Reason}};
David Reissf4494ee2010-08-30 22:06:03 +0000188 (#protocol{}, tprot_data_tag()) -> {#protocol{}, {ok, any()} | {error, _Reason}}.
David Reiss6c187532010-08-30 22:05:33 +0000189read_specific(Proto = #protocol{module = Module,
190 data = ModuleData}, ProtocolType) ->
191 {NewData, Result} = Module:read(ModuleData, ProtocolType),
192 {Proto#protocol{data = NewData}, Result}.
David Reissac549552008-06-10 22:56:59 +0000193
David Reiss6c187532010-08-30 22:05:33 +0000194read_struct_loop(IProto0, SDict, RTuple) ->
David Reiss5ed313d2010-08-30 22:05:57 +0000195 {IProto1, #protocol_field_begin{type = FType, id = Fid}} =
David Reiss6c187532010-08-30 22:05:33 +0000196 thrift_protocol:read(IProto0, field_begin),
David Reissae756f42008-06-10 22:57:11 +0000197 case {FType, Fid} of
198 {?tType_STOP, _} ->
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +0900199 {IProto2, ok} = read(IProto1, struct_end),
200 {IProto2, RTuple};
David Reissae756f42008-06-10 22:57:11 +0000201 _Else ->
202 case dict:find(Fid, SDict) of
203 {ok, {Type, Index}} ->
David Reiss233ace52009-03-30 20:46:47 +0000204 case term_to_typeid(Type) of
205 FType ->
David Reiss6c187532010-08-30 22:05:33 +0000206 {IProto2, {ok, Val}} = read(IProto1, Type),
207 {IProto3, ok} = thrift_protocol:read(IProto2, field_end),
David Reiss233ace52009-03-30 20:46:47 +0000208 NewRTuple = setelement(Index, RTuple, Val),
David Reiss6c187532010-08-30 22:05:33 +0000209 read_struct_loop(IProto3, SDict, NewRTuple);
David Reiss233ace52009-03-30 20:46:47 +0000210 Expected ->
211 error_logger:info_msg(
212 "Skipping field ~p with wrong type (~p != ~p)~n",
213 [Fid, FType, Expected]),
David Reiss6c187532010-08-30 22:05:33 +0000214 skip_field(FType, IProto1, SDict, RTuple)
David Reiss233ace52009-03-30 20:46:47 +0000215 end;
David Reissae756f42008-06-10 22:57:11 +0000216 _Else2 ->
David Reiss6c187532010-08-30 22:05:33 +0000217 skip_field(FType, IProto1, SDict, RTuple)
David Reissae756f42008-06-10 22:57:11 +0000218 end
219 end.
David Reissac549552008-06-10 22:56:59 +0000220
David Reiss6c187532010-08-30 22:05:33 +0000221skip_field(FType, IProto0, SDict, RTuple) ->
David Hulle544a892017-07-27 02:15:00 +0200222 {IProto1, ok} = skip(IProto0, typeid_to_atom(FType)),
David Reiss6c187532010-08-30 22:05:33 +0000223 {IProto2, ok} = read(IProto1, field_end),
224 read_struct_loop(IProto2, SDict, RTuple).
David Reiss233ace52009-03-30 20:46:47 +0000225
David Hulle544a892017-07-27 02:15:00 +0200226-spec skip(#protocol{}, atom()) -> {#protocol{}, ok}.
David Reissac549552008-06-10 22:56:59 +0000227
David Reiss6c187532010-08-30 22:05:33 +0000228skip(Proto0, struct) ->
229 {Proto1, ok} = read(Proto0, struct_begin),
230 {Proto2, ok} = skip_struct_loop(Proto1),
231 {Proto3, ok} = read(Proto2, struct_end),
232 {Proto3, ok};
David Reissf32d0fb2010-08-30 22:05:00 +0000233
David Reiss6c187532010-08-30 22:05:33 +0000234skip(Proto0, map) ->
235 {Proto1, Map} = read(Proto0, map_begin),
236 {Proto2, ok} = skip_map_loop(Proto1, Map),
237 {Proto3, ok} = read(Proto2, map_end),
238 {Proto3, ok};
David Reissf32d0fb2010-08-30 22:05:00 +0000239
David Reiss6c187532010-08-30 22:05:33 +0000240skip(Proto0, set) ->
241 {Proto1, Set} = read(Proto0, set_begin),
242 {Proto2, ok} = skip_set_loop(Proto1, Set),
243 {Proto3, ok} = read(Proto2, set_end),
244 {Proto3, ok};
David Reissf32d0fb2010-08-30 22:05:00 +0000245
David Reiss6c187532010-08-30 22:05:33 +0000246skip(Proto0, list) ->
247 {Proto1, List} = read(Proto0, list_begin),
248 {Proto2, ok} = skip_list_loop(Proto1, List),
249 {Proto3, ok} = read(Proto2, list_end),
250 {Proto3, ok};
David Reissf32d0fb2010-08-30 22:05:00 +0000251
David Reiss6c187532010-08-30 22:05:33 +0000252skip(Proto0, Type) when is_atom(Type) ->
253 {Proto1, _Ignore} = read(Proto0, Type),
254 {Proto1, ok}.
David Reissf32d0fb2010-08-30 22:05:00 +0000255
256
David Reiss6c187532010-08-30 22:05:33 +0000257skip_struct_loop(Proto0) ->
258 {Proto1, #protocol_field_begin{type = Type}} = read(Proto0, field_begin),
David Reissac549552008-06-10 22:56:59 +0000259 case Type of
260 ?tType_STOP ->
David Reiss6c187532010-08-30 22:05:33 +0000261 {Proto1, ok};
David Reissac549552008-06-10 22:56:59 +0000262 _Else ->
David Hulle544a892017-07-27 02:15:00 +0200263 {Proto2, ok} = skip(Proto1, typeid_to_atom(Type)),
David Reiss6c187532010-08-30 22:05:33 +0000264 {Proto3, ok} = read(Proto2, field_end),
265 skip_struct_loop(Proto3)
David Reissac549552008-06-10 22:56:59 +0000266 end.
267
David Reiss6c187532010-08-30 22:05:33 +0000268skip_map_loop(Proto0, Map = #protocol_map_begin{ktype = Ktype,
269 vtype = Vtype,
270 size = Size}) ->
David Reissac549552008-06-10 22:56:59 +0000271 case Size of
272 N when N > 0 ->
David Hulle544a892017-07-27 02:15:00 +0200273 {Proto1, ok} = skip(Proto0, typeid_to_atom(Ktype)),
274 {Proto2, ok} = skip(Proto1, typeid_to_atom(Vtype)),
David Reiss6c187532010-08-30 22:05:33 +0000275 skip_map_loop(Proto2,
David Reissac549552008-06-10 22:56:59 +0000276 Map#protocol_map_begin{size = Size - 1});
David Reiss6c187532010-08-30 22:05:33 +0000277 0 -> {Proto0, ok}
David Reissac549552008-06-10 22:56:59 +0000278 end.
279
David Reiss6c187532010-08-30 22:05:33 +0000280skip_set_loop(Proto0, Map = #protocol_set_begin{etype = Etype,
281 size = Size}) ->
David Reissac549552008-06-10 22:56:59 +0000282 case Size of
283 N when N > 0 ->
David Hulle544a892017-07-27 02:15:00 +0200284 {Proto1, ok} = skip(Proto0, typeid_to_atom(Etype)),
David Reiss6c187532010-08-30 22:05:33 +0000285 skip_set_loop(Proto1,
David Reissac549552008-06-10 22:56:59 +0000286 Map#protocol_set_begin{size = Size - 1});
David Reiss6c187532010-08-30 22:05:33 +0000287 0 -> {Proto0, ok}
David Reissac549552008-06-10 22:56:59 +0000288 end.
289
David Reiss6c187532010-08-30 22:05:33 +0000290skip_list_loop(Proto0, Map = #protocol_list_begin{etype = Etype,
291 size = Size}) ->
David Reissac549552008-06-10 22:56:59 +0000292 case Size of
293 N when N > 0 ->
David Hulle544a892017-07-27 02:15:00 +0200294 {Proto1, ok} = skip(Proto0, typeid_to_atom(Etype)),
David Reiss6c187532010-08-30 22:05:33 +0000295 skip_list_loop(Proto1,
David Reissac549552008-06-10 22:56:59 +0000296 Map#protocol_list_begin{size = Size - 1});
David Reiss6c187532010-08-30 22:05:33 +0000297 0 -> {Proto0, ok}
David Reissac549552008-06-10 22:56:59 +0000298 end.
David Reissae756f42008-06-10 22:57:11 +0000299
300
301%%--------------------------------------------------------------------
302%% Function: write(OProto, {Type, Data}) -> ok
David Reiss6b3e40f2008-06-11 00:59:03 +0000303%%
David Reissae756f42008-06-10 22:57:11 +0000304%% Type = {struct, StructDef} |
305%% {list, Type} |
306%% {map, KeyType, ValType} |
307%% {set, Type} |
308%% BaseType
309%%
310%% Data =
311%% tuple() -- for struct
312%% | list() -- for list
313%% | dictionary() -- for map
314%% | set() -- for set
David Reissf4494ee2010-08-30 22:06:03 +0000315%% | any() -- for base types
David Reissae756f42008-06-10 22:57:11 +0000316%%
David Reiss6b3e40f2008-06-11 00:59:03 +0000317%% Description:
David Reissae756f42008-06-10 22:57:11 +0000318%%--------------------------------------------------------------------
David Reissf4494ee2010-08-30 22:06:03 +0000319-spec write(#protocol{}, any()) -> {#protocol{}, ok | {error, _Reason}}.
David Reiss5e6637b2010-08-30 22:05:18 +0000320
David Reissc4657992010-08-30 22:05:31 +0000321write(Proto0, {{struct, StructDef}, Data})
David Reiss76f0d112008-06-10 22:57:35 +0000322 when is_list(StructDef), is_tuple(Data), length(StructDef) == size(Data) - 1 ->
323
324 [StructName | Elems] = tuple_to_list(Data),
David Reissc4657992010-08-30 22:05:31 +0000325 {Proto1, ok} = write(Proto0, #protocol_struct_begin{name = StructName}),
326 {Proto2, ok} = struct_write_loop(Proto1, StructDef, Elems),
327 {Proto3, ok} = write(Proto2, struct_end),
328 {Proto3, ok};
David Reissae756f42008-06-10 22:57:11 +0000329
David Reiss76f0d112008-06-10 22:57:35 +0000330write(Proto, {{struct, {Module, StructureName}}, Data})
331 when is_atom(Module),
332 is_atom(StructureName),
333 element(1, Data) =:= StructureName ->
David Reiss76f0d112008-06-10 22:57:35 +0000334 write(Proto, {Module:struct_info(StructureName), Data});
335
Anthony F. Molinaroe49a44a2011-06-18 06:04:01 +0000336write(_, {{struct, {Module, StructureName}}, Data})
337 when is_atom(Module),
338 is_atom(StructureName) ->
Anthony F. Molinaro76bd0052011-06-20 22:03:40 +0000339 erlang:error(struct_unmatched, {{provided, element(1, Data)},
Anthony F. Molinaroe49a44a2011-06-18 06:04:01 +0000340 {expected, StructureName}});
341
David Reissc4657992010-08-30 22:05:31 +0000342write(Proto0, {{list, Type}, Data})
David Reissae756f42008-06-10 22:57:11 +0000343 when is_list(Data) ->
David Reissc4657992010-08-30 22:05:31 +0000344 {Proto1, ok} = write(Proto0,
David Reissae756f42008-06-10 22:57:11 +0000345 #protocol_list_begin{
346 etype = term_to_typeid(Type),
347 size = length(Data)
348 }),
David Reissc4657992010-08-30 22:05:31 +0000349 Proto2 = lists:foldl(fun(Elem, ProtoIn) ->
350 {ProtoOut, ok} = write(ProtoIn, {Type, Elem}),
351 ProtoOut
352 end,
353 Proto1,
354 Data),
355 {Proto3, ok} = write(Proto2, list_end),
356 {Proto3, ok};
David Reissae756f42008-06-10 22:57:11 +0000357
David Reissc4657992010-08-30 22:05:31 +0000358write(Proto0, {{map, KeyType, ValType}, Data}) ->
359 {Proto1, ok} = write(Proto0,
360 #protocol_map_begin{
361 ktype = term_to_typeid(KeyType),
362 vtype = term_to_typeid(ValType),
363 size = dict:size(Data)
364 }),
365 Proto2 = dict:fold(fun(KeyData, ValData, ProtoS0) ->
366 {ProtoS1, ok} = write(ProtoS0, {KeyType, KeyData}),
367 {ProtoS2, ok} = write(ProtoS1, {ValType, ValData}),
368 ProtoS2
369 end,
370 Proto1,
371 Data),
372 {Proto3, ok} = write(Proto2, map_end),
373 {Proto3, ok};
David Reissae756f42008-06-10 22:57:11 +0000374
David Reissc4657992010-08-30 22:05:31 +0000375write(Proto0, {{set, Type}, Data}) ->
David Reissae756f42008-06-10 22:57:11 +0000376 true = sets:is_set(Data),
David Reissc4657992010-08-30 22:05:31 +0000377 {Proto1, ok} = write(Proto0,
378 #protocol_set_begin{
379 etype = term_to_typeid(Type),
380 size = sets:size(Data)
381 }),
382 Proto2 = sets:fold(fun(Elem, ProtoIn) ->
383 {ProtoOut, ok} = write(ProtoIn, {Type, Elem}),
384 ProtoOut
385 end,
386 Proto1,
387 Data),
388 {Proto3, ok} = write(Proto2, set_end),
389 {Proto3, ok};
David Reissae756f42008-06-10 22:57:11 +0000390
David Reissc4657992010-08-30 22:05:31 +0000391write(Proto = #protocol{module = Module,
392 data = ModuleData}, Data) ->
393 {NewData, Result} = Module:write(ModuleData, Data),
394 {Proto#protocol{data = NewData}, Result}.
David Reissae756f42008-06-10 22:57:11 +0000395
David Reissc4657992010-08-30 22:05:31 +0000396struct_write_loop(Proto0, [{Fid, Type} | RestStructDef], [Data | RestData]) ->
397 NewProto = case Data of
398 undefined ->
399 Proto0; % null fields are skipped in response
400 _ ->
401 {Proto1, ok} = write(Proto0,
402 #protocol_field_begin{
403 type = term_to_typeid(Type),
404 id = Fid
405 }),
406 {Proto2, ok} = write(Proto1, {Type, Data}),
407 {Proto3, ok} = write(Proto2, field_end),
408 Proto3
409 end,
410 struct_write_loop(NewProto, RestStructDef, RestData);
David Reissae756f42008-06-10 22:57:11 +0000411struct_write_loop(Proto, [], []) ->
David Reissc4657992010-08-30 22:05:31 +0000412 write(Proto, field_stop).