From 315a251f8636d36f9672aea05cbadf537de0d7e3 Mon Sep 17 00:00:00 2001 From: Yaroslav Rogov Date: Wed, 4 Aug 2021 17:30:42 +0700 Subject: [PATCH] fix: Add thrift encoding for boolean values (#31) * fix: Add typespecs * fix: Fix typespecs * fix: Fix typespecs * refactor: Fix formattinc * fix: Add bool encoding --- src/bouncer_thrift.erl | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/bouncer_thrift.erl b/src/bouncer_thrift.erl index 22c14ba..17f05a8 100644 --- a/src/bouncer_thrift.erl +++ b/src/bouncer_thrift.erl @@ -25,6 +25,11 @@ | {set, field_type()} | {map, field_type(), field_type()}. +-type field_thrift_value(_Type) :: term(). +-type field_native_value(_Type) :: term(). + +-type name_mapping_fun() :: fun((term()) -> binary()). + -type struct_field_info() :: {field_num(), field_req(), field_type(), field_name(), any()}. @@ -63,6 +68,8 @@ to_thrift_struct([], MapLeft, _Idx, Acc, _NameFun) -> error({excess_data, MapLeft}) end. +-spec to_thrift_value(Type :: field_type(), field_native_value(Type), name_mapping_fun()) -> + field_thrift_value(Type). to_thrift_value({struct, struct, {Mod, Name}}, V = #{}, NameFun) -> {struct, _, StructDef} = Mod:struct_info(Name), Acc = erlang:make_tuple(length(StructDef) + 1, undefined, [{1, Mod:record_name(Name)}]), @@ -78,6 +85,8 @@ to_thrift_value(i32, V, _NameFun) -> to_thrift_value(i16, V, _NameFun) -> V; to_thrift_value(byte, V, _NameFun) -> + V; +to_thrift_value(bool, V, _NameFun) -> V. from_thrift_struct(StructDef, Struct) -> @@ -98,6 +107,7 @@ from_thrift_struct([{_, _Req, Type, Name, _Default} | Rest], Struct, Idx, Acc) - from_thrift_struct([], _Struct, _, Acc) -> Acc. +-spec from_thrift_value(Type :: field_type(), field_thrift_value(Type)) -> field_native_value(Type). from_thrift_value({struct, struct, {Mod, Name}}, V) -> {struct, _, StructDef} = Mod:struct_info(Name), from_thrift_struct(StructDef, V); @@ -112,6 +122,8 @@ from_thrift_value(i32, V) -> from_thrift_value(i16, V) -> V; from_thrift_value(byte, V) -> + V; +from_thrift_value(bool, V) -> V. identity(V) ->