mehcode / config-rs

⚙️ Layered configuration system for Rust applications (with strong support for 12-factor applications).
Apache License 2.0
2.43k stars 206 forks source link

Serde alias not working #384

Open hal8888 opened 1 year ago

hal8888 commented 1 year ago

Im expecting to put alias to each fields in a struct where the alias name are the keys from os env

#[derive(Debug, Deserialize, Clone)]
pub struct BuildInfo {
    #[serde(alias = "VERSION")]
    pub app_version: String,
}

in my os env:

VERSION=1.2.3

When I build with:

let builder = Config::builder().add_source(Environment::default()).build();
builder.unwrap().try_deserialize::<BuildInfo>().unwrap()

I got panic error: value: missing fieldapp_version``

If I change with serde rename I got: value: missing field VERSION

it works if I just change the letter case like from VERSION to version, but I need way to map this.

How to map easily from os env with different keys name to structs with different keys name?

mfirhas commented 8 months ago

It seems that aliasing doesn't work with os envs. It works for other kind of file formats.