feat(config): read schemas for apps from file at build time

This commit is contained in:
Shawn 2021-07-28 14:07:43 +08:00 committed by turtleDeng
parent f93ee2ca17
commit e7ced477a9
3 changed files with 35 additions and 17 deletions

View File

@ -79,22 +79,11 @@ structs() -> ["cluster", "node", "rpc", "log", "lager",
"plugins", "sysmon", "alarm"] "plugins", "sysmon", "alarm"]
++ ?MODULE:includes(). ++ ?MODULE:includes().
-ifdef(TEST). -ifndef(EMQX_EXT_SCHEMAS).
includes() ->[]. includes() -> [].
-else. -else.
includes() -> includes() ->
[ "emqx_data_bridge" [FieldName || {FieldName, _SchemaMod} <- ?EMQX_EXT_SCHEMAS].
, "emqx_retainer"
, "emqx_statsd"
, "emqx_authn"
, "emqx_authz"
, "emqx_bridge_mqtt"
, "emqx_modules"
, "emqx_management"
, "emqx_dashboard"
, "emqx_gateway"
, "emqx_prometheus"
].
-endif. -endif.
fields("cluster") -> fields("cluster") ->
@ -525,9 +514,16 @@ fields("alarm") ->
, {"validity_period", t(duration(), undefined, "24h")} , {"validity_period", t(duration(), undefined, "24h")}
]; ];
fields(ExtraField) -> fields(FieldName) ->
Mod = to_atom(ExtraField++"_schema"), extra_schema_fields(FieldName).
Mod:fields(ExtraField).
-ifndef(EMQX_EXT_SCHEMAS).
extra_schema_fields(FieldName) -> error({unknown_field, FieldName}).
-else.
extra_schema_fields(FieldName) ->
{_, Mod} = lists:keyfind(FieldName, 1, ?EMQX_EXT_SCHEMAS),
Mod:fields(FieldName).
-endif.
mqtt_listener() -> mqtt_listener() ->
base_listener() ++ base_listener() ++

17
extension_schemas.config Normal file
View File

@ -0,0 +1,17 @@
%% -*-: erlang -*-
%% This file lists all the hocon schemas other than the `emqx_schema`.
%% Each element is a two-element tuple where the first element is the field name,
%% and the second element is the schema module name.
%%
[ {"emqx_data_bridge", emqx_data_bridge_schema}
, {"emqx_retainer", emqx_retainer_schema}
, {"emqx_statsd", emqx_statsd_schema}
, {"emqx_authn", emqx_authn_schema}
, {"emqx_authz", emqx_authz_schema}
, {"emqx_bridge_mqtt", emqx_bridge_mqtt_schema}
, {"emqx_modules", emqx_modules_schema}
, {"emqx_management", emqx_management_schema}
, {"emqx_dashboard", emqx_dashboard_schema}
, {"emqx_gateway", emqx_gateway_schema}
, {"emqx_prometheus", emqx_prometheus_schema}
].

View File

@ -79,6 +79,10 @@ is_cover_enabled() ->
is_enterprise() -> is_enterprise() ->
filelib:is_regular("EMQX_ENTERPRISE"). filelib:is_regular("EMQX_ENTERPRISE").
emqx_ext_schemas() ->
{ok, Schemas} = file:script("extension_schemas.config"),
Schemas.
is_quicer_supported() -> is_quicer_supported() ->
not (false =/= os:getenv("BUILD_WITHOUT_QUIC") orelse not (false =/= os:getenv("BUILD_WITHOUT_QUIC") orelse
is_win32() orelse is_centos_6() is_win32() orelse is_centos_6()
@ -134,6 +138,7 @@ common_compile_opts() ->
, {d, snk_kind, msg} , {d, snk_kind, msg}
] ++ ] ++
[{d, 'EMQX_ENTERPRISE'} || is_enterprise()] ++ [{d, 'EMQX_ENTERPRISE'} || is_enterprise()] ++
[{d, 'EMQX_EXT_SCHEMAS', emqx_ext_schemas()}] ++
[{d, 'EMQX_BENCHMARK'} || os:getenv("EMQX_BENCHMARK") =:= "1" ]. [{d, 'EMQX_BENCHMARK'} || os:getenv("EMQX_BENCHMARK") =:= "1" ].
prod_compile_opts() -> prod_compile_opts() ->