From 0343b9785fa68cf8c7325715f784792d41a4bba7 Mon Sep 17 00:00:00 2001 From: Andrii Kuteiko Date: Fri, 6 Sep 2024 15:11:55 +0200 Subject: [PATCH] Allow to bypass encoding and send pre-encoded payload --- src/ox_thrift_protocol.hrl | 7 +++++++ test/ox_thrift_binary_tests.erl | 5 +++++ test/ox_thrift_compact_tests.erl | 7 +++++++ test/ox_thrift_tests.hrl | 10 ++++++---- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/ox_thrift_protocol.hrl b/src/ox_thrift_protocol.hrl index a712dc3..2ac0eae 100644 --- a/src/ox_thrift_protocol.hrl +++ b/src/ox_thrift_protocol.hrl @@ -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. diff --git a/test/ox_thrift_binary_tests.erl b/test/ox_thrift_binary_tests.erl index 58714db..ecfa0ca 100644 --- a/test/ox_thrift_binary_tests.erl +++ b/test/ox_thrift_binary_tests.erl @@ -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. diff --git a/test/ox_thrift_compact_tests.erl b/test/ox_thrift_compact_tests.erl index ea4b5dc..56f27f5 100644 --- a/test/ox_thrift_compact_tests.erl +++ b/test/ox_thrift_compact_tests.erl @@ -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. diff --git a/test/ox_thrift_tests.hrl b/test/ox_thrift_tests.hrl index 21ea767..052bcbb 100644 --- a/test/ox_thrift_tests.hrl +++ b/test/ox_thrift_tests.hrl @@ -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) -> @@ -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}.