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

[Feature Request] allow to override default name for environment variables #96

Closed
mh-trimble opened this issue Mar 15, 2024 · 5 comments

Comments

@mh-trimble
Copy link

I am writing an application copying an existing webservice, to stay sane I am structuring the config for my copy differently than the original service.

Most of the config I get from a toml file, but when I want to ship this to production I have to get api keys from environment variables that don't match the nested structure imposed by figment.

Is there a way to override the default (structured) names for environment variables like in this example taken from the envconfig crate

#[derive(Envconfig)]
struct Config {
    #[envconfig(from = "DB_HOST")]
    pub db_host: String,

    #[envconfig(from = "DB_PORT")]
    pub db_port: Option<u16>,

    #[envconfig(from = "HTTP_PORT", default = "8080")]
    pub http_port: u16,
}
@SergioBenitez
Copy link
Owner

I'm not quite sure I follow the question. Can you give a concrete example of what you're doing now, what doesn't work with it, and what you'd like to do? Alternatively, consider looking at https://serde.rs/field-attrs.html. Maybe alias or rename are what you want?

@mh-trimble
Copy link
Author

mh-trimble commented Mar 18, 2024

Hey sure I'll try. The config I use is looking something like

host = "localhost"
port = 9000
[remote-service]
api_key = "dev-key"
...
#[derive(Debug, Deserialize, Clone)]
struct Config {
host: String,
port: u16,
remote_service: RemoteServiceConfig,
}

#[derive(Debug, Deserialize, Clone)]
struct RemoteService {
api_key: String
}

in production the environment variable to set/override is FOO_API_KEY instead of REMOTE_SERVICE__API_KEY - for historical reasons.

Is there a way (that I don't know) to set the name of the environment variable to deserialize the nested api_key?

@mh-trimble
Copy link
Author

I hope this is a bit clearer

@mh-trimble
Copy link
Author

I finally got around to testing the serde route and it worked - would you mind me adding a bit of documentation to help future users of the library (a.k.a. me in a month).

@SergioBenitez
Copy link
Owner

I finally got around to testing the serde route and it worked - would you mind me adding a bit of documentation to help future users of the library (a.k.a. me in a month).

Sure! What specifically might have helped you?

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

2 participants