-
Notifications
You must be signed in to change notification settings - Fork 31
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
How to handle replacement of single config entries inside a nested configuration? #60
Comments
All you need to do is ensure that the CLI arguments structure serializes in such a way that's compatible with the structure of your other configuration options. Serde has an incredible number of options to configure the automatic derivation of Alternatively, you could manually convert your CLI config structure into a |
Thanks for your answer - I didn't know about serde attributes, yet. That looks very promising. Edit:
ApparentlySerde + Figment take care of the rest of the work and make sure, the arguments are unwrapped correctly to their inner values and merging the result with my base figment works like a charm. |
Hi,
First of all: I just recently started using Figment and really like it - thanks for your work :)
I did not really know where the best place to ask the following question would be, therefore I decided to create an issue.
However of course feel free to point me in another direction and close the issue, if this is the wrong place to ask questions ;)
Now to my question:
I am currently struggling with merging multiple configuration sources for a nested configuration.
Here's a simplified example, to illustrate my setup:
Source A - toml file
I have a toml file structured like this:
Source B - struct with defaults
Source C - some arguments parsed by clap
Goal
My struct provides my configuration defaults. Then the toml file takes priority and clap arguments have the highest priority.
Question
What would be the best/simplest way to merge these CLI-arguments with my other sources? As shown above I can't just use my whole CLI-configuration as a source, because a) it has other arguments and commands as well and b) its structure is different to my actual configuration structure. I thought of implementing
From<SomeConfigOptions>
on myConfig
, however then I would need to provide default values to the fields missing from my clap args and these would then override parts of my configuration from e.g. the toml file. Another way I thought of would be to create some kind of DTO struct which imitates the structure of my config, so that the nesting would be correct. But that would mean I have to write such a struct for every optional argument and that would feel really hacky. Or do I need to write a provider? But even if so, how would that look like so that it solves my problem?I feel like I am missing something here and am probably overthinking the whole thing. But I just can't figure out what a good way to do this would be.
Thanks in advance :)
EDIT: Hm 🤔 one way I could think of, is to manually put the values from the arguments into nested
HashMap
s - that should probably work, but isn't really pretty either...The text was updated successfully, but these errors were encountered: