Просмотр исходного кода

feat: make avsc and i18n valid

JimMoen 1 год назад
Родитель
Сommit
c198ad21ae
3 измененных файлов с 50 добавлено и 5 удалено
  1. 5 1
      rebar.config
  2. 3 1
      src/emqx_plugrel.app.src
  3. 42 3
      src/emqx_plugrel.erl

+ 5 - 1
rebar.config

@@ -1,2 +1,6 @@
+%% -*- mode: erlang -*-
 {erl_opts, [debug_info]}.
-{deps, [jsx]}.
+{deps, [
+    {jsx, {git, "https://github.com/talentdeficit/jsx", {tag, "v3.1.0"}}},
+    {erlavro, {git, "https://github.com/emqx/erlavro.git", {tag, "2.10.0"}}}
+]}.

+ 3 - 1
src/emqx_plugrel.app.src

@@ -4,7 +4,9 @@
   {registered, []},
   {applications,
    [kernel,
-    stdlib
+    stdlib,
+    jsx,
+    erlavro
    ]},
   {env,[]},
   {modules, []},

+ 42 - 3
src/emqx_plugrel.erl

@@ -1,5 +1,9 @@
 -module(emqx_plugrel).
 
+-feature(maybe_expr, enable).
+
+-include_lib("kernel/include/file.hrl").
+
 -export([init/1, do/1, format_error/1]).
 
 -define(METADATA_VSN, <<"0.1.0">>).
@@ -45,7 +49,7 @@ do(State) ->
     end,
     {ok, State}.
 
--spec format_error(any()) ->  iolist().
+-spec format_error(any()) -> iolist().
 format_error(Reason) ->
     io_lib:format("~p", [Reason]).
 
@@ -146,8 +150,11 @@ do_make_tar(Cwd, NameWithVsn) ->
 maybe_copy_files(LibDir) ->
     lists:foreach(
         fun(F) ->
-            case file:read_file_info(F) of
-                {ok, _} -> rebar_file_utils:cp_r([F], LibDir);
+            maybe
+                true ?= filelib:is_regular(F),
+                true ?= validate_file(F), %% validate only when file existed
+                rebar_file_utils:cp_r([F], LibDir)
+            else
                 _ -> ok
             end
         end,
@@ -158,6 +165,38 @@ maybe_copy_files(LibDir) ->
     ),
     ok.
 
+validate_file(?plugin_avsc_file = F) ->
+    validate_avsc(F);
+validate_file(?plugin_i18n_file = F) ->
+    validate_i18n(F);
+validate_file(F) ->
+    ?LOG(debug, "Skipping validate file ~ts", [F]),
+    true.
+
+validate_avsc(F) ->
+    {ok, Bin} = file:read_file(F),
+    try avro:decode_schema(Bin) of
+        _ -> 
+            ?LOG(debug, "avsc file valid: ~ts", [F]),
+            true
+    catch
+        _ : _ ->
+            ?LOG(error, "Trying validate avsc file failed.", []),
+            error({failed_to_validate_avsc_file, F})
+    end.
+
+validate_i18n(F) ->
+    {ok, Bin} = file:read_file(F),
+    try jsx:decode(Bin) of
+        _ ->
+            ?LOG(debug, "i18n file valid: ~ts", [F]),
+            true
+    catch
+        _ : _ ->
+            ?LOG(error, "Trying validate i18n file failed.", []),
+            error({failed_to_validate_i18n_file, F})
+    end.
+
 bin(X) -> iolist_to_binary(X).
 
 str_list(L) -> lists:map(fun bin/1, L).