config-rs icon indicating copy to clipboard operation
config-rs copied to clipboard

Serde alias not working

Open hal8888 opened this issue 3 years ago • 1 comments

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 field app_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?

hal8888 avatar Oct 14 '22 05:10 hal8888

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

mfirhas avatar Oct 22 '23 00:10 mfirhas