confique icon indicating copy to clipboard operation
confique copied to clipboard

Deriving environment variable names from structs

Open IgnisDa opened this issue 2 years ago • 3 comments

figment can automatically get names of expected environment variables. With the following config:


#[derive(Deserialize, Debug, Clone, Serialize)]
pub struct OpenlibraryConfig {
    pub url: String
}

#[derive(Deserialize, Debug, Clone, Serialize)]
pub struct BookConfig {
    pub openlibrary: OpenlibraryConfig
}

#[derive(Deserialize, Debug, Clone)]
pub struct AppConfig {
    #[serde(default)]
    book: BookConfig
}

let conf: AppConfig = Figment::new().merge(Env::raw().split("_")).extract()?;

The above configuration expects BOOK_OPENLIBRARY_URL, without me having to manually annotate it in the struct. Would be nice if confique supported it as well.

IgnisDa avatar Apr 18 '23 11:04 IgnisDa

frankly I see this as an antipattern. It means I can't search a codebase to find where the env vars are defined

jdx avatar Dec 04 '23 16:12 jdx

This could also be made configurable, by having an empty #[config(env)] generate this.

Simple case would be to have #[config(env)] expand to #[config(env = "<field-name-in-uppercase>")].

ModProg avatar Feb 06 '24 08:02 ModProg

I ended up going with https://github.com/moonrepo/schematic.

IgnisDa avatar Feb 06 '24 10:02 IgnisDa