From de5e388afbc022ead69e49d2327c880aa46693ab Mon Sep 17 00:00:00 2001 From: Zhongwen Deng Date: Wed, 1 Jun 2022 16:56:35 +0800 Subject: [PATCH 1/3] chore: hocon validate message --- apps/emqx/rebar.config | 2 +- .../src/emqx_dashboard_swagger.erl | 25 +++++++++++-------- apps/emqx_prometheus/rebar.config | 2 +- mix.exs | 2 +- rebar.config | 2 +- 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/apps/emqx/rebar.config b/apps/emqx/rebar.config index 263de55f1..d84ddfd39 100644 --- a/apps/emqx/rebar.config +++ b/apps/emqx/rebar.config @@ -29,7 +29,7 @@ {esockd, {git, "https://github.com/emqx/esockd", {tag, "5.9.2"}}}, {ekka, {git, "https://github.com/emqx/ekka", {tag, "0.12.8"}}}, {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"}}}, {recon, {git, "https://github.com/ferd/recon", {tag, "2.5.1"}}}, {snabbkaffe, {git, "https://github.com/kafka4beam/snabbkaffe.git", {tag, "1.0.0"}}} diff --git a/apps/emqx_dashboard/src/emqx_dashboard_swagger.erl b/apps/emqx_dashboard/src/emqx_dashboard_swagger.erl index 976fa97cd..186010959 100644 --- a/apps/emqx_dashboard/src/emqx_dashboard_swagger.erl +++ b/apps/emqx_dashboard/src/emqx_dashboard_swagger.erl @@ -825,19 +825,24 @@ schema_converter(Options) -> maps:get(schema_converter, Options, fun hocon_schema_to_spec/2). serialize_hocon_error_msg({_Schema, Errors}) -> - Msg = lists:map(fun hocon_error/1, Errors), - iolist_to_binary(Msg); + 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) -> - iolist_to_binary(io_lib:format("~p", [Error])). + iolist_to_binary(io_lib:format("~0p", [Error])). hocon_error({Type, #{path := Path} = Error}) -> Error1 = maps:without([path, stacktrace], Error), - emqx_logger_jsonfmt:best_effort_json( - Error1#{ - <<"path">> => sub_path(Path), - <<"type">> => Type - }, - [] - ). + Error1#{ + path => sub_path(Path), + type => Type, + reason => remove_useless_field(maps:get(reason, Error, #{})) + }. sub_path(Path) -> string:trim(Path, leading, "root."). + +remove_useless_field(#{} = Field) -> maps:without([stacktrace], Field); +remove_useless_field(Field) -> Field. diff --git a/apps/emqx_prometheus/rebar.config b/apps/emqx_prometheus/rebar.config index b26dfa05a..4c55c7d64 100644 --- a/apps/emqx_prometheus/rebar.config +++ b/apps/emqx_prometheus/rebar.config @@ -4,7 +4,7 @@ {emqx, {path, "../emqx"}}, %% FIXME: tag this as v3.1.3 {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}]}. diff --git a/mix.exs b/mix.exs index 7d47a038d..d26d7f68d 100644 --- a/mix.exs +++ b/mix.exs @@ -66,7 +66,7 @@ defmodule EMQXUmbrella.MixProject do # in conflict by emqtt and hocon {:getopt, "1.0.2", 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}, {:esasl, github: "emqx/esasl", tag: "0.2.0"}, {:jose, github: "potatosalad/erlang-jose", tag: "1.11.2"}, diff --git a/rebar.config b/rebar.config index 499e58bb5..a92eb7074 100644 --- a/rebar.config +++ b/rebar.config @@ -67,7 +67,7 @@ , {system_monitor, {git, "https://github.com/ieQu1/system_monitor", {tag, "3.0.3"}}} , {getopt, "1.0.2"} , {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"}}} , {esasl, {git, "https://github.com/emqx/esasl", {tag, "0.2.0"}}} , {jose, {git, "https://github.com/potatosalad/erlang-jose", {tag, "1.11.2"}}} From 2f5e0ada0c9e8f09adedab6ef43acac73aa4c87b Mon Sep 17 00:00:00 2001 From: Zhongwen Deng Date: Wed, 1 Jun 2022 16:59:58 +0800 Subject: [PATCH 2/3] chore: more detailed about about retainer index spec --- .../src/emqx_retainer_schema.erl | 39 ++++++++++++------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/apps/emqx_retainer/src/emqx_retainer_schema.erl b/apps/emqx_retainer/src/emqx_retainer_schema.erl index db419bf1d..0ab71c410 100644 --- a/apps/emqx_retainer/src/emqx_retainer_schema.erl +++ b/apps/emqx_retainer/src/emqx_retainer_schema.erl @@ -124,22 +124,33 @@ retainer_indices(example) -> retainer_indices(default) -> ?DEFAULT_INDICES; retainer_indices(validator) -> - fun is_valid_index_specs/1; + fun check_index_specs/1; retainer_indices(_) -> undefined. -is_valid_index_specs(IndexSpecs) -> - case lists:all(fun is_valid_index_spec/1, IndexSpecs) of - true -> - case length(IndexSpecs) =:= ordsets:size(ordsets:from_list(IndexSpecs)) of - true -> ok; - false -> {error, duplicate_index_specs} - end; - false -> - {error, invalid_index_spec} +check_index_specs([]) -> + ok; +check_index_specs(IndexSpecs) when is_list(IndexSpecs) -> + lists:foreach(fun check_index_spec/1, IndexSpecs), + check_duplicate(IndexSpecs); +check_index_specs(_IndexSpecs) -> + invalid_spec(list_index_spec_limited). + +check_index_spec([]) -> + invalid_spec(non_empty_index_spec_limited); +check_index_spec(IndexSpec) when is_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. -is_valid_index_spec(IndexSpec) -> - length(IndexSpec) > 0 andalso - lists:all(fun(Idx) -> Idx > 0 end, IndexSpec) andalso - IndexSpec =:= ordsets:to_list(ordsets:from_list(IndexSpec)). +invalid_spec(Reason) -> + throw({Reason, #{default => ?DEFAULT_INDICES}}). From 4a49b2794359615e8a93db35ebe3048202ef0bfe Mon Sep 17 00:00:00 2001 From: Zhongwen Deng Date: Mon, 6 Jun 2022 11:05:39 +0800 Subject: [PATCH 3/3] fix: dialyzer warning --- apps/emqx_retainer/src/emqx_retainer_schema.erl | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/apps/emqx_retainer/src/emqx_retainer_schema.erl b/apps/emqx_retainer/src/emqx_retainer_schema.erl index 0ab71c410..ff683ab82 100644 --- a/apps/emqx_retainer/src/emqx_retainer_schema.erl +++ b/apps/emqx_retainer/src/emqx_retainer_schema.erl @@ -12,6 +12,8 @@ [3] ]). +-define(INVALID_SPEC(_REASON_), throw({_REASON_, #{default => ?DEFAULT_INDICES}})). + namespace() -> "retainer". roots() -> ["retainer"]. @@ -134,23 +136,20 @@ check_index_specs(IndexSpecs) when is_list(IndexSpecs) -> lists:foreach(fun check_index_spec/1, IndexSpecs), check_duplicate(IndexSpecs); check_index_specs(_IndexSpecs) -> - invalid_spec(list_index_spec_limited). + ?INVALID_SPEC(list_index_spec_limited). check_index_spec([]) -> - invalid_spec(non_empty_index_spec_limited); + ?INVALID_SPEC(non_empty_index_spec_limited); check_index_spec(IndexSpec) when is_list(IndexSpec) -> case lists:all(fun(Idx) -> is_integer(Idx) andalso Idx > 0 end, IndexSpec) of - false -> invalid_spec(pos_integer_index_limited); + false -> ?INVALID_SPEC(pos_integer_index_limited); true -> check_duplicate(IndexSpec) end; check_index_spec(_IndexSpec) -> - invalid_spec(list_index_spec_limited). + ?INVALID_SPEC(list_index_spec_limited). check_duplicate(List) -> case length(List) =:= length(lists:usort(List)) of - false -> invalid_spec(unique_index_spec_limited); + false -> ?INVALID_SPEC(unique_index_spec_limited); true -> ok end. - -invalid_spec(Reason) -> - throw({Reason, #{default => ?DEFAULT_INDICES}}).