David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | %% |
| 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 Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 20 | -module(thrift_protocol). |
| 21 | |
| 22 | -export([new/2, |
| 23 | write/2, |
| 24 | read/2, |
David Reiss | 58a961a | 2008-06-11 01:13:19 +0000 | [diff] [blame] | 25 | read/3, |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 26 | skip/2, |
David Reiss | 90b4083 | 2008-06-10 22:58:52 +0000 | [diff] [blame] | 27 | flush_transport/1, |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 28 | close_transport/1, |
David Reiss | 6b3e40f | 2008-06-11 00:59:03 +0000 | [diff] [blame] | 29 | typeid_to_atom/1 |
| 30 | ]). |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 31 | |
David Reiss | 6b3e40f | 2008-06-11 00:59:03 +0000 | [diff] [blame] | 32 | -export([behaviour_info/1]). |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 33 | |
| 34 | -include("thrift_constants.hrl"). |
| 35 | -include("thrift_protocol.hrl"). |
| 36 | |
| 37 | -record(protocol, {module, data}). |
| 38 | |
| 39 | behaviour_info(callbacks) -> |
| 40 | [ |
| 41 | {read, 2}, |
David Reiss | 90b4083 | 2008-06-10 22:58:52 +0000 | [diff] [blame] | 42 | {write, 2}, |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 43 | {flush_transport, 1}, |
| 44 | {close_transport, 1} |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 45 | ]; |
| 46 | behaviour_info(_Else) -> undefined. |
| 47 | |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 48 | new(Module, Data) when is_atom(Module) -> |
| 49 | {ok, #protocol{module = Module, |
| 50 | data = Data}}. |
| 51 | |
David Reiss | 5e6637b | 2010-08-30 22:05:18 +0000 | [diff] [blame^] | 52 | -spec flush_transport(#protocol{}) -> ok. |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 53 | flush_transport(#protocol{module = Module, |
| 54 | data = Data}) -> |
| 55 | Module:flush_transport(Data). |
David Reiss | 90b4083 | 2008-06-10 22:58:52 +0000 | [diff] [blame] | 56 | |
David Reiss | 5e6637b | 2010-08-30 22:05:18 +0000 | [diff] [blame^] | 57 | -spec close_transport(#protocol{}) -> ok. |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 58 | close_transport(#protocol{module = Module, |
| 59 | data = Data}) -> |
| 60 | Module:close_transport(Data). |
| 61 | |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 62 | typeid_to_atom(?tType_STOP) -> field_stop; |
| 63 | typeid_to_atom(?tType_VOID) -> void; |
| 64 | typeid_to_atom(?tType_BOOL) -> bool; |
| 65 | typeid_to_atom(?tType_BYTE) -> byte; |
| 66 | typeid_to_atom(?tType_DOUBLE) -> double; |
| 67 | typeid_to_atom(?tType_I16) -> i16; |
| 68 | typeid_to_atom(?tType_I32) -> i32; |
| 69 | typeid_to_atom(?tType_I64) -> i64; |
| 70 | typeid_to_atom(?tType_STRING) -> string; |
| 71 | typeid_to_atom(?tType_STRUCT) -> struct; |
| 72 | typeid_to_atom(?tType_MAP) -> map; |
| 73 | typeid_to_atom(?tType_SET) -> set; |
| 74 | typeid_to_atom(?tType_LIST) -> list. |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 75 | |
| 76 | term_to_typeid(void) -> ?tType_VOID; |
| 77 | term_to_typeid(bool) -> ?tType_BOOL; |
| 78 | term_to_typeid(byte) -> ?tType_BYTE; |
| 79 | term_to_typeid(double) -> ?tType_DOUBLE; |
| 80 | term_to_typeid(i16) -> ?tType_I16; |
| 81 | term_to_typeid(i32) -> ?tType_I32; |
| 82 | term_to_typeid(i64) -> ?tType_I64; |
| 83 | term_to_typeid(string) -> ?tType_STRING; |
| 84 | term_to_typeid({struct, _}) -> ?tType_STRUCT; |
| 85 | term_to_typeid({map, _, _}) -> ?tType_MAP; |
| 86 | term_to_typeid({set, _}) -> ?tType_SET; |
| 87 | term_to_typeid({list, _}) -> ?tType_LIST. |
| 88 | |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 89 | %% Structure is like: |
| 90 | %% [{Fid, Type}, ...] |
David Reiss | 5e6637b | 2010-08-30 22:05:18 +0000 | [diff] [blame^] | 91 | -spec read(#protocol{}, {struct, _StructDef}, atom()) -> {ok, tuple()}. |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 92 | read(IProto, {struct, Structure}, Tag) |
David Reiss | 58a961a | 2008-06-11 01:13:19 +0000 | [diff] [blame] | 93 | when is_list(Structure), is_atom(Tag) -> |
| 94 | |
| 95 | % If we want a tagged tuple, we need to offset all the tuple indices |
| 96 | % by 1 to avoid overwriting the tag. |
| 97 | Offset = if Tag =/= undefined -> 1; true -> 0 end, |
David Reiss | eea8298 | 2008-06-10 22:58:21 +0000 | [diff] [blame] | 98 | IndexList = case length(Structure) of |
David Reiss | 58a961a | 2008-06-11 01:13:19 +0000 | [diff] [blame] | 99 | N when N > 0 -> lists:seq(1 + Offset, N + Offset); |
David Reiss | eea8298 | 2008-06-10 22:58:21 +0000 | [diff] [blame] | 100 | _ -> [] |
| 101 | end, |
| 102 | |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 103 | SWithIndices = [{Fid, {Type, Index}} || |
| 104 | {{Fid, Type}, Index} <- |
David Reiss | eea8298 | 2008-06-10 22:58:21 +0000 | [diff] [blame] | 105 | lists:zip(Structure, IndexList)], |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 106 | % Fid -> {Type, Index} |
| 107 | SDict = dict:from_list(SWithIndices), |
| 108 | |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 109 | ok = read(IProto, struct_begin), |
David Reiss | 58a961a | 2008-06-11 01:13:19 +0000 | [diff] [blame] | 110 | RTuple0 = erlang:make_tuple(length(Structure) + Offset, undefined), |
| 111 | RTuple1 = if Tag =/= undefined -> setelement(1, RTuple0, Tag); |
| 112 | true -> RTuple0 |
| 113 | end, |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 114 | |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 115 | RTuple2 = read_struct_loop(IProto, SDict, RTuple1), |
| 116 | {ok, RTuple2}. |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 117 | |
David Reiss | 5e6637b | 2010-08-30 22:05:18 +0000 | [diff] [blame^] | 118 | -spec read(#protocol{}, term()) -> term(). |
| 119 | |
David Reiss | 76f0d11 | 2008-06-10 22:57:35 +0000 | [diff] [blame] | 120 | read(IProto, {struct, {Module, StructureName}}) when is_atom(Module), |
| 121 | is_atom(StructureName) -> |
David Reiss | 58a961a | 2008-06-11 01:13:19 +0000 | [diff] [blame] | 122 | read(IProto, Module:struct_info(StructureName), StructureName); |
| 123 | |
| 124 | read(IProto, S={struct, Structure}) when is_list(Structure) -> |
| 125 | read(IProto, S, undefined); |
David Reiss | 76f0d11 | 2008-06-10 22:57:35 +0000 | [diff] [blame] | 126 | |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 127 | read(IProto, {list, Type}) -> |
| 128 | #protocol_list_begin{etype = EType, size = Size} = |
| 129 | read(IProto, list_begin), |
| 130 | List = [Result || {ok, Result} <- |
| 131 | [read(IProto, Type) || _X <- lists:duplicate(Size, 0)]], |
| 132 | ok = read(IProto, list_end), |
| 133 | {ok, List}; |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 134 | |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 135 | read(IProto, {map, KeyType, ValType}) -> |
| 136 | #protocol_map_begin{size = Size} = |
| 137 | read(IProto, map_begin), |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 138 | |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 139 | List = [{Key, Val} || {{ok, Key}, {ok, Val}} <- |
| 140 | [{read(IProto, KeyType), |
| 141 | read(IProto, ValType)} || _X <- lists:duplicate(Size, 0)]], |
| 142 | ok = read(IProto, map_end), |
| 143 | {ok, dict:from_list(List)}; |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 144 | |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 145 | read(IProto, {set, Type}) -> |
| 146 | #protocol_set_begin{etype = _EType, |
| 147 | size = Size} = |
| 148 | read(IProto, set_begin), |
| 149 | List = [Result || {ok, Result} <- |
| 150 | [read(IProto, Type) || _X <- lists:duplicate(Size, 0)]], |
| 151 | ok = read(IProto, set_end), |
| 152 | {ok, sets:from_list(List)}; |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 153 | |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 154 | read(#protocol{module = Module, |
| 155 | data = ModuleData}, ProtocolType) -> |
| 156 | Module:read(ModuleData, ProtocolType). |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 157 | |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 158 | read_struct_loop(IProto, SDict, RTuple) -> |
| 159 | #protocol_field_begin{type = FType, id = Fid, name = Name} = |
| 160 | thrift_protocol:read(IProto, field_begin), |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 161 | case {FType, Fid} of |
| 162 | {?tType_STOP, _} -> |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 163 | RTuple; |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 164 | _Else -> |
| 165 | case dict:find(Fid, SDict) of |
| 166 | {ok, {Type, Index}} -> |
David Reiss | 233ace5 | 2009-03-30 20:46:47 +0000 | [diff] [blame] | 167 | case term_to_typeid(Type) of |
| 168 | FType -> |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 169 | {ok, Val} = read(IProto, Type), |
| 170 | thrift_protocol:read(IProto, field_end), |
David Reiss | 233ace5 | 2009-03-30 20:46:47 +0000 | [diff] [blame] | 171 | NewRTuple = setelement(Index, RTuple, Val), |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 172 | read_struct_loop(IProto, SDict, NewRTuple); |
David Reiss | 233ace5 | 2009-03-30 20:46:47 +0000 | [diff] [blame] | 173 | Expected -> |
| 174 | error_logger:info_msg( |
| 175 | "Skipping field ~p with wrong type (~p != ~p)~n", |
| 176 | [Fid, FType, Expected]), |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 177 | skip_field(FType, IProto, SDict, RTuple) |
David Reiss | 233ace5 | 2009-03-30 20:46:47 +0000 | [diff] [blame] | 178 | end; |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 179 | _Else2 -> |
David Reiss | 233ace5 | 2009-03-30 20:46:47 +0000 | [diff] [blame] | 180 | error_logger:info_msg("Skipping field ~p with unknown fid~n", [Fid]), |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 181 | skip_field(FType, IProto, SDict, RTuple) |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 182 | end |
| 183 | end. |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 184 | |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 185 | skip_field(FType, IProto, SDict, RTuple) -> |
David Reiss | 233ace5 | 2009-03-30 20:46:47 +0000 | [diff] [blame] | 186 | FTypeAtom = thrift_protocol:typeid_to_atom(FType), |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 187 | thrift_protocol:skip(IProto, FTypeAtom), |
| 188 | read(IProto, field_end), |
| 189 | read_struct_loop(IProto, SDict, RTuple). |
David Reiss | 233ace5 | 2009-03-30 20:46:47 +0000 | [diff] [blame] | 190 | |
David Reiss | 5e6637b | 2010-08-30 22:05:18 +0000 | [diff] [blame^] | 191 | -spec skip(#protocol{}, term()) -> ok. |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 192 | |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 193 | skip(Proto, struct) -> |
| 194 | ok = read(Proto, struct_begin), |
| 195 | ok = skip_struct_loop(Proto), |
| 196 | ok = read(Proto, struct_end); |
| 197 | |
| 198 | skip(Proto, map) -> |
| 199 | Map = read(Proto, map_begin), |
| 200 | ok = skip_map_loop(Proto, Map), |
| 201 | ok = read(Proto, map_end); |
| 202 | |
| 203 | skip(Proto, set) -> |
| 204 | Set = read(Proto, set_begin), |
| 205 | ok = skip_set_loop(Proto, Set), |
| 206 | ok = read(Proto, set_end); |
| 207 | |
| 208 | skip(Proto, list) -> |
| 209 | List = read(Proto, list_begin), |
| 210 | ok = skip_list_loop(Proto, List), |
| 211 | ok = read(Proto, list_end); |
| 212 | |
| 213 | skip(Proto, Type) when is_atom(Type) -> |
| 214 | _Ignore = read(Proto, Type), |
| 215 | ok. |
| 216 | |
| 217 | |
| 218 | skip_struct_loop(Proto) -> |
| 219 | #protocol_field_begin{type = Type} = read(Proto, field_begin), |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 220 | case Type of |
| 221 | ?tType_STOP -> |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 222 | ok; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 223 | _Else -> |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 224 | skip(Proto, Type), |
| 225 | ok = read(Proto, field_end), |
| 226 | skip_struct_loop(Proto) |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 227 | end. |
| 228 | |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 229 | skip_map_loop(Proto, Map = #protocol_map_begin{ktype = Ktype, |
| 230 | vtype = Vtype, |
| 231 | size = Size}) -> |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 232 | case Size of |
| 233 | N when N > 0 -> |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 234 | skip(Proto, Ktype), |
| 235 | skip(Proto, Vtype), |
| 236 | skip_map_loop(Proto, |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 237 | Map#protocol_map_begin{size = Size - 1}); |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 238 | 0 -> ok |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 239 | end. |
| 240 | |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 241 | skip_set_loop(Proto, Map = #protocol_set_begin{etype = Etype, |
| 242 | size = Size}) -> |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 243 | case Size of |
| 244 | N when N > 0 -> |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 245 | skip(Proto, Etype), |
| 246 | skip_set_loop(Proto, |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 247 | Map#protocol_set_begin{size = Size - 1}); |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 248 | 0 -> ok |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 249 | end. |
| 250 | |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 251 | skip_list_loop(Proto, Map = #protocol_list_begin{etype = Etype, |
| 252 | size = Size}) -> |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 253 | case Size of |
| 254 | N when N > 0 -> |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 255 | skip(Proto, Etype), |
| 256 | skip_list_loop(Proto, |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 257 | Map#protocol_list_begin{size = Size - 1}); |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 258 | 0 -> ok |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 259 | end. |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 260 | |
| 261 | |
| 262 | %%-------------------------------------------------------------------- |
| 263 | %% Function: write(OProto, {Type, Data}) -> ok |
David Reiss | 6b3e40f | 2008-06-11 00:59:03 +0000 | [diff] [blame] | 264 | %% |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 265 | %% Type = {struct, StructDef} | |
| 266 | %% {list, Type} | |
| 267 | %% {map, KeyType, ValType} | |
| 268 | %% {set, Type} | |
| 269 | %% BaseType |
| 270 | %% |
| 271 | %% Data = |
| 272 | %% tuple() -- for struct |
| 273 | %% | list() -- for list |
| 274 | %% | dictionary() -- for map |
| 275 | %% | set() -- for set |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 276 | %% | term() -- for base types |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 277 | %% |
David Reiss | 6b3e40f | 2008-06-11 00:59:03 +0000 | [diff] [blame] | 278 | %% Description: |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 279 | %%-------------------------------------------------------------------- |
David Reiss | 5e6637b | 2010-08-30 22:05:18 +0000 | [diff] [blame^] | 280 | -spec write(#protocol{}, term()) -> ok | {error, _Reason}. |
| 281 | |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 282 | write(Proto, {{struct, StructDef}, Data}) |
David Reiss | 76f0d11 | 2008-06-10 22:57:35 +0000 | [diff] [blame] | 283 | when is_list(StructDef), is_tuple(Data), length(StructDef) == size(Data) - 1 -> |
| 284 | |
| 285 | [StructName | Elems] = tuple_to_list(Data), |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 286 | ok = write(Proto, #protocol_struct_begin{name = StructName}), |
| 287 | ok = struct_write_loop(Proto, StructDef, Elems), |
| 288 | ok = write(Proto, struct_end), |
| 289 | ok; |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 290 | |
David Reiss | 76f0d11 | 2008-06-10 22:57:35 +0000 | [diff] [blame] | 291 | write(Proto, {{struct, {Module, StructureName}}, Data}) |
| 292 | when is_atom(Module), |
| 293 | is_atom(StructureName), |
| 294 | element(1, Data) =:= StructureName -> |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 295 | StructType = Module:struct_info(StructureName), |
David Reiss | 76f0d11 | 2008-06-10 22:57:35 +0000 | [diff] [blame] | 296 | write(Proto, {Module:struct_info(StructureName), Data}); |
| 297 | |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 298 | write(Proto, {{list, Type}, Data}) |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 299 | when is_list(Data) -> |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 300 | ok = write(Proto, |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 301 | #protocol_list_begin{ |
| 302 | etype = term_to_typeid(Type), |
| 303 | size = length(Data) |
| 304 | }), |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 305 | lists:foreach(fun(Elem) -> |
| 306 | ok = write(Proto, {Type, Elem}) |
| 307 | end, |
| 308 | Data), |
| 309 | ok = write(Proto, list_end), |
| 310 | ok; |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 311 | |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 312 | write(Proto, {{map, KeyType, ValType}, Data}) -> |
| 313 | ok = write(Proto, |
| 314 | #protocol_map_begin{ |
| 315 | ktype = term_to_typeid(KeyType), |
| 316 | vtype = term_to_typeid(ValType), |
| 317 | size = dict:size(Data) |
| 318 | }), |
| 319 | dict:fold(fun(KeyData, ValData, _Acc) -> |
| 320 | ok = write(Proto, {KeyType, KeyData}), |
| 321 | ok = write(Proto, {ValType, ValData}) |
| 322 | end, |
| 323 | _AccO = ok, |
| 324 | Data), |
| 325 | ok = write(Proto, map_end), |
| 326 | ok; |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 327 | |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 328 | write(Proto, {{set, Type}, Data}) -> |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 329 | true = sets:is_set(Data), |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 330 | ok = write(Proto, |
| 331 | #protocol_set_begin{ |
| 332 | etype = term_to_typeid(Type), |
| 333 | size = sets:size(Data) |
| 334 | }), |
| 335 | sets:fold(fun(Elem, _Acc) -> |
| 336 | ok = write(Proto, {Type, Elem}) |
| 337 | end, |
| 338 | _Acc0 = ok, |
| 339 | Data), |
| 340 | ok = write(Proto, set_end), |
| 341 | ok; |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 342 | |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 343 | write(#protocol{module = Module, |
| 344 | data = ModuleData}, Data) -> |
| 345 | Module:write(ModuleData, Data). |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 346 | |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 347 | struct_write_loop(Proto, [{Fid, Type} | RestStructDef], [Data | RestData]) -> |
| 348 | case Data of |
| 349 | undefined -> |
| 350 | % null fields are skipped in response |
| 351 | skip; |
| 352 | _ -> |
| 353 | ok = write(Proto, |
| 354 | #protocol_field_begin{ |
| 355 | type = term_to_typeid(Type), |
| 356 | id = Fid |
| 357 | }), |
| 358 | ok = write(Proto, {Type, Data}), |
| 359 | ok = write(Proto, field_end) |
| 360 | end, |
| 361 | struct_write_loop(Proto, RestStructDef, RestData); |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 362 | struct_write_loop(Proto, [], []) -> |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 363 | ok = write(Proto, field_stop), |
| 364 | ok. |