From a2ded48dac032a609cb7b87747700151322110e7 Mon Sep 17 00:00:00 2001 From: JimMoen Date: Fri, 17 May 2024 00:16:46 +0800 Subject: [PATCH 1/3] chore: bump gitignore --- .gitignore | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 71ab013..f8821f1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,20 +1,33 @@ -.rebar3 -_* .eunit +deps *.o *.beam *.plt -*.swp -*.swo -.erlang.cookie -ebin -log erl_crash.dump +ebin +rel/example_project +.concrete/DEV_MODE .rebar -logs -_build -.idea -*.iml +.erlang.mk/ +data/ +emqx_plugin_template.d +.DS_Store +erlang.mk +_build/ +rebar.lock +test/ct.cover.spec +.rebar3 +/.idea/ rebar3.crashdump +rebar3 +erlang_ls.config +# VSCode files +.vs/ +.vscode/ +# Emacs Backup files *~ -rebar.lock +# Emacs temporary files +.#* +*# +# For direnv +.envrc From c7d7f217bcf610a34a17f8d0871855c5079c4196 Mon Sep 17 00:00:00 2001 From: JimMoen Date: Fri, 17 May 2024 00:20:11 +0800 Subject: [PATCH 2/3] chore: maybe_expr back to module for IDE and other editing modes --- rebar.config | 2 +- src/emqx_plugrel.erl | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/rebar.config b/rebar.config index 9fa61cf..94023c3 100644 --- a/rebar.config +++ b/rebar.config @@ -1,6 +1,6 @@ %% -*- mode: erlang -*- -{erl_opts, [debug_info, {feature, maybe_expr, enable}]}. +{erl_opts, [debug_info]}. {deps, [ {erlavro, {git, "https://github.com/emqx/erlavro.git", {tag, "2.10.0"}}} diff --git a/src/emqx_plugrel.erl b/src/emqx_plugrel.erl index 61c4bc3..ab39774 100644 --- a/src/emqx_plugrel.erl +++ b/src/emqx_plugrel.erl @@ -1,5 +1,7 @@ -module(emqx_plugrel). +-feature(maybe_expr, enable). + -export([init/1, do/1, format_error/1]). -define(METADATA_VSN, <<"0.1.0">>). From 39495225ed8c75790624cafd553b6e23808a714a Mon Sep 17 00:00:00 2001 From: JimMoen Date: Fri, 17 May 2024 03:10:31 +0800 Subject: [PATCH 3/3] feat: validate avsc and hocon when packaging --- rebar.config | 1 + src/emqx_plugrel.erl | 39 +++++++++++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/rebar.config b/rebar.config index 94023c3..86eb6a5 100644 --- a/rebar.config +++ b/rebar.config @@ -3,5 +3,6 @@ {erl_opts, [debug_info]}. {deps, [ + {hocon, {git, "https://github.com/emqx/hocon.git", {tag, "0.42.2"}}}, {erlavro, {git, "https://github.com/emqx/erlavro.git", {tag, "2.10.0"}}} ]}. diff --git a/src/emqx_plugrel.erl b/src/emqx_plugrel.erl index ab39774..14b846d 100644 --- a/src/emqx_plugrel.erl +++ b/src/emqx_plugrel.erl @@ -9,6 +9,9 @@ -define(plugin_readme_file, "README.md"). -define(plugin_avsc_file, "priv/config_schema.avsc"). -define(plugin_i18n_file, "priv/config_i18n.json"). +-define(plugin_hocon_file, "priv/config.hocon"). +-define(validate_but_no_copy, undefined). + -define(LOG(LEVEL, FORMAT, ARGS), rebar_api:LEVEL("[emqx_plugrel] " ++ FORMAT, ARGS)). @@ -147,18 +150,21 @@ do_make_tar(Cwd, NameWithVsn) -> maybe_copy_files(LibDir) -> lists:foreach( - fun(F) -> + fun({F, TargetDir}) -> maybe true ?= filelib:is_regular(F), true ?= validate_file(F), %% validate only when file existed - rebar_file_utils:cp_r([F], LibDir) + case TargetDir of + ?validate_but_no_copy -> ok; + _ -> rebar_file_utils:cp_r([F], TargetDir), ok + end else _ -> ok end end, - [ ?plugin_readme_file - , ?plugin_avsc_file - , ?plugin_i18n_file + [ {?plugin_readme_file, LibDir} + , {?plugin_avsc_file, ?validate_but_no_copy} + , {?plugin_i18n_file, ?validate_but_no_copy} ] ), ok. @@ -172,14 +178,27 @@ validate_file(F) -> true. validate_avsc(F) -> - {ok, Bin} = file:read_file(F), - try avro:decode_schema(Bin) of - _ -> - ?LOG(debug, "Valid AVRO schema file: ~ts", [F]), + Name = <<"TryDecodeDefaultAvro">>, + try + begin + {ok, HoconMap} = hocon:load(?plugin_hocon_file), + JsonEncodedAvroBin = jsone:encode(HoconMap, [native_forward_slash, {space, 1}, {indent, 4}]), + Store0 = avro_schema_store:new([map]), + {ok, AvscBin} = file:read_file(?plugin_avsc_file), + Store = avro_schema_store:import_schema_json(Name, AvscBin, Store0), + Opts = avro:make_decoder_options([{map_type, map}, {record_type, map}, {encoding, avro_json}]), + {ok, avro_json_decoder:decode_value(JsonEncodedAvroBin, Name, Store, Opts)} + end + + of + {ok, _Value} -> + ?LOG(debug, "Schema matched. Valid Plugin Hocon and Schema files: ~ts, ~ts", [?plugin_hocon_file, ?plugin_avsc_file]), true catch E : R : S -> - ?LOG(error, "Invalid AVRO schema file. Error = ~p, Reason = ~p, Stacktrace=~p", [E, R, S]), + ?LOG(error, "Invalid plugin Hocon or Schema. Please check ~ts and ~ts.~n" + "Error = ~p, Reason = ~p, Stacktrace=~p", + [?plugin_hocon_file, ?plugin_avsc_file, E, R, S]), error({failed_to_validate_avsc_file, F}) end.