LukasKalbertodt/confique

Deriving environment variable names from structs

Opened this issue · 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.

jdx commented

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

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>")].