-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Feature request: new methods viper.GetOrDefault()
returning default value if the config is not set
#1459
Comments
👋 Thanks for reporting! A maintainer will take a look at your issue shortly. 👀 In the meantime: We are working on Viper v2 and we would love to hear your thoughts about what you like or don't like about Viper, so we can improve or fix those issues. ⏰ If you have a couple minutes, please take some time and share your thoughts: https://forms.gle/R6faU74qPRPAzchZ9 📣 If you've already given us your feedback, you can still help by spreading the news, https://twitter.com/sagikazarmark/status/1306904078967074816 Thank you! ❤️ |
Thanks for opening this issue @Bin-Huang ! I realize this is not very well explained in the documentation, but ideally Viper shouldn't leave the main function. It's also a good practice to keep your Viper setup and config unmarshaling separate. Here is a good example of that. Lastly, I personally don't like the Get* interface, because it hints at using Viper outside of the main function. It's better to use For these reasons, I'm not really a fond of the idea to expand the Get* interface. I'd love to hear your thoughts about the above practices and examples and whether they would work for your use case (or if not, why). |
Your practice has helped me a lot and I think it's even better. And I also have a problem with type Conf struct {
App struct {
Name string
Port int
}
}
func main() {
v := viper.New()
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
v.AllowEmptyEnv(true)
v.AutomaticEnv()
fmt.Println(`Get`, v.GetInt("app.port")) // got 8080
var conf Conf
err := v.Unmarshal(&conf)
if err != nil {
panic(err)
}
fmt.Println(conf.App.Port) // expected 8080, got 0
} When running As for a reason, I found that Is this behavior by design? Looking forward to your suggestions and answers. 😁 |
Yeah, unfortunately this is a known issue: #761 The workaround is either manually binding the env var or setting a default. It's actually probably a good idea that accepted env vars are explicitly defined somehow. I always thought that automatic env makes things a bit weird, because you never know what env vars you can use. |
Preflight Checklist
Problem Description
Sometimes we need to get some unimportant configuration, if the configuration is not set, then use the default value.
Now we have to call two methods to do this,
SetDefault()
and thenGet()
, which makes the code a bit unreadable:Proposed Solution
It will be better if there are some methods just like
GetOrDefault()
,GetStringOrDefault()
,GetBoolOrDefault()
...Alternatives Considered
No response
Additional Information
I'm interested in implementing it if the idea is considered worthwhile.
The text was updated successfully, but these errors were encountered: