Skip to content

Environment Variable List of Structs #603

Closed as not planned
Closed as not planned
@0xForerunner

Description

@0xForerunner

Tracking issue for #597

Support parsing environment variables into a list of structs.

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
            },
        ]
    );

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions