Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for configuration subsets #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

skizot722
Copy link

@skizot722 skizot722 commented Apr 23, 2016

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
Copy link
Author

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

tools/godep#452
tools/godep#453

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

// NewConfigSubset returns a new config based on
func (manager *Config) NewConfigSubset(prefix string) *Config {
configSubset := &Config{}
*configSubset = *manager
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. You're choosing to copy the data here rather than maintaining a reference. Would it surprise you to find that your subset was unchanged after modifying a value in the parent config?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No surprise to me :-).This was intentional, as I only want to modify the subset of config passed. That's why I chose to prefix the function name with "new". I can change it to a reference, but the intent was to make a copy of the subset.

@jacobstr
Copy link
Owner

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

@skizot722
Copy link
Author

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

@skizot722
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants