confer icon indicating copy to clipboard operation
confer copied to clipboard

Add support for configuration subsets

Open skizot722 opened this issue 9 years ago • 4 comments

Sometimes there may be a need for passing around subsets of a configuration to your methods. Accessing this section of a config can become very verbose and also a hassle, especially with deeply nested configs. This code change introduces the concept of "configuration subsets", hopefully making it easier to deal with. Please let me know what you think. I am successfully using this with my application, and I'm hoping it may be of benefit to others.

Configuration Subsets

An alternative to the GetStringMap / Materialized paths is a configuration subset. This allows you to create a new *confer.Config object which is a subset of the full configuration, specified by a key prefix. This makes passing around a subset of the configuration easy, allowing you to access the values using the getter methods listed above as you would normally. The setter methods are also supported on a configuration subset, as well as nested configuration subsets.

Example Config

---
my-app:
    logging:
        enabled: true
        level: debug
    server:
        workers:
            - 127.0.0.1
            - 127.0.0.2
            - 127.0.0.3
    database:
        bird:
            host:     feathers
            user:     postgres
            password: superl33+
        wind:
            host:     sea-spray
            user:     postgres
            password: easyPZ
Config Subset
windDbConfig := config.NewConfigSubset("my-app.database.wind")
host := windDbConfig.GetString("host")
Nested Config Subset
dbConfig := config.NewConfigSubset("my-app.database")
windDbConfig := dbConfig.NewConfigSubset("wind")
host := windDbConfig.GetString("host")

skizot722 avatar Apr 23 '16 01:04 skizot722

Looks like there are issues with godep using tip right now:

https://github.com/tools/godep/issues/452 https://github.com/tools/godep/issues/453

Until that issue is solved, build using tip is not going to succeed on Travis CI.

skizot722 avatar Apr 23 '16 02:04 skizot722

Cool. I'm curious what the use case here is? How are you using subsets?

jacobstr avatar Apr 29 '16 03:04 jacobstr

Subsets are used like in the example listed above. However in practice, the subset is nested much deeper.

skizot722 avatar Apr 29 '16 05:04 skizot722

Hi @jacobstr is this something you'd be interested in?

skizot722 avatar May 13 '16 15:05 skizot722