Skip to content

Set hierarchical child config via env #104

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

Closed
nkostoulas opened this issue Mar 21, 2019 · 8 comments
Closed

Set hierarchical child config via env #104

nkostoulas opened this issue Mar 21, 2019 · 8 comments

Comments

@nkostoulas
Copy link

For something like in the hierarchicalenv example:

#[derive(Debug, Deserialize)]
struct Braintree {
    merchant_id: String,
    public_key: String,
    private_key: String,
}

#[derive(Debug, Deserialize)]
pub struct Settings {
    debug: bool,
    braintree: Braintree,
}

Is it possible to set Braintree.public_key via an env or do I have to separately parse env for each child struct and then merge?

@chachi
Copy link

chachi commented Apr 21, 2019

I was looking for this myself and looked in the code for it. There's a separator argument which allows you to set a nested variable. It defaults to "." but can be set like the prefix argument using a separator(sep) method.

https://github.com/mehcode/config-rs/blob/master/src/env.rs#L108

@nkostoulas
Copy link
Author

Ah okay that's nice! I guess then it would make sense to have a separator different than _ as in the case of Braintree.public_key an env set to SETTINGS_BRAINTREE_PUBLIC_KEY would fail but an env set to something like SETTINGS_BRAINTREE.PUBLIC_KEY with . separator would be fine.

@nkostoulas
Copy link
Author

This makes it impossible to use variables with underscore, which is a bit of a standard in Rust. The alternative would be to use single underscore for prefix and double underscore for nested config, which is not very readable.

It still seems the best solution to read envs manually for any nested config.

@mehcode
Copy link
Collaborator

mehcode commented May 9, 2019

Config 0.9 removes this behavior by default.

See the Environment::separator method.

@nkostoulas
Copy link
Author

nkostoulas commented May 9, 2019

Which behaviour are you referring to? I meant that it's not possible to have config variables with underscore in the name, because they will be perceived as nested if an underscore separator is used.

For example using something like this:

Environment::with_prefix("SETTINGS").separator("_"))

with SETTINGS_BRAINTREE_PUBLIC_KEY

will create a key braintree.public.key instead of braintree.public_key.

Maybe it's not a good idea using underscores in variable names in this case.

@mehcode
Copy link
Collaborator

mehcode commented May 9, 2019

That's the exact behavior. I meant that that the separator configuration is opt-in (it used to not be). I know of a few users of config that opt for __ as a separator so they can use _ to mean an actual _.

// SETTINGS_BRAINTREE__PUBLIC_KEY
// braintree.public_key
Environment::with_prefix("SETTINGS").separator("__")

The _ after prefix is not configurable in 0.9.

@mehcode
Copy link
Collaborator

mehcode commented May 9, 2019

On another note, in v0.10, this will likely change to:

env::vars().prefix("settings_").map(|s| s.replace("__", "."))
  • prefix will no longer assume a trailing _
  • potentially removing (deprecating) .separator("__") as I'm not sure it makes sense as a separate method when we have map
  • config::env::vars() as the primary method of construction instead of config::Environment::new()

See #111 for more details

@nkostoulas
Copy link
Author

Thanks for the reply! I've decided to stick with reading all nested envs manually instead of the double underscore as I don't like the readability. I honestly don't see any other way around this though.

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

No branches or pull requests

3 participants