config-rs icon indicating copy to clipboard operation
config-rs copied to clipboard

Environment Variable List of Structs

Open 0xForerunner opened this issue 1 year ago • 5 comments

Enables specifying lists of structs within environment variables.

Example

    #[derive(Deserialize, Debug, PartialEq)]
    struct Struct {
        a: Vec<Option<u32>>,
        b: u32,
    }

    #[derive(Deserialize, Debug)]
    struct ListOfStructs {
        list: Vec<Struct>,
    }

    let values = vec![
        ("LIST_0_A_0".to_owned(), "1".to_owned()),
        ("LIST_0_A_2".to_owned(), "2".to_owned()),
        ("LIST_0_B".to_owned(), "3".to_owned()),
        ("LIST_1_A_1".to_owned(), "4".to_owned()),
        ("LIST_1_B".to_owned(), "5".to_owned()),
    ];

    let environment = Environment::default()
        .separator("_")
        .try_parsing(true)
        .source(Some(values.into_iter().collect()));

    let config = Config::builder().add_source(environment).build().unwrap();

    let config: ListOfStructs = config.try_deserialize().unwrap();
    assert_eq!(
        config.list,
        vec![
            Struct {
                a: vec![Some(1), None, Some(2)],
                b: 3
            },
            Struct {
                a: vec![None, Some(4)],
                b: 5
            },
        ]
    );

0xForerunner avatar Oct 31 '24 11:10 0xForerunner

closes #603

No breaking changes in this PR, should be fully backward compatible.

0xForerunner avatar Nov 04 '24 17:11 0xForerunner

@epage or @matthiasbeyer any chance I could get one of you to take a look?

0xForerunner avatar Nov 21 '24 12:11 0xForerunner

Sorry, I had forgotten to change my watch status for this repo.

My general approach is that PRs should not be reviewed, or generally posted, until use cases and requirements are understood and we've agreed to a design. See also https://github.com/rust-cli/config-rs/blob/main/CONTRIBUTING.md#pull-requests

epage avatar Nov 21 '24 20:11 epage

Pull Request Test Coverage Report for Build 11611055215

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 53 of 53 (100.0%) changed or added relevant lines in 2 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage increased (+1.4%) to 64.416%

Files with Coverage Reduction New Missed Lines %
src/value.rs 1 40.18%
<!-- Total: 1
Totals Coverage Status
Change from base Build 11488444135: 1.4%
Covered Lines: 992
Relevant Lines: 1540

💛 - Coveralls

coveralls avatar Nov 21 '24 20:11 coveralls

@epage sounds good to me. I needed this change on my personal fork anyhow. I posted an issue here a couple weeks ago. Happy to discuss any design changes you'd like to see there.

0xForerunner avatar Nov 21 '24 20:11 0xForerunner