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

using Environment source to get List of struct #536

Open
Gaelik-git opened this issue Feb 23, 2024 · 5 comments
Open

using Environment source to get List of struct #536

Gaelik-git opened this issue Feb 23, 2024 · 5 comments

Comments

@Gaelik-git
Copy link

Hello I would like to know if it's actually possible to have this Settings::new return a Ok result

use config::{Config, ConfigError, Environment};
use serde::Deserialize;

#[derive(Debug, Deserialize)]
#[allow(unused)]
struct Auth {
    token: String,
    secret: String,
}

#[derive(Debug, Deserialize)]
#[allow(unused)]
pub struct Settings {
    auths: Vec<Auth>,
}

impl Settings {
    pub fn new() -> Result<Self, ConfigError> {
        let s = Config::builder()
            .add_source(
                Environment::with_prefix("APP")
                    .separator("_")
                    .try_parsing(true),
            )
            .build()?;

        s.try_deserialize()
    }
}

fn main() {
    let settings = Settings::new();

    // Print out our settings
    println!("{:?}", settings);
}

I was thinking something like APP_AUTHS_0_TOKEN=token APP_AUTHS_0_SECRET=secret cargo run
I found the list_separator method on Environment but I think it's only to manage Vec<String>

Is it something that is currently possible ? If not is it a feature that would make sense to implement or maybe I should use another Source ?

Thanks

@matthiasbeyer
Copy link
Member

Hi, I think something like that is not possible right now.

@momvart
Copy link

momvart commented Sep 4, 2024

It would be great to have support for this. Also, the convention that the OP has used is employed in ASP.Net as well.

@0xForerunner
Copy link

+1 on this!

@epage
Copy link
Contributor

epage commented Jan 2, 2025

git puts the index in a weird spot but they also require a _COUNT variable

From https://git-scm.com/docs/git-config#ENVIRONMENT

GIT_CONFIG_COUNT
GIT_CONFIG_KEY_<n>
GIT_CONFIG_VALUE_<n>

If GIT_CONFIG_COUNT is set to a positive number, all environment pairs GIT_CONFIG_KEY_<n> and GIT_CONFIG_VALUE_<n> up to that number will be added to the process’s runtime configuration. The config pairs are zero-indexed. Any missing key or value is treated as an error. An empty GIT_CONFIG_COUNT is treated the same as GIT_CONFIG_COUNT=0, namely no pairs are processed. These environment variables will override values in configuration files, but will be overridden by any explicit options passed via git -c.

This is useful for cases where you want to spawn multiple git commands with a common configuration but cannot depend on a configuration file, for example when writing scripts.

@epage epage added the A-source label Jan 2, 2025
@weyert
Copy link

weyert commented Jan 3, 2025

Wouldn’t the reason of _COUNT already being mentioned above?

This is useful for cases where you want to spawn multiple git commands with a common configuration but cannot depend on a configuration file, for example when writing scripts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants