Ulf Wiger de5d2bfb7d 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
2025-04-02 16:35:38 +09:00
2025-02-24 22:17:41 +01:00

Gmconfig - A JSON-SCHEMA-based configuration management support library

Introduction

This library offers the basic support functions for the Gajumaru configuration management subsystem. It is based on JSON-Schema, and includes, among other things:

  • A reasonably complete JSON-Schema validator
  • Validating user configurations against the schema
  • In-service update of the user config
  • Caching of the user config (and schema) as persistent terms
  • Fast config lookups using key paths
  • Lookups can handle both schema defaults and user-provided defaults

JSON-Schema validator

The main thing the validator currently doesn't support is proper management of complex sets of schemas and subSchemas. There are some preparations made for supporting "$id" and uris, but this is still incomplete. "definitions" and "$ref" properties are recognized.

As almost anything is theoretically possible with JSON-Schema, there are surely other things that are unsupported.

All standard data types

  • "null"
  • "boolean"
  • "integer"
  • "number"
  • "boolean"
  • "string"
  • "array"
  • "object"

Dynamic properties

  • "if"
  • "not"
  • "oneOf"
  • "anyOf"
  • "allOf"

Static Properties

Array

  • "maxItems"
  • "minItems"
  • "uniqueItems"
  • "prefixItems"
  • "contains" ("minContains", "maxContains")

Object

  • "properties" ("minProperties", "maxProperties")
  • "patternProperties"
  • "additionalProperties"
  • "required"

String

  • "pattern"
  • "minLength"
  • "maxLength"

Number, Integer

  • "minimum"
  • "maximum"
  • "exclusiveMinimum"
  • "exclusiveMinimum"
  • "multipleOf" (only if object is an integer)

"$updateSemantics"

  • "replace" (fully replaces any old data)
  • "merge" (for objects, keeps and/or updates existing values)
  • "suggest" (adds value if not already present)
Description
Configuration management support
Readme ISC 144 KiB
Languages
Erlang 100%