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

Environment Variable List of Structs #597

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

0xForerunner
Copy link

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

0xForerunner commented Nov 4, 2024

closes #603

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

@0xForerunner
Copy link
Author

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

match (&mut self.kind, other.kind) {
(ValueKind::Table(ref mut table), ValueKind::Table(other_table)) => {
for (k, v) in other_table {
match table.entry(k) {

Check failure

Code scanning / clippy

mismatched types Error

mismatched types
match (&mut self.kind, other.kind) {
(ValueKind::Table(ref mut table), ValueKind::Table(other_table)) => {
for (k, v) in other_table {
match table.entry(k) {

Check failure

Code scanning / clippy

mismatched types Error

mismatched types
(ValueKind::Table(ref mut table), ValueKind::Table(other_table)) => {
for (k, v) in other_table {
match table.entry(k) {
std::collections::hash_map::Entry::Occupied(mut entry) => {

Check failure

Code scanning / clippy

mismatched types Error

mismatched types
for (k, v) in other_table {
match table.entry(k) {
std::collections::hash_map::Entry::Occupied(mut entry) => {
entry.get_mut().merge(v);

Check failure

Code scanning / clippy

type annotations needed Error

type annotations needed
std::collections::hash_map::Entry::Occupied(mut entry) => {
entry.get_mut().merge(v);
}
std::collections::hash_map::Entry::Vacant(entry) => {

Check failure

Code scanning / clippy

mismatched types Error

mismatched types
@epage
Copy link
Contributor

epage commented Nov 21, 2024

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

@coveralls
Copy link

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%
Totals Coverage Status
Change from base Build 11488444135: 1.4%
Covered Lines: 992
Relevant Lines: 1540

💛 - Coveralls

@0xForerunner
Copy link
Author

@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.

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

Successfully merging this pull request may close these issues.

5 participants