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

feat(map_lib): support emqx_config:get_listener_conf/3,4

Shawn 4 лет назад
Родитель
Сommit
807bbb391c
1 измененных файлов с 34 добавлено и 0 удалено
  1. 34 0
      apps/emqx/src/emqx_config.erl

+ 34 - 0
apps/emqx/src/emqx_config.erl

@@ -20,10 +20,16 @@
 -export([ get/0
         , get/1
         , get/2
+        , find/1
         , put/1
         , put/2
         ]).
 
+-export([ get_listener_conf/3
+        , get_listener_conf/4
+        , find_listener_conf/3
+        ]).
+
 -export([ update_config/2
         ]).
 
@@ -54,6 +60,34 @@ get(KeyPath) ->
 get(KeyPath, Default) ->
     emqx_map_lib:deep_get(KeyPath, get(), Default).
 
+-spec find(emqx_map_lib:config_key_path()) ->
+    {ok, term()} | {not_found, emqx_map_lib:config_key_path(), term()}.
+find(KeyPath) ->
+    emqx_map_lib:deep_find(KeyPath, get()).
+
+-spec get_listener_conf(atom(), atom(), emqx_map_lib:config_key_path()) -> term().
+get_listener_conf(Zone, Listener, KeyPath) ->
+    case find_listener_conf(Zone, Listener, KeyPath) of
+        {not_found, SubKeyPath, Data} -> error({not_found, SubKeyPath, Data});
+        {ok, Data} -> Data
+    end.
+
+-spec get_listener_conf(atom(), atom(), emqx_map_lib:config_key_path(), term()) -> term().
+get_listener_conf(Zone, Listener, KeyPath, Default) ->
+    case find_listener_conf(Zone, Listener, KeyPath) of
+        {not_found, _, _} -> Default;
+        {ok, Data} -> Data
+    end.
+
+-spec find_listener_conf(atom(), atom(), emqx_map_lib:config_key_path()) ->
+    {ok, term()} | {not_foud, emqx_map_lib:config_key_path(), term()}.
+find_listener_conf(Zone, Listener, KeyPath) ->
+    %% the configs in listener is prior to the ones in the zone
+    case find([zones, Zone, listeners, Listener | KeyPath]) of
+        {not_found, _, _} -> find([zones, Zone | KeyPath]);
+        {ok, Data} -> {ok, Data}
+    end.
+
 -spec put(map()) -> ok.
 put(Config) ->
     persistent_term:put(?CONF, Config).