Skip to content

Commit

Permalink
Allow to bypass encoding and send pre-encoded payload
Browse files Browse the repository at this point in the history
  • Loading branch information
andy128k committed Sep 11, 2024
1 parent c921325 commit 0343b97
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/ox_thrift_protocol.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ encode_message (ServiceModule, Function, MessageType, SeqId, Args) ->
].


encode (_, {ox_thrift_pre_encoded, ?THRIFT_PROTOCOL, Binary}, Acc) ->
%% Collect pre-encoded binary as-is
[ Binary | Acc ];

encode (_, {ox_thrift_pre_encoded, Protocol, _}, _) ->
throw(lists:flatten(io_lib:format("Protocol mismatch in a pre-encoded value. '~w' is expected while '~w' was given.", [?THRIFT_PROTOCOL, Protocol])));

encode ({struct, StructDef}, Data, Acc)
when is_list(StructDef), is_tuple(Data), length(StructDef) == size(Data) - 1 ->
%% Encode a record from a struct definition.
Expand Down
5 changes: 5 additions & 0 deletions test/ox_thrift_binary_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@

-define(PROTOCOL, ox_thrift_protocol_binary).
-include("ox_thrift_tests.hrl").

echo (#'AllTypes'{byte_field = 43}) ->
{ox_thrift_pre_encoded, binary, <<2,0,1,1,3,0,2,43,6,0,6,127,14,8,0,5,255,255,255,133,10,0,4,1,35,69,103,138,188,222,240,4,0,3,64,36,64,0,0,0,0,0,11,0,7,0,0,0,5,120,121,122,122,121,15,0,8,8,0,0,0,3,0,0,0,1,0,0,0,2,0,0,0,3,14,0,9,11,0,0,0,3,0,0,0,2,98,98,0,0,0,3,99,99,99,0,0,0,1,97,13,0,10,11,8,0,0,0,3,0,0,0,1,73,0,0,0,1,0,0,0,1,88,0,0,0,10,0,0,0,1,86,0,0,0,5,15,0,11,2,0,0,0,4,1,1,0,1,15,0,12,3,0,0,0,0,15,0,13,4,0,0,0,2,63,240,0,0,0,0,0,0,192,0,0,0,0,0,0,0,15,0,14,11,0,0,0,3,0,0,0,3,111,110,101,0,0,0,3,116,119,111,0,0,0,5,116,104,114,101,101,0>>};
echo (V) ->
V.
7 changes: 7 additions & 0 deletions test/ox_thrift_compact_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@

-define(PROTOCOL, ox_thrift_protocol_compact).
-include("ox_thrift_tests.hrl").

echo (#'AllTypes'{byte_field = 43}) ->
{ox_thrift_pre_encoded, compact,
<<17,19,43,68,156,252,3,5,10,245,1,6,8,224,251,230,171,241,217,162,163,2,7,6,0,0,0,0,0,64,36,64,72,5,120,121,122,122,121,25,53,2,4,6,26,56,2,98,98,3,99,99,99,1,97,27,3,133,1,73,2,1,88,20,1,86,10,25,65,2,2,0,2,25,3,25,39,0,0,0,0,0,0,240,63,0,0,0,0,0,0,0,192,25,56,3,111,110,101,3,116,119,111,5,116,104,114,101,101,0>>
};
echo (V) ->
V.
10 changes: 6 additions & 4 deletions test/ox_thrift_tests.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,12 @@ all_types_test (_TestType, MapModule, NewClientFun, DestroyClientFun) ->
{ok, Client3, Reply3} = ox_thrift_client:call(Client2, echo, [ V3 ]),
?assertEqual(#'AllTypes'{string_field = <<"string">>}, Reply3),

DestroyClientFun(Client3).
%% Pre-encoded response
V4 = V1#'AllTypes'{byte_field = 43},
{ok, Client4, Reply4} = ox_thrift_client:call(Client3, echo, [ V4 ]),
?assertEqual(V4, Reply4),

DestroyClientFun(Client4).


is_open (Client) ->
Expand Down Expand Up @@ -491,9 +496,6 @@ sum_ints (#'Container'{first_field=FirstInt, second_struct=SecondStruct, third_f
end,
FirstInt + SecondSum + ThirdInt + SecondArg.

echo (V) ->
V.

simple_exception () ->
#'SimpleException'{message = <<"hello">>, line_number = 201}.

Expand Down

0 comments on commit 0343b97

Please sign in to comment.