Search code examples
configurationyamlconfigconfiguration-filesconfigurationmanager

YAML configuration framework


I have an application where we are using configuration framework, based on *.conf files. Each such file represents some layer with configurations which can be overridden or merged with next layer. Also there is an ability to use references to some configuration definition from current definition, ability to build custom configurations based on some configuration template definition...

So actually there is an hierarchy of layers with configurations.

I am wondering if there is a mature enough YAML framework which could give above mentioned abilities? Please advise.


Solution

  • I did implement a similar configuration system for an application.

    YAML itself only loads a stream into a native data structure. You need to think about how you define this data structure. There are several approaches:

    • Implicitly. This is the way most people go when using YAML with Python or another dynamically typed language. You load the YAML file into a hierarchy of dict, list and atomic values (string, int, …). This has the benefit that it is faily easy to merge the content of different files after loading.
    • Explicitly. This means that you define the data structure you want to load your files into as type. Typical for statically typed languages like Java or Go. Here, the main problem that if you have references that cannot be encoded with YAML's anchors/aliases system (e.g. because you reference something from another file), you need to have places in your type structure where a transient reference can exist that can later be replaced with an actual value.
    • Graph-based. Most YAML implementations provide a way to load the YAML file into a node graph where each node is a sequence, a mapping, or a scalar as per YAML spec. The good part about this is that you can implement your post-processing on the node graph and can later load that into the target native structure. I used this approach with go-yaml (v3), which supports it pretty well.

    If you are searching for something that already implements most of what you want to do, I don't think you'll find anything. The problem with configurations is that they often are tightly bound to the application both in what they contain and in the way they ought to be processed. YAML is one of several common denominators for configuration files which is why it is often used for them, but more sophisticated features tend to depend so much on your specific requirements that it often does not make sense to release them as standalone frameworks.