Fix generic thrift→json encoding (#13)

Tags (which are integer field designators in thrift serializations)
were wrongly interpreted as indices in Erlang record tuples.

* Update for recent rebar3_lint w/ elvis_core 1.0.0
This commit is contained in:
Andrew Mayorov 2020-12-28 17:59:17 +03:00 committed by GitHub
parent 2445f446d6
commit 2850275de0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 13 deletions

View File

@ -122,6 +122,8 @@
{elvis_style, used_ignored_variable, disable},
% Tests are usually more comprehensible when a bit more verbose.
{elvis_style, dont_repeat_yourself, #{min_complexity => 20}},
% Too opionated
{elvis_style, state_record_and_type, disable},
{elvis_style, god_modules, #{ignore => [ct_gun_event_h]}}
]
},
@ -134,18 +136,18 @@
dirs => ["."],
filter => "rebar.config",
rules => [
{elvis_style, line_length, #{limit => 100, skip_comments => false}},
{elvis_style, no_tabs},
{elvis_style, no_trailing_whitespace}
{elvis_text_style, line_length, #{limit => 100, skip_comments => false}},
{elvis_text_style, no_tabs},
{elvis_text_style, no_trailing_whitespace}
]
},
#{
dirs => ["src"],
filter => "*.app.src",
rules => [
{elvis_style, line_length, #{limit => 100, skip_comments => false}},
{elvis_style, no_tabs},
{elvis_style, no_trailing_whitespace}
{elvis_text_style, line_length, #{limit => 100, skip_comments => false}},
{elvis_text_style, no_tabs},
{elvis_text_style, no_trailing_whitespace}
]
}
]}.

View File

@ -56,9 +56,13 @@ from_thrift(#bctx_v1_ContextFragment{} = Ctx0) ->
from_thrift_context(Ctx) ->
{struct, _, [_VsnField | StructDef]} =
bouncer_context_v1_thrift:struct_info('ContextFragment'),
% NOTE
% This 3 refers to the first data field in a ContextFragment, after version field.
from_thrift_struct(StructDef, Ctx, 3, #{}).
from_thrift_struct(StructDef, Struct) ->
% NOTE
% This 2 refers to the first field in a record tuple.
from_thrift_struct(StructDef, Struct, 2, #{}).
from_thrift_struct([{_, _Req, Type, Name, _Default} | Rest], Struct, Idx, Acc) ->
@ -117,18 +121,23 @@ to_thrift(Context) ->
{struct, _, StructDef} = bouncer_context_v1_thrift:struct_info('ContextFragment'),
to_thrift_struct(StructDef, Context, #bctx_v1_ContextFragment{}).
to_thrift_struct([{Idx, _Req, Type, Name, Default} | Rest], Map, Acc) ->
to_thrift_struct(StructDef, Map, Acc) ->
% NOTE
% This 2 refers to the first field in a record tuple.
to_thrift_struct(StructDef, Map, 2, Acc).
to_thrift_struct([{_Tag, _Req, Type, Name, Default} | Rest], Map, Idx, Acc) ->
case maps:take(Name, Map) of
{V, MapLeft} ->
Acc1 = erlang:setelement(Idx + 1, Acc, to_thrift_value(Type, V)),
to_thrift_struct(Rest, MapLeft, Acc1);
Acc1 = erlang:setelement(Idx, Acc, to_thrift_value(Type, V)),
to_thrift_struct(Rest, MapLeft, Idx + 1, Acc1);
error when Default /= undefined ->
Acc1 = erlang:setelement(Idx + 1, Acc, Default),
to_thrift_struct(Rest, Map, Acc1);
Acc1 = erlang:setelement(Idx, Acc, Default),
to_thrift_struct(Rest, Map, Idx + 1, Acc1);
error ->
to_thrift_struct(Rest, Map, Acc)
to_thrift_struct(Rest, Map, Idx + 1, Acc)
end;
to_thrift_struct([], MapLeft, Acc) ->
to_thrift_struct([], MapLeft, _Idx, Acc) ->
case map_size(MapLeft) of
0 ->
Acc;