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

Overwrite a value from toml with env #369

Closed
tekjar opened this issue Aug 23, 2022 · 4 comments
Closed

Overwrite a value from toml with env #369

tekjar opened this issue Aug 23, 2022 · 4 comments

Comments

@tekjar
Copy link

tekjar commented Aug 23, 2022

Hi! I'm trying to overwrite a variable in toml with env variable. Due to restrictions w.r.t underscores, I had to alias a field with serde.

 config::{Config, ConfigError, File, Environment};
use serde::{Serialize,  Deserialize};

#[derive(Debug, Deserialize, Clone)]
pub struct Settings {
    pub name: String,
    pub v4: TlsConfig,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct TlsConfig {
    pub ca_path: String,
    #[serde(alias = "certpath")]
    pub cert_path: String,
    // #[serde(alias = "keypath")]
    pub key_path: String,
}

pub fn main() -> Result<(), ConfigError> {
    std::env::set_var("V4_CERTPATH", "Hello World");

    let s = Config::builder()
        .add_source(File::with_name("default.toml"))
        .add_source(Environment::default().separator("_"))
        .build()?;

    dbg!(&s);
    let v: Settings = s.try_deserialize()?;
    dbg!(v);
    Ok(())
}

This is leading go this error

Error: duplicate field `cert_path`

How do I fix this?

@matthiasbeyer
Copy link
Member

Interesting! I filed a PR that adds (and hopefully fixes at some point) a test for this in #370

@nstinus
Copy link
Contributor

nstinus commented Sep 19, 2023

I think I found a reasonable workaround: use "__" (2 '_') as separator. That way the nested struct are appropriately set.

@matthiasbeyer
Copy link
Member

Okay, I'll close this then. Thanks for reporting back!

@polarathene
Copy link
Collaborator

Thanks for reporting back!

That wasn't the original reporter btw.

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

4 participants