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

Deriving environment variable names from structs #30

Open
IgnisDa opened this issue Apr 18, 2023 · 3 comments
Open

Deriving environment variable names from structs #30

IgnisDa opened this issue Apr 18, 2023 · 3 comments

Comments

@IgnisDa
Copy link

IgnisDa commented Apr 18, 2023

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
Copy link

jdx commented Dec 4, 2023

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

@ModProg
Copy link

ModProg commented Feb 6, 2024

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

@IgnisDa
Copy link
Author

IgnisDa commented Feb 6, 2024

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

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

3 participants