Skip to content

[Feature request] Add configuration source reading from local data structure  #9

@NicolasMelot

Description

@NicolasMelot

I understand that the only way to define configuration values used when none of the files, environment variables or command line sources, is to use the memory source. The problem with it is hard to define complex data structures using the list of (key, value) string pairs.
I would be great if the function add_in_memory could take an existing data structure and its values, that can be used as default value until any subsequent source overrides them. For example:

use serde::Deserialize;

#[derive(Default, Deserialize)]
#[serde(rename_all(deserialize = "PascalCase"))]
struct Client {
    region: String,
    url: String,
}

#[derive(Default, Deserialize)]
#[serde(rename_all(deserialize = "PascalCase"))]
struct AppOptions {
    text: String,
    demo: bool,
    clients: Vec<Client>,
}

use config::{*, ext::*};

fn main() {
    let default = AppOptions {
        text: String::from("Default text"),
        demo: false,
        clients: Vec::new()
    };

    let config = DefaultConfigurationBuilder::new()
        .add_in_memory(default)
        .add_json_file("base.json")
        .build()
        .unwrap();
    let app: AppOptions = config.reify();
    
    if app.demo
    {
        println!("{}", &app.text);
        println!("{}", &app.clients[0].region);
        return;
    }   
    
    println!("Not a demo!");
}

Metadata

Metadata

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions