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