fix: Add thrift encoding for boolean values (#31)

* fix: Add typespecs

* fix: Fix typespecs

* fix: Fix typespecs

* refactor: Fix formattinc

* fix: Add bool encoding
This commit is contained in:
Yaroslav Rogov 2021-08-04 17:30:42 +07:00 committed by GitHub
parent 105005f237
commit 315a251f86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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) ->