Skip to content

Commit

Permalink
Clean up format_items
Browse files Browse the repository at this point in the history
- Rename 'none' to 'list' and make it accept only lists.
- Skip the 'process_items' step for options, which don't have items.
  • Loading branch information
Paweł Chrząszcz committed May 26, 2022
1 parent 5df2660 commit 651e21a
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion include/mongoose_config_spec.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

-record(list, {items :: mongoose_config_spec:config_node(),
validate = any :: mongoose_config_validator:list_validator(),
format_items = none :: mongoose_config_spec:format_items(),
format_items = list :: mongoose_config_spec:format_items(),
process :: undefined | mongoose_config_parser_toml:list_processor(),
wrap = default :: mongoose_config_spec:wrapper()}).

Expand Down
2 changes: 1 addition & 1 deletion src/auth/ejabberd_auth_jwt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ jwt_secret_config_spec() ->
<<"env">> => #option{type = string,
validate = non_empty},
<<"value">> => #option{type = string}},
format_items = none,
format_items = list,
process = fun ?MODULE:process_jwt_secret/1
}.

Expand Down
7 changes: 4 additions & 3 deletions src/config/mongoose_config_parser_toml.erl
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ parse_list(Path, L, #list{items = ItemSpec}) ->
end, L).

-spec handle(path(), toml_value(), mongoose_config_spec:config_node()) -> [config_part()].
handle(Path, Value, Spec = #option{}) ->
handle(Path, Value, Spec, [parse, validate, process, wrap]);
handle(Path, Value, Spec) ->
handle(Path, Value, Spec, [parse, validate, format_items, process, wrap]).

Expand Down Expand Up @@ -184,15 +186,14 @@ validate_keys(#section{validate_keys = Validator}, Section) ->

-spec format_items_spec(mongoose_config_spec:config_node()) -> mongoose_config_spec:format_items().
format_items_spec(#section{format_items = FormatItems}) -> FormatItems;
format_items_spec(#list{format_items = FormatItems}) -> FormatItems;
format_items_spec(#option{}) -> none.
format_items_spec(#list{format_items = FormatItems}) -> FormatItems.

-spec format_items(config_part(), mongoose_config_spec:format_items()) -> config_part().
format_items(KVs, map) ->
Keys = lists:map(fun({K, _}) -> K end, KVs),
mongoose_config_validator:validate_list(Keys, unique),
maps:from_list(KVs);
format_items(Value, none) ->
format_items(Value, list) when is_list(Value) ->
Value.

-spec validate(config_part(), mongoose_config_spec:config_node()) -> any().
Expand Down
18 changes: 9 additions & 9 deletions src/config/mongoose_config_spec.erl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@

%% This option allows to put list/section items in a map
-type format_items() ::
none % keep the processed items unchanged
list % keep the processed items in a list
| map. % convert the processed items (which have to be a KV list) to a map

-export_type([config_node/0, config_section/0, config_list/0, config_option/0,
Expand Down Expand Up @@ -113,7 +113,7 @@ root() ->
required = [<<"general">>],
process = fun ?MODULE:process_root/1,
wrap = none,
format_items = none
format_items = list
}.

%% path: host_config[]
Expand Down Expand Up @@ -148,7 +148,7 @@ host_config() ->
<<"s2s">> => s2s()
},
wrap = none,
format_items = none
format_items = list
}.

%% path: general
Expand Down Expand Up @@ -194,7 +194,7 @@ general() ->
wrap = host_config},
<<"mongooseimctl_access_commands">> => #section{
items = #{default => ctl_access_rule()},
format_items = none,
format_items = list,
wrap = global_config},
<<"routing_modules">> => #list{items = #option{type = atom,
validate = module},
Expand All @@ -209,7 +209,7 @@ general() ->
wrap = global_config}
},
wrap = none,
format_items = none
format_items = list
}.

general_defaults() ->
Expand All @@ -231,7 +231,7 @@ ctl_access_rule() ->
items = #{<<"commands">> => #list{items = #option{type = string}},
<<"argument_restrictions">> => #section{
items = #{default => #option{type = string}},
format_items = none
format_items = list
}
},
defaults = #{<<"commands">> => all,
Expand Down Expand Up @@ -259,7 +259,7 @@ listen() ->
|| Key <- Keys]),
process = fun mongoose_listener_config:verify_unique_listeners/1,
wrap = global_config,
format_items = none
format_items = list
}.

%% path: listen.*[]
Expand Down Expand Up @@ -439,9 +439,9 @@ outgoing_pools() ->
Items = [{Type, #section{items = #{default => outgoing_pool(Type)},
validate_keys = non_empty,
wrap = none,
format_items = none}} || Type <- PoolTypes],
format_items = list}} || Type <- PoolTypes],
#section{items = maps:from_list(Items),
format_items = none,
format_items = list,
wrap = global_config,
include = always}.

Expand Down
2 changes: 1 addition & 1 deletion src/mongoose_http_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ config_spec() ->
|| Module <- configurable_handler_modules()]),
#section{items = Items#{default => #list{items = common_handler_config_spec(),
wrap = none}},
format_items = none,
format_items = list,
validate_keys = module,
include = always}.

Expand Down
2 changes: 1 addition & 1 deletion src/pubsub/mod_pubsub.erl
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ default_node_config_spec() ->
validate = non_empty},
<<"subscribe">> => #option{type = boolean}
},
format_items = none
format_items = list
}.

process_pep_mapping(#{namespace := NameSpace, node := Node}) ->
Expand Down

0 comments on commit 651e21a

Please sign in to comment.