Skip to content

Commit

Permalink
Merge pull request #195 from emqx/fix-sort-order
Browse files Browse the repository at this point in the history
fix: struct order error if namespace is undefined
  • Loading branch information
zhongwencool authored Apr 25, 2022
2 parents 3351493 + 483e84c commit f25a375
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/hocon_schema.erl
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ find_structs_per_type(_Schema, _Type, Acc, _Stack, _TStack) ->
Acc.

find_ref(Schema, Name, Acc, Stack, TStack) ->
Namespace = hocon_schema:namespace(Schema),
Key = {Namespace, Name},
Namespace = namespace(Schema),
Key = {Namespace, Schema, Name},
Path = path(Stack),
Paths =
case maps:find(Key, Acc) of
Expand All @@ -327,7 +327,7 @@ find_structs(Schema) ->
RootNs = hocon_schema:namespace(Schema),
{RootNs, RootFields, [
{Ns, Name, Fields}
|| {{Ns, Name}, Fields} <- lists:keysort(1, maps:to_list(All))
|| {{Ns, _Schema, Name}, Fields} <- lists:keysort(1, maps:to_list(All))
]}.

unify_roots(Schema) ->
Expand Down
11 changes: 6 additions & 5 deletions src/hocon_tconf.erl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
-define(NULL_BOX, #{?METADATA => #{made_for => null_value}}).
-define(MAGIC, '$magic_chicken').
-define(MAGIC_SCHEMA, #{type => ?MAGIC}).
-define(MAP_KEY_RE, <<"^[A-Za-z0-9]+[A-Za-z0-9-_]*$">>).

%% @doc generates application env from a parsed .conf and a schema module.
%% For example, one can set the output values by
Expand Down Expand Up @@ -529,13 +530,13 @@ map_field(?MAP(_Name, Type), FieldSchema, Value, Opts) ->
%% start over
do_map(NewFields, Value, Opts, NewSc);
InvalidNames ->
Reason =
Context =
#{
reason => invalid_map_key,
path => path(Opts),
expected_data_type => ?MAP_KEY_RE,
got => InvalidNames
},
{validation_errs(Opts, Reason), Value}
{validation_errs(Opts, Context), Value}
end
end;
map_field(?R_REF(Module, Ref), FieldSchema, Value, Opts) ->
Expand Down Expand Up @@ -910,7 +911,7 @@ ensure_obfuscate_sensitive(Opts, Schema, Val) ->

obfuscate(Schema, Value) ->
case field_schema(Schema, sensitive) of
true -> "******";
true -> <<"******">>;
_ -> Value
end.

Expand Down Expand Up @@ -1136,7 +1137,7 @@ get_invalid_name(Names) ->
fun(F) ->
nomatch =:=
try
re:run(F, "^[A-Za-z0-9]+[A-Za-z0-9-_]*$")
re:run(F, ?MAP_KEY_RE)
catch
_:_ -> nomatch
end
Expand Down
10 changes: 5 additions & 5 deletions test/hocon_tconf_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ obfuscate_sensitive_values_test() ->
#{
<<"bar">> => #{
<<"union_with_default">> => dummy,
<<"field1">> => "******"
<<"field1">> => <<"******">>
}
},
Res1
Expand All @@ -111,7 +111,7 @@ obfuscate_sensitive_map_test() ->
Opts1 = #{obfuscate_sensitive_values => true},
Conf1 = map_translate_conf(Hocon, Opts1),
?assertMatch(
#{<<"foo">> := #{<<"max">> := 2, <<"min">> := 1, <<"setting">> := "******"}},
#{<<"foo">> := #{<<"max">> := 2, <<"min">> := 1, <<"setting">> := <<"******">>}},
richmap_to_map(Conf1)
),
ok.
Expand All @@ -129,7 +129,7 @@ obfuscate_sensitive_fill_default_test() ->
Opts1 = #{obfuscate_sensitive_values => true, only_fill_defaults => true},
Conf1 = map_translate_conf(Hocon, Opts1),
?assertMatch(
#{<<"foo">> := #{<<"max">> := 2, <<"min">> := 1, <<"setting">> := "******"}},
#{<<"foo">> := #{<<"max">> := 2, <<"min">> := 1, <<"setting">> := <<"******">>}},
richmap_to_map(Conf1)
),

Expand All @@ -143,7 +143,7 @@ obfuscate_sensitive_fill_default_test() ->
Opts3 = #{obfuscate_sensitive_values => true},
Conf3 = map_translate_conf(Hocon, Opts3),
?assertMatch(
#{<<"foo">> := #{<<"max">> := 2, <<"min">> := 1, <<"setting">> := "******"}},
#{<<"foo">> := #{<<"max">> := 2, <<"min">> := 1, <<"setting">> := <<"******">>}},
richmap_to_map(Conf3)
),
ok.
Expand Down Expand Up @@ -946,7 +946,7 @@ sensitive_data_obfuscation_test() ->
receive
#{hocon_env_var_name := "EMQX_SECRET", path := Path, value := Value} ->
?assertEqual("secret", Path),
?assertEqual("******", Value)
?assertEqual(<<"******">>, Value)
end
end,
envs([{"EMQX_SECRET", "bbb"}])
Expand Down

0 comments on commit f25a375

Please sign in to comment.