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