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; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 66 | typeid_to_atom(?tType_DOUBLE) -> double; |
Jens Geyer | 40c28d3 | 2015-10-20 23:13:02 +0200 | [diff] [blame] | 67 | typeid_to_atom(?tType_I8) -> byte; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 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; |
Jens Geyer | 40c28d3 | 2015-10-20 23:13:02 +0200 | [diff] [blame] | 79 | term_to_typeid(byte) -> ?tType_I8; |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 80 | term_to_typeid(double) -> ?tType_DOUBLE; |
Jens Geyer | 40c28d3 | 2015-10-20 23:13:02 +0200 | [diff] [blame] | 81 | term_to_typeid(i8) -> ?tType_I8; |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 82 | term_to_typeid(i16) -> ?tType_I16; |
| 83 | term_to_typeid(i32) -> ?tType_I32; |
| 84 | term_to_typeid(i64) -> ?tType_I64; |
| 85 | term_to_typeid(string) -> ?tType_STRING; |
| 86 | term_to_typeid({struct, _}) -> ?tType_STRUCT; |
| 87 | term_to_typeid({map, _, _}) -> ?tType_MAP; |
| 88 | term_to_typeid({set, _}) -> ?tType_SET; |
| 89 | term_to_typeid({list, _}) -> ?tType_LIST. |
| 90 | |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 91 | %% Structure is like: |
| 92 | %% [{Fid, Type}, ...] |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 93 | -spec read(#protocol{}, {struct, _StructDef}, atom()) -> {#protocol{}, {ok, tuple()}}. |
| 94 | read(IProto0, {struct, Structure}, Tag) |
David Reiss | 58a961a | 2008-06-11 01:13:19 +0000 | [diff] [blame] | 95 | 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 Reiss | eea8298 | 2008-06-10 22:58:21 +0000 | [diff] [blame] | 100 | IndexList = case length(Structure) of |
David Reiss | 58a961a | 2008-06-11 01:13:19 +0000 | [diff] [blame] | 101 | N when N > 0 -> lists:seq(1 + Offset, N + Offset); |
David Reiss | eea8298 | 2008-06-10 22:58:21 +0000 | [diff] [blame] | 102 | _ -> [] |
| 103 | end, |
| 104 | |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 105 | SWithIndices = [{Fid, {Type, Index}} || |
| 106 | {{Fid, Type}, Index} <- |
David Reiss | eea8298 | 2008-06-10 22:58:21 +0000 | [diff] [blame] | 107 | lists:zip(Structure, IndexList)], |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 108 | % Fid -> {Type, Index} |
| 109 | SDict = dict:from_list(SWithIndices), |
| 110 | |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 111 | {IProto1, ok} = read(IProto0, struct_begin), |
David Reiss | 58a961a | 2008-06-11 01:13:19 +0000 | [diff] [blame] | 112 | RTuple0 = erlang:make_tuple(length(Structure) + Offset, undefined), |
| 113 | RTuple1 = if Tag =/= undefined -> setelement(1, RTuple0, Tag); |
| 114 | true -> RTuple0 |
| 115 | end, |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 116 | |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 117 | {IProto2, RTuple2} = read_struct_loop(IProto1, SDict, RTuple1), |
| 118 | {IProto2, {ok, RTuple2}}. |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 119 | |
David Reiss | 1cb979b | 2010-08-30 22:05:25 +0000 | [diff] [blame] | 120 | |
David Reiss | cf7f397 | 2010-08-30 22:05:55 +0000 | [diff] [blame] | 121 | %% NOTE: Keep this in sync with thrift_protocol_behaviour:read |
David Reiss | 1cb979b | 2010-08-30 22:05:25 +0000 | [diff] [blame] | 122 | -spec read |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 123 | (#protocol{}, {struct, _Info}) -> {#protocol{}, {ok, tuple()} | {error, _Reason}}; |
David Reiss | f4494ee | 2010-08-30 22:06:03 +0000 | [diff] [blame] | 124 | (#protocol{}, tprot_cont_tag()) -> {#protocol{}, {ok, any()} | {error, _Reason}}; |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 125 | (#protocol{}, tprot_empty_tag()) -> {#protocol{}, ok | {error, _Reason}}; |
| 126 | (#protocol{}, tprot_header_tag()) -> {#protocol{}, tprot_header_val() | {error, _Reason}}; |
David Reiss | f4494ee | 2010-08-30 22:06:03 +0000 | [diff] [blame] | 127 | (#protocol{}, tprot_data_tag()) -> {#protocol{}, {ok, any()} | {error, _Reason}}. |
David Reiss | 5e6637b | 2010-08-30 22:05:18 +0000 | [diff] [blame] | 128 | |
David Reiss | 76f0d11 | 2008-06-10 22:57:35 +0000 | [diff] [blame] | 129 | read(IProto, {struct, {Module, StructureName}}) when is_atom(Module), |
| 130 | is_atom(StructureName) -> |
David Reiss | 58a961a | 2008-06-11 01:13:19 +0000 | [diff] [blame] | 131 | read(IProto, Module:struct_info(StructureName), StructureName); |
| 132 | |
| 133 | read(IProto, S={struct, Structure}) when is_list(Structure) -> |
| 134 | read(IProto, S, undefined); |
David Reiss | 76f0d11 | 2008-06-10 22:57:35 +0000 | [diff] [blame] | 135 | |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 136 | read(IProto0, {list, Type}) -> |
| 137 | {IProto1, #protocol_list_begin{etype = EType, size = Size}} = |
| 138 | read(IProto0, list_begin), |
David Reiss | b4ab008 | 2010-08-30 22:05:58 +0000 | [diff] [blame] | 139 | {EType, EType} = {term_to_typeid(Type), EType}, |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 140 | {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 Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 148 | |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 149 | read(IProto0, {map, KeyType, ValType}) -> |
David Reiss | b4ab008 | 2010-08-30 22:05:58 +0000 | [diff] [blame] | 150 | {IProto1, #protocol_map_begin{size = Size, ktype = KType, vtype = VType}} = |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 151 | read(IProto0, map_begin), |
Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 152 | _ = 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 Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 158 | {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 Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 167 | |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 168 | read(IProto0, {set, Type}) -> |
| 169 | {IProto1, #protocol_set_begin{etype = EType, size = Size}} = |
| 170 | read(IProto0, set_begin), |
David Reiss | b4ab008 | 2010-08-30 22:05:58 +0000 | [diff] [blame] | 171 | {EType, EType} = {term_to_typeid(Type), EType}, |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 172 | {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 Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 180 | |
David Reiss | 480d5ab | 2010-08-30 22:05:26 +0000 | [diff] [blame] | 181 | read(Protocol, ProtocolType) -> |
| 182 | read_specific(Protocol, ProtocolType). |
| 183 | |
David Reiss | cf7f397 | 2010-08-30 22:05:55 +0000 | [diff] [blame] | 184 | %% NOTE: Keep this in sync with thrift_protocol_behaviour:read |
David Reiss | 480d5ab | 2010-08-30 22:05:26 +0000 | [diff] [blame] | 185 | -spec read_specific |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 186 | (#protocol{}, tprot_empty_tag()) -> {#protocol{}, ok | {error, _Reason}}; |
| 187 | (#protocol{}, tprot_header_tag()) -> {#protocol{}, tprot_header_val() | {error, _Reason}}; |
David Reiss | f4494ee | 2010-08-30 22:06:03 +0000 | [diff] [blame] | 188 | (#protocol{}, tprot_data_tag()) -> {#protocol{}, {ok, any()} | {error, _Reason}}. |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 189 | read_specific(Proto = #protocol{module = Module, |
| 190 | data = ModuleData}, ProtocolType) -> |
| 191 | {NewData, Result} = Module:read(ModuleData, ProtocolType), |
| 192 | {Proto#protocol{data = NewData}, Result}. |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 193 | |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 194 | read_struct_loop(IProto0, SDict, RTuple) -> |
David Reiss | 5ed313d | 2010-08-30 22:05:57 +0000 | [diff] [blame] | 195 | {IProto1, #protocol_field_begin{type = FType, id = Fid}} = |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 196 | thrift_protocol:read(IProto0, field_begin), |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 197 | case {FType, Fid} of |
| 198 | {?tType_STOP, _} -> |
Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 199 | {IProto2, ok} = read(IProto1, struct_end), |
| 200 | {IProto2, RTuple}; |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 201 | _Else -> |
| 202 | case dict:find(Fid, SDict) of |
| 203 | {ok, {Type, Index}} -> |
David Reiss | 233ace5 | 2009-03-30 20:46:47 +0000 | [diff] [blame] | 204 | case term_to_typeid(Type) of |
| 205 | FType -> |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 206 | {IProto2, {ok, Val}} = read(IProto1, Type), |
| 207 | {IProto3, ok} = thrift_protocol:read(IProto2, field_end), |
David Reiss | 233ace5 | 2009-03-30 20:46:47 +0000 | [diff] [blame] | 208 | NewRTuple = setelement(Index, RTuple, Val), |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 209 | read_struct_loop(IProto3, SDict, NewRTuple); |
David Reiss | 233ace5 | 2009-03-30 20:46:47 +0000 | [diff] [blame] | 210 | Expected -> |
| 211 | error_logger:info_msg( |
| 212 | "Skipping field ~p with wrong type (~p != ~p)~n", |
| 213 | [Fid, FType, Expected]), |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 214 | skip_field(FType, IProto1, SDict, RTuple) |
David Reiss | 233ace5 | 2009-03-30 20:46:47 +0000 | [diff] [blame] | 215 | end; |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 216 | _Else2 -> |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 217 | skip_field(FType, IProto1, SDict, RTuple) |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 218 | end |
| 219 | end. |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 220 | |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 221 | skip_field(FType, IProto0, SDict, RTuple) -> |
David Reiss | 233ace5 | 2009-03-30 20:46:47 +0000 | [diff] [blame] | 222 | FTypeAtom = thrift_protocol:typeid_to_atom(FType), |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 223 | {IProto1, ok} = thrift_protocol:skip(IProto0, FTypeAtom), |
| 224 | {IProto2, ok} = read(IProto1, field_end), |
| 225 | read_struct_loop(IProto2, SDict, RTuple). |
David Reiss | 233ace5 | 2009-03-30 20:46:47 +0000 | [diff] [blame] | 226 | |
David Reiss | f4494ee | 2010-08-30 22:06:03 +0000 | [diff] [blame] | 227 | -spec skip(#protocol{}, any()) -> {#protocol{}, ok}. |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 228 | |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 229 | skip(Proto0, struct) -> |
| 230 | {Proto1, ok} = read(Proto0, struct_begin), |
| 231 | {Proto2, ok} = skip_struct_loop(Proto1), |
| 232 | {Proto3, ok} = read(Proto2, struct_end), |
| 233 | {Proto3, ok}; |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 234 | |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 235 | skip(Proto0, map) -> |
| 236 | {Proto1, Map} = read(Proto0, map_begin), |
| 237 | {Proto2, ok} = skip_map_loop(Proto1, Map), |
| 238 | {Proto3, ok} = read(Proto2, map_end), |
| 239 | {Proto3, ok}; |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 240 | |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 241 | skip(Proto0, set) -> |
| 242 | {Proto1, Set} = read(Proto0, set_begin), |
| 243 | {Proto2, ok} = skip_set_loop(Proto1, Set), |
| 244 | {Proto3, ok} = read(Proto2, set_end), |
| 245 | {Proto3, ok}; |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 246 | |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 247 | skip(Proto0, list) -> |
| 248 | {Proto1, List} = read(Proto0, list_begin), |
| 249 | {Proto2, ok} = skip_list_loop(Proto1, List), |
| 250 | {Proto3, ok} = read(Proto2, list_end), |
| 251 | {Proto3, ok}; |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 252 | |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 253 | skip(Proto0, Type) when is_atom(Type) -> |
| 254 | {Proto1, _Ignore} = read(Proto0, Type), |
| 255 | {Proto1, ok}. |
David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 256 | |
| 257 | |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 258 | skip_struct_loop(Proto0) -> |
| 259 | {Proto1, #protocol_field_begin{type = Type}} = read(Proto0, field_begin), |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 260 | case Type of |
| 261 | ?tType_STOP -> |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 262 | {Proto1, ok}; |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 263 | _Else -> |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 264 | {Proto2, ok} = skip(Proto1, Type), |
| 265 | {Proto3, ok} = read(Proto2, field_end), |
| 266 | skip_struct_loop(Proto3) |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 267 | end. |
| 268 | |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 269 | skip_map_loop(Proto0, Map = #protocol_map_begin{ktype = Ktype, |
| 270 | vtype = Vtype, |
| 271 | size = Size}) -> |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 272 | case Size of |
| 273 | N when N > 0 -> |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 274 | {Proto1, ok} = skip(Proto0, Ktype), |
| 275 | {Proto2, ok} = skip(Proto1, Vtype), |
| 276 | skip_map_loop(Proto2, |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 277 | Map#protocol_map_begin{size = Size - 1}); |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 278 | 0 -> {Proto0, ok} |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 279 | end. |
| 280 | |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 281 | skip_set_loop(Proto0, Map = #protocol_set_begin{etype = Etype, |
| 282 | size = Size}) -> |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 283 | case Size of |
| 284 | N when N > 0 -> |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 285 | {Proto1, ok} = skip(Proto0, Etype), |
| 286 | skip_set_loop(Proto1, |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 287 | Map#protocol_set_begin{size = Size - 1}); |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 288 | 0 -> {Proto0, ok} |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 289 | end. |
| 290 | |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 291 | skip_list_loop(Proto0, Map = #protocol_list_begin{etype = Etype, |
| 292 | size = Size}) -> |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 293 | case Size of |
| 294 | N when N > 0 -> |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 295 | {Proto1, ok} = skip(Proto0, Etype), |
| 296 | skip_list_loop(Proto1, |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 297 | Map#protocol_list_begin{size = Size - 1}); |
David Reiss | 6c18753 | 2010-08-30 22:05:33 +0000 | [diff] [blame] | 298 | 0 -> {Proto0, ok} |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 299 | end. |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 300 | |
| 301 | |
| 302 | %%-------------------------------------------------------------------- |
| 303 | %% Function: write(OProto, {Type, Data}) -> ok |
David Reiss | 6b3e40f | 2008-06-11 00:59:03 +0000 | [diff] [blame] | 304 | %% |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 305 | %% Type = {struct, StructDef} | |
| 306 | %% {list, Type} | |
| 307 | %% {map, KeyType, ValType} | |
| 308 | %% {set, Type} | |
| 309 | %% BaseType |
| 310 | %% |
| 311 | %% Data = |
| 312 | %% tuple() -- for struct |
| 313 | %% | list() -- for list |
| 314 | %% | dictionary() -- for map |
| 315 | %% | set() -- for set |
David Reiss | f4494ee | 2010-08-30 22:06:03 +0000 | [diff] [blame] | 316 | %% | any() -- for base types |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 317 | %% |
David Reiss | 6b3e40f | 2008-06-11 00:59:03 +0000 | [diff] [blame] | 318 | %% Description: |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 319 | %%-------------------------------------------------------------------- |
David Reiss | f4494ee | 2010-08-30 22:06:03 +0000 | [diff] [blame] | 320 | -spec write(#protocol{}, any()) -> {#protocol{}, ok | {error, _Reason}}. |
David Reiss | 5e6637b | 2010-08-30 22:05:18 +0000 | [diff] [blame] | 321 | |
David Reiss | c465799 | 2010-08-30 22:05:31 +0000 | [diff] [blame] | 322 | write(Proto0, {{struct, StructDef}, Data}) |
David Reiss | 76f0d11 | 2008-06-10 22:57:35 +0000 | [diff] [blame] | 323 | when is_list(StructDef), is_tuple(Data), length(StructDef) == size(Data) - 1 -> |
| 324 | |
| 325 | [StructName | Elems] = tuple_to_list(Data), |
David Reiss | c465799 | 2010-08-30 22:05:31 +0000 | [diff] [blame] | 326 | {Proto1, ok} = write(Proto0, #protocol_struct_begin{name = StructName}), |
| 327 | {Proto2, ok} = struct_write_loop(Proto1, StructDef, Elems), |
| 328 | {Proto3, ok} = write(Proto2, struct_end), |
| 329 | {Proto3, ok}; |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 330 | |
David Reiss | 76f0d11 | 2008-06-10 22:57:35 +0000 | [diff] [blame] | 331 | write(Proto, {{struct, {Module, StructureName}}, Data}) |
| 332 | when is_atom(Module), |
| 333 | is_atom(StructureName), |
| 334 | element(1, Data) =:= StructureName -> |
David Reiss | 76f0d11 | 2008-06-10 22:57:35 +0000 | [diff] [blame] | 335 | write(Proto, {Module:struct_info(StructureName), Data}); |
| 336 | |
Anthony F. Molinaro | e49a44a | 2011-06-18 06:04:01 +0000 | [diff] [blame] | 337 | write(_, {{struct, {Module, StructureName}}, Data}) |
| 338 | when is_atom(Module), |
| 339 | is_atom(StructureName) -> |
Anthony F. Molinaro | 76bd005 | 2011-06-20 22:03:40 +0000 | [diff] [blame] | 340 | erlang:error(struct_unmatched, {{provided, element(1, Data)}, |
Anthony F. Molinaro | e49a44a | 2011-06-18 06:04:01 +0000 | [diff] [blame] | 341 | {expected, StructureName}}); |
| 342 | |
David Reiss | c465799 | 2010-08-30 22:05:31 +0000 | [diff] [blame] | 343 | write(Proto0, {{list, Type}, Data}) |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 344 | when is_list(Data) -> |
David Reiss | c465799 | 2010-08-30 22:05:31 +0000 | [diff] [blame] | 345 | {Proto1, ok} = write(Proto0, |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 346 | #protocol_list_begin{ |
| 347 | etype = term_to_typeid(Type), |
| 348 | size = length(Data) |
| 349 | }), |
David Reiss | c465799 | 2010-08-30 22:05:31 +0000 | [diff] [blame] | 350 | Proto2 = lists:foldl(fun(Elem, ProtoIn) -> |
| 351 | {ProtoOut, ok} = write(ProtoIn, {Type, Elem}), |
| 352 | ProtoOut |
| 353 | end, |
| 354 | Proto1, |
| 355 | Data), |
| 356 | {Proto3, ok} = write(Proto2, list_end), |
| 357 | {Proto3, ok}; |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 358 | |
David Reiss | c465799 | 2010-08-30 22:05:31 +0000 | [diff] [blame] | 359 | write(Proto0, {{map, KeyType, ValType}, Data}) -> |
| 360 | {Proto1, ok} = write(Proto0, |
| 361 | #protocol_map_begin{ |
| 362 | ktype = term_to_typeid(KeyType), |
| 363 | vtype = term_to_typeid(ValType), |
| 364 | size = dict:size(Data) |
| 365 | }), |
| 366 | Proto2 = dict:fold(fun(KeyData, ValData, ProtoS0) -> |
| 367 | {ProtoS1, ok} = write(ProtoS0, {KeyType, KeyData}), |
| 368 | {ProtoS2, ok} = write(ProtoS1, {ValType, ValData}), |
| 369 | ProtoS2 |
| 370 | end, |
| 371 | Proto1, |
| 372 | Data), |
| 373 | {Proto3, ok} = write(Proto2, map_end), |
| 374 | {Proto3, ok}; |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 375 | |
David Reiss | c465799 | 2010-08-30 22:05:31 +0000 | [diff] [blame] | 376 | write(Proto0, {{set, Type}, Data}) -> |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 377 | true = sets:is_set(Data), |
David Reiss | c465799 | 2010-08-30 22:05:31 +0000 | [diff] [blame] | 378 | {Proto1, ok} = write(Proto0, |
| 379 | #protocol_set_begin{ |
| 380 | etype = term_to_typeid(Type), |
| 381 | size = sets:size(Data) |
| 382 | }), |
| 383 | Proto2 = sets:fold(fun(Elem, ProtoIn) -> |
| 384 | {ProtoOut, ok} = write(ProtoIn, {Type, Elem}), |
| 385 | ProtoOut |
| 386 | end, |
| 387 | Proto1, |
| 388 | Data), |
| 389 | {Proto3, ok} = write(Proto2, set_end), |
| 390 | {Proto3, ok}; |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 391 | |
David Reiss | c465799 | 2010-08-30 22:05:31 +0000 | [diff] [blame] | 392 | write(Proto = #protocol{module = Module, |
| 393 | data = ModuleData}, Data) -> |
| 394 | {NewData, Result} = Module:write(ModuleData, Data), |
| 395 | {Proto#protocol{data = NewData}, Result}. |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 396 | |
David Reiss | c465799 | 2010-08-30 22:05:31 +0000 | [diff] [blame] | 397 | struct_write_loop(Proto0, [{Fid, Type} | RestStructDef], [Data | RestData]) -> |
| 398 | NewProto = case Data of |
| 399 | undefined -> |
| 400 | Proto0; % null fields are skipped in response |
| 401 | _ -> |
| 402 | {Proto1, ok} = write(Proto0, |
| 403 | #protocol_field_begin{ |
| 404 | type = term_to_typeid(Type), |
| 405 | id = Fid |
| 406 | }), |
| 407 | {Proto2, ok} = write(Proto1, {Type, Data}), |
| 408 | {Proto3, ok} = write(Proto2, field_end), |
| 409 | Proto3 |
| 410 | end, |
| 411 | struct_write_loop(NewProto, RestStructDef, RestData); |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 412 | struct_write_loop(Proto, [], []) -> |
David Reiss | c465799 | 2010-08-30 22:05:31 +0000 | [diff] [blame] | 413 | write(Proto, field_stop). |