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