Thread overview
App/lib config system
Jul 08, 2020
Jacob Carlborg
Jul 09, 2020
Kagamin
Jul 09, 2020
Jacob Carlborg
Jul 10, 2020
Kagamin
Jul 10, 2020
Kagamin
July 08, 2020
I'm looking for a way to configure applications and libraries. I'm imagining a way for libraries to hook into a single point to provide the configuration parameters the library provides. These parameters can then be changed in one location from the user of the libraries (usually an application). My main requirement is that it should be possible to configure compile time values. I think the best way to explain what I'm looking for is with an example:

module log;

import config;

void log(Level level, string message)
{
    static if (config.log.isEnabled)
    {
        if (config.log.level >= level)
            writeln(message);
    }
}

In the above example, the code would be part of the "log" library. The configuration parameters `config.log.isEnabled` and `config.log.level` can be changed by the application that is using the "log" library. `config.log.isEnabled` is a compile time value, that can only be changed at compile time. `config.log.level` is a runtime value that can be changed at runtime as well.

Here's a list of my ideal requirements:

* Should support both compile time and runtime config parameters
* Arbitrary libraries should be able to hook into config system to provide it with its config parameters
* There should be one place to change the config
* The "config file" should be a regular D module
* It should work with Dub. In the above example the "log" library and the application would be in separate Dub packages.

I've done some experiments and I have some pieces working, but have not been able to glue everything together. I don't mind if there are any dirty tricks behind the scenes but I would like it to be as easy as the above example to use it, if possible.

Does anyone have a system like this that is already available?

--
/Jacob Carlborg

July 09, 2020
If you suspect there's a contradiction in requirements, you need to specify them with better precision.
July 09, 2020
On Thursday, 9 July 2020 at 06:57:22 UTC, Kagamin wrote:
> If you suspect there's a contradiction in requirements, you need to specify them with better precision.

What are the contradictions in the requirements? I don't see any.

--
/Jacob Carlborg
July 10, 2020
Contradictions that don't let you glue it all together.
July 10, 2020
Without contradictions the solution is trivial:

module config;

version(LogEnabled) enum isEnabled=true;
else enum isEnabled=false;

shared int level;