Preflight Checklist
- [x] I have searched the issue tracker for an issue that matches the one I want to file, without success.
Problem Description
I use viper in web development to help me manage and read configuration files. One problem is: I need to connect to the database. When the specific database.type is not indicated in the configuration file, I hope I can point to the sqlite database by default, but viper does not support providing default configuration when the configuration file fails to be obtained, resulting in a lot of redundancy and unsightly appearance in my code. I want viper to provide an optional parameter to the function to provide a default configuration item. Viper is a good library.
Proposed Solution
It is hoped that the default value option can be supported when the configuration item fails to be obtained. Just like when the viper.GetString("database.type") function fails to get the configuration, it allows you to manually configure a return value, just like the viper.GetString("database.type", "sqlite"), where the second parameter represents the default value returned if the get fails.
Alternatives Considered
Currently I am working through the viper.IsSet solve the problem.
func GetString(key string, defaultValue string) string {
if viper.IsSet(key) {
return viper.GetString(key)
} else {
return defaultValue
}
}
But I still hope that viper will provide a solution to this problem gracefully in the future.
Additional Information
No response
Comment From: ccoVeille
@lfcypo based on GetString, it returns a single value
https://pkg.go.dev/github.com/spf13/viper#GetString
Did you consider using cmp.Or ?
dbType := cmp.Or(viper.GetString("database.type"), "sqlite")
You could add you own method with a wrapper
func getStringFromViper(key, defaultValue string)
return cmp.Or(viper.GetString(key), defaultValue)
}
Please note, I'm not arguing about the idea of the having default value for v2.
But, I'm suggesting a solution that can be used now.
Comment From: lfcypo
@ccoVeille Thank you for your comment : ) This approach is useful in some situations. However, it can cause trouble in some cases. I want it to be a generic solution.
For example,
config.json
{
"a_configuration_item_with_content": "hello",
"a_configuration_item_with_an_empty_string": ""
}
package main
import (
"cmp"
"fmt"
"github.com/spf13/viper"
)
func getStringFromViper(key, defaultValue string) string {
return cmp.Or(viper.GetString(key), defaultValue)
}
func main() {
viper.SetConfigFile("./config.json")
err := viper.ReadInConfig()
if err != nil {
panic(err)
}
// This is no problem
fmt.Println(getStringFromViper("a_configuration_item_with_content", "default_value"))
// Output: hello
// This is no problem because the key a_unknown_configuration_item is indeed not set
fmt.Println(getStringFromViper("a_unknown_configuration_item", "default_value"))
// Output: default_value
// I specified an empty string for it in the configuration file
// and this function should have returned an empty string, but this returned the default value.
fmt.Println(getStringFromViper("a_configuration_item_with_an_empty_string", "default_value"))
// Output: default_value
}
Currently I am working through the viper.IsSet solve the problem.
func GetString(key string, defaultValue string) string {
if viper.IsSet(key) {
return viper.GetString(key)
} else {
return defaultValue
}
}
But I still hope that viper will provide a solution to this problem gracefully in the future.
Comment From: ccoVeille
Crystal clear, thanks
Comment From: github-actions[bot]
Issues with no activity for 30 days are marked stale and subject to being closed.