Make config file parsers pluggable (#1)

In order to simplify deps, this PR makes config file formats pluggable. The extensions supported by default are "json" and "eterm".
The default json decoder is the one from OTP, but if one prefers another (e.g. `zj`), it's possible to choose that when installing the gmconfig env.

Example of adding a yaml decoder:
```erlang
instrument_gmconfig() ->
    gmconfig:set_gmconfig_env(gmconfig_env()).

-spec gmconfig_env() -> gmconfig:gmconfig().
gmconfig_env() ->
    #{ ...
     , config_formats => add_yamerl() }.

add_yamerl() ->
    Default = maps:get(config_formats, gmconfig:default_gmconfig_env()),
    case maps:is_key("yaml", Default) of
        true ->
            Default;
        false ->
            Default#{"yaml" => fun yamerl_decode/1}
    end.

yamerl_decode(Bin) ->
    yamerl:decode(Bin, [{str_node_as_binary, true},
                        {map_node_format, map}]).
```

Co-authored-by: Ulf Wiger <ulf@wiger.net>
Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
2025-04-02 16:35:38 +09:00
parent ec02bfed41
commit de5d2bfb7d
4 changed files with 107 additions and 86 deletions
+2 -4
View File
@@ -3,9 +3,7 @@
{plugins, [rebar3_hex]}.
{deps, [
{zj, {git, "https://gitlab.com/zxq9/zj.git", {tag, "1.1.2"}}}
, {yamerl, "0.10.0"}
, {setup, "2.1.2"}
{setup, "2.2.1"}
]}.
{profiles, [
@@ -17,5 +15,5 @@
deprecated_function_calls, deprecated_functions]}.
{dialyzer, [ {warnings, [unknown]}
, {base_plt_apps, [erts, kernel, stdlib, yamerl, zj, setup]}
, {base_plt_apps, [erts, kernel, stdlib, setup]}
]}.