Merge pull request #8116 from zhongwencool/better-hocon-validate-msg

chore: better hocon validate msg
This commit is contained in:
zhongwencool 2022-06-06 20:29:25 +08:00 committed by GitHub
commit 914fbedd7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 29 deletions

View File

@ -29,7 +29,7 @@
{esockd, {git, "https://github.com/emqx/esockd", {tag, "5.9.2"}}}, {esockd, {git, "https://github.com/emqx/esockd", {tag, "5.9.2"}}},
{ekka, {git, "https://github.com/emqx/ekka", {tag, "0.12.8"}}}, {ekka, {git, "https://github.com/emqx/ekka", {tag, "0.12.8"}}},
{gen_rpc, {git, "https://github.com/emqx/gen_rpc", {tag, "2.8.1"}}}, {gen_rpc, {git, "https://github.com/emqx/gen_rpc", {tag, "2.8.1"}}},
{hocon, {git, "https://github.com/emqx/hocon.git", {tag, "0.28.0"}}}, {hocon, {git, "https://github.com/emqx/hocon.git", {tag, "0.28.1"}}},
{pbkdf2, {git, "https://github.com/emqx/erlang-pbkdf2.git", {tag, "2.0.4"}}}, {pbkdf2, {git, "https://github.com/emqx/erlang-pbkdf2.git", {tag, "2.0.4"}}},
{recon, {git, "https://github.com/ferd/recon", {tag, "2.5.1"}}}, {recon, {git, "https://github.com/ferd/recon", {tag, "2.5.1"}}},
{snabbkaffe, {git, "https://github.com/kafka4beam/snabbkaffe.git", {tag, "1.0.0"}}} {snabbkaffe, {git, "https://github.com/kafka4beam/snabbkaffe.git", {tag, "1.0.0"}}}

View File

@ -827,19 +827,24 @@ schema_converter(Options) ->
maps:get(schema_converter, Options, fun hocon_schema_to_spec/2). maps:get(schema_converter, Options, fun hocon_schema_to_spec/2).
serialize_hocon_error_msg({_Schema, Errors}) -> serialize_hocon_error_msg({_Schema, Errors}) ->
Msg = lists:map(fun hocon_error/1, Errors), Msg =
iolist_to_binary(Msg); case lists:map(fun hocon_error/1, Errors) of
[Error0] -> Error0;
Errors -> Errors
end,
iolist_to_binary(io_lib:format("~0p", [Msg]));
serialize_hocon_error_msg(Error) -> serialize_hocon_error_msg(Error) ->
iolist_to_binary(io_lib:format("~p", [Error])). iolist_to_binary(io_lib:format("~0p", [Error])).
hocon_error({Type, #{path := Path} = Error}) -> hocon_error({Type, #{path := Path} = Error}) ->
Error1 = maps:without([path, stacktrace], Error), Error1 = maps:without([path, stacktrace], Error),
emqx_logger_jsonfmt:best_effort_json(
Error1#{ Error1#{
<<"path">> => sub_path(Path), path => sub_path(Path),
<<"type">> => Type type => Type,
}, reason => remove_useless_field(maps:get(reason, Error, #{}))
[] }.
).
sub_path(Path) -> string:trim(Path, leading, "root."). sub_path(Path) -> string:trim(Path, leading, "root.").
remove_useless_field(#{} = Field) -> maps:without([stacktrace], Field);
remove_useless_field(Field) -> Field.

View File

@ -4,7 +4,7 @@
{emqx, {path, "../emqx"}}, {emqx, {path, "../emqx"}},
%% FIXME: tag this as v3.1.3 %% FIXME: tag this as v3.1.3
{prometheus, {git, "https://github.com/deadtrickster/prometheus.erl", {tag, "v4.8.1"}}}, {prometheus, {git, "https://github.com/deadtrickster/prometheus.erl", {tag, "v4.8.1"}}},
{hocon, {git, "https://github.com/emqx/hocon.git", {tag, "0.28.0"}}} {hocon, {git, "https://github.com/emqx/hocon.git", {tag, "0.28.1"}}}
]}. ]}.
{edoc_opts, [{preprocess, true}]}. {edoc_opts, [{preprocess, true}]}.

View File

@ -12,6 +12,8 @@
[3] [3]
]). ]).
-define(INVALID_SPEC(_REASON_), throw({_REASON_, #{default => ?DEFAULT_INDICES}})).
namespace() -> "retainer". namespace() -> "retainer".
roots() -> ["retainer"]. roots() -> ["retainer"].
@ -124,22 +126,30 @@ retainer_indices(example) ->
retainer_indices(default) -> retainer_indices(default) ->
?DEFAULT_INDICES; ?DEFAULT_INDICES;
retainer_indices(validator) -> retainer_indices(validator) ->
fun is_valid_index_specs/1; fun check_index_specs/1;
retainer_indices(_) -> retainer_indices(_) ->
undefined. undefined.
is_valid_index_specs(IndexSpecs) -> check_index_specs([]) ->
case lists:all(fun is_valid_index_spec/1, IndexSpecs) of ok;
true -> check_index_specs(IndexSpecs) when is_list(IndexSpecs) ->
case length(IndexSpecs) =:= ordsets:size(ordsets:from_list(IndexSpecs)) of lists:foreach(fun check_index_spec/1, IndexSpecs),
true -> ok; check_duplicate(IndexSpecs);
false -> {error, duplicate_index_specs} check_index_specs(_IndexSpecs) ->
end; ?INVALID_SPEC(list_index_spec_limited).
false ->
{error, invalid_index_spec}
end.
is_valid_index_spec(IndexSpec) -> check_index_spec([]) ->
length(IndexSpec) > 0 andalso ?INVALID_SPEC(non_empty_index_spec_limited);
lists:all(fun(Idx) -> Idx > 0 end, IndexSpec) andalso check_index_spec(IndexSpec) when is_list(IndexSpec) ->
IndexSpec =:= ordsets:to_list(ordsets:from_list(IndexSpec)). case lists:all(fun(Idx) -> is_integer(Idx) andalso Idx > 0 end, IndexSpec) of
false -> ?INVALID_SPEC(pos_integer_index_limited);
true -> check_duplicate(IndexSpec)
end;
check_index_spec(_IndexSpec) ->
?INVALID_SPEC(list_index_spec_limited).
check_duplicate(List) ->
case length(List) =:= length(lists:usort(List)) of
false -> ?INVALID_SPEC(unique_index_spec_limited);
true -> ok
end.

View File

@ -66,7 +66,7 @@ defmodule EMQXUmbrella.MixProject do
# in conflict by emqtt and hocon # in conflict by emqtt and hocon
{:getopt, "1.0.2", override: true}, {:getopt, "1.0.2", override: true},
{:snabbkaffe, github: "kafka4beam/snabbkaffe", tag: "1.0.0", override: true}, {:snabbkaffe, github: "kafka4beam/snabbkaffe", tag: "1.0.0", override: true},
{:hocon, github: "emqx/hocon", tag: "0.28.0", override: true}, {:hocon, github: "emqx/hocon", tag: "0.28.1", override: true},
{:emqx_http_lib, github: "emqx/emqx_http_lib", tag: "0.5.1", override: true}, {:emqx_http_lib, github: "emqx/emqx_http_lib", tag: "0.5.1", override: true},
{:esasl, github: "emqx/esasl", tag: "0.2.0"}, {:esasl, github: "emqx/esasl", tag: "0.2.0"},
{:jose, github: "potatosalad/erlang-jose", tag: "1.11.2"}, {:jose, github: "potatosalad/erlang-jose", tag: "1.11.2"},

View File

@ -67,7 +67,7 @@
, {system_monitor, {git, "https://github.com/ieQu1/system_monitor", {tag, "3.0.3"}}} , {system_monitor, {git, "https://github.com/ieQu1/system_monitor", {tag, "3.0.3"}}}
, {getopt, "1.0.2"} , {getopt, "1.0.2"}
, {snabbkaffe, {git, "https://github.com/kafka4beam/snabbkaffe.git", {tag, "1.0.0"}}} , {snabbkaffe, {git, "https://github.com/kafka4beam/snabbkaffe.git", {tag, "1.0.0"}}}
, {hocon, {git, "https://github.com/emqx/hocon.git", {tag, "0.28.0"}}} , {hocon, {git, "https://github.com/emqx/hocon.git", {tag, "0.28.1"}}}
, {emqx_http_lib, {git, "https://github.com/emqx/emqx_http_lib.git", {tag, "0.5.1"}}} , {emqx_http_lib, {git, "https://github.com/emqx/emqx_http_lib.git", {tag, "0.5.1"}}}
, {esasl, {git, "https://github.com/emqx/esasl", {tag, "0.2.0"}}} , {esasl, {git, "https://github.com/emqx/esasl", {tag, "0.2.0"}}}
, {jose, {git, "https://github.com/potatosalad/erlang-jose", {tag, "1.11.2"}}} , {jose, {git, "https://github.com/potatosalad/erlang-jose", {tag, "1.11.2"}}}