-
Notifications
You must be signed in to change notification settings - Fork 217
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
When no configs are present, Config::new() should default to the structs default #60
Comments
I was looking for this exact feature. Originally I thought you could work around it by converting/serialising your EDIT: I came up with a hacky workaround but in the long term it'd be nice if pub fn from_env() -> Result<Settings, Error> {
let mut s = Config::new();
let defaults = Settings::default();
s.cache = serde_json::from_value(serde_json::to_value(defaults)?)?;
let mut env = Environment::new();
env.separator("__".to_string());
s.merge(env)?;
s.try_into().map_err(Error::from)
} |
@Michael-F-Bryan that sort of worked. However whenever I merged a file my cache override got blown away. After reading through the config merge code I see that the cache is repopulated from |
I think a better solution would be to have a |
How about: s.set_defaults::<Settings>()?;
|
I think there are two issues here, aren't there? First one is how to use defaults ‒ and the answer for that one is to use the serde's default handling (eg. the The other problem is the annoying |
I didn't see that before. Thanks for making that clear. This is more of a bug then. Marking to get fixed for 0.10. |
Creating a config so
expect that the values will fall into But
😥 |
This can also be fixed by a call to The problem is the deserialization of |
Fix defaults serialization and 'invalid type: unit value' deserialization error (#60)
In an application, sometimes there is no config present and you would just like to use the default values for your struct. If this is the case currently, you get an obtuse error like:
invalid type: unit value, expected struct Settings
. As an example usage:The text was updated successfully, but these errors were encountered: