Added default parameter on getters to return it in case it doesn't exist

This commit is contained in:
Roger Solé 2016-08-25 15:09:30 +02:00
parent 7fb2782df3
commit 09ed8f49d2

103
viper.go
View file

@ -548,66 +548,165 @@ func (v *Viper) GetString(key string) string {
return cast.ToString(v.Get(key)) return cast.ToString(v.Get(key))
} }
// Returns the value associated with the key as a string or the passed default value
func GetDefaultString(key string, defaultValue string) string { return v.GetDefaultString(key, defaultValue) }
func (v *Viper) GetDefaultString(key string, defaultValue string) string {
if value := v.Get(key); value != nil {
return cast.ToString(value)
}
return defaultValue
}
// Returns the value associated with the key as a boolean // Returns the value associated with the key as a boolean
func GetBool(key string) bool { return v.GetBool(key) } func GetBool(key string) bool { return v.GetBool(key) }
func (v *Viper) GetBool(key string) bool { func (v *Viper) GetBool(key string) bool {
return cast.ToBool(v.Get(key)) return cast.ToBool(v.Get(key))
} }
// Returns the value associated with the key as a boolean or the passed default value
func GetDefaultBoolean(key string, defaultValue bool) bool { return v.GetDefaultBoolean(key, defaultValue) }
func (v *Viper) GetDefaultBoolean(key string, defaultValue bool) bool {
if value := v.Get(key); value != nil {
return cast.ToBool(value)
}
return defaultValue
}
// Returns the value associated with the key as an integer // Returns the value associated with the key as an integer
func GetInt(key string) int { return v.GetInt(key) } func GetInt(key string) int { return v.GetInt(key) }
func (v *Viper) GetInt(key string) int { func (v *Viper) GetInt(key string) int {
return cast.ToInt(v.Get(key)) return cast.ToInt(v.Get(key))
} }
// Returns the value associated with the key as an int or the passed default value
func GetDefaultInt(key string, defaultValue int) int { return v.GetDefaultInt(key, defaultValue) }
func (v *Viper) GetDefaultInt(key string, defaultValue int) int {
if value := v.Get(key); value != nil {
return cast.ToInt(value)
}
return defaultValue
}
// Returns the value associated with the key as an integer // Returns the value associated with the key as an integer
func GetInt64(key string) int64 { return v.GetInt64(key) } func GetInt64(key string) int64 { return v.GetInt64(key) }
func (v *Viper) GetInt64(key string) int64 { func (v *Viper) GetInt64(key string) int64 {
return cast.ToInt64(v.Get(key)) return cast.ToInt64(v.Get(key))
} }
// Returns the value associated with the key as an int64 or the passed default value
func GetDefaultInt64(key string, defaultValue int64) int64 { return v.GetDefaultInt64(key, defaultValue) }
func (v *Viper) GetDefaultInt64(key string, defaultValue int64) int64 {
if value := v.Get(key); value != nil {
return cast.ToInt64(value)
}
return defaultValue
}
// Returns the value associated with the key as a float64 // Returns the value associated with the key as a float64
func GetFloat64(key string) float64 { return v.GetFloat64(key) } func GetFloat64(key string) float64 { return v.GetFloat64(key) }
func (v *Viper) GetFloat64(key string) float64 { func (v *Viper) GetFloat64(key string) float64 {
return cast.ToFloat64(v.Get(key)) return cast.ToFloat64(v.Get(key))
} }
// Returns the value associated with the key as a float64 or the passed default value
func GetDefaultFloat64(key string, defaultValue float64) float64 { return v.GetDefaultFloat64(key, defaultValue) }
func (v *Viper) GetDefaultFloat64(key string, defaultValue float64) float64 {
if value := v.Get(key); value != nil {
return cast.ToFloat64(value)
}
return defaultValue
}
// Returns the value associated with the key as time // Returns the value associated with the key as time
func GetTime(key string) time.Time { return v.GetTime(key) } func GetTime(key string) time.Time { return v.GetTime(key) }
func (v *Viper) GetTime(key string) time.Time { func (v *Viper) GetTime(key string) time.Time {
return cast.ToTime(v.Get(key)) return cast.ToTime(v.Get(key))
} }
// Returns the value associated with the key as time or the passed default value
func GetDefaultTime(key string, defaultValue time.Time) time.Time { return v.GetDefaultTime(key, defaultValue) }
func (v *Viper) GetDefaultTime(key string, defaultValue time.Time) time.Time {
if value := v.Get(key); value != nil {
return cast.ToTime(value)
}
return defaultValue
}
// Returns the value associated with the key as a duration // Returns the value associated with the key as a duration
func GetDuration(key string) time.Duration { return v.GetDuration(key) } func GetDuration(key string) time.Duration { return v.GetDuration(key) }
func (v *Viper) GetDuration(key string) time.Duration { func (v *Viper) GetDuration(key string) time.Duration {
return cast.ToDuration(v.Get(key)) return cast.ToDuration(v.Get(key))
} }
// Returns the value associated with the key as a duration or the passed default value
func GetDefaultDuration(key string, defaultValue time.Duration) time.Duration { return v.GetDefaultDuration(key, defaultValue) }
func (v *Viper) GetDefaultDuration(key string, defaultValue time.Duration) time.Duration {
if value := v.Get(key); value != nil {
return cast.ToDuration(value)
}
return defaultValue
}
// Returns the value associated with the key as a slice of strings // Returns the value associated with the key as a slice of strings
func GetStringSlice(key string) []string { return v.GetStringSlice(key) } func GetStringSlice(key string) []string { return v.GetStringSlice(key) }
func (v *Viper) GetStringSlice(key string) []string { func (v *Viper) GetStringSlice(key string) []string {
return cast.ToStringSlice(v.Get(key)) return cast.ToStringSlice(v.Get(key))
} }
// Returns the value associated with the key as a duration or the passed default value
func GetDefaultStringSlice(key string, defaultValue []string) []string { return v.GetDefaultStringSlice(key, defaultValue) }
func (v *Viper) GetDefaultStringSlice(key string, defaultValue []string) []string {
if value := v.Get(key); value != nil {
return cast.ToStringSlice(value)
}
return defaultValue
}
// Returns the value associated with the key as a map of interfaces // Returns the value associated with the key as a map of interfaces
func GetStringMap(key string) map[string]interface{} { return v.GetStringMap(key) } func GetStringMap(key string) map[string]interface{} { return v.GetStringMap(key) }
func (v *Viper) GetStringMap(key string) map[string]interface{} { func (v *Viper) GetStringMap(key string) map[string]interface{} {
return cast.ToStringMap(v.Get(key)) return cast.ToStringMap(v.Get(key))
} }
// Returns the value associated with the key as a map or the passed default value
func GetDefaultStringMap(key string, defaultValue map[string]interface{}) map[string]interface{} { return v.GetDefaultStringMap(key, defaultValue) }
func (v *Viper) GetDefaultStringMap(key string, defaultValue map[string]interface{}) map[string]interface{} {
if value := v.Get(key); value != nil {
return cast.ToStringMap(value)
}
return defaultValue
}
// Returns the value associated with the key as a map of strings // Returns the value associated with the key as a map of strings
func GetStringMapString(key string) map[string]string { return v.GetStringMapString(key) } func GetStringMapString(key string) map[string]string { return v.GetStringMapString(key) }
func (v *Viper) GetStringMapString(key string) map[string]string { func (v *Viper) GetStringMapString(key string) map[string]string {
return cast.ToStringMapString(v.Get(key)) return cast.ToStringMapString(v.Get(key))
} }
// Returns the value associated with the key as a map of strings or the passed default value
func GetDefaultStringMapString(key string, defaultValue map[string]string) map[string]string { return v.GetDefaultStringMapString(key, defaultValue) }
func (v *Viper) GetDefaultStringMapString(key string, defaultValue map[string]string) map[string]string {
if value := v.Get(key); value != nil {
return cast.ToStringMapString(value)
}
return defaultValue
}
// Returns the value associated with the key as a map to a slice of strings. // Returns the value associated with the key as a map to a slice of strings.
func GetStringMapStringSlice(key string) map[string][]string { return v.GetStringMapStringSlice(key) } func GetStringMapStringSlice(key string) map[string][]string { return v.GetStringMapStringSlice(key) }
func (v *Viper) GetStringMapStringSlice(key string) map[string][]string { func (v *Viper) GetStringMapStringSlice(key string) map[string][]string {
return cast.ToStringMapStringSlice(v.Get(key)) return cast.ToStringMapStringSlice(v.Get(key))
} }
// Returns the value associated with the key as a map to a slice of string or the passed default value
func GetDefaultStringMapStringSlice(key string, defaultValue map[string][]string) map[string][]string { return v.GetDefaultStringMapStringSlice(key, defaultValue) }
func (v *Viper) GetDefaultStringMapStringSlice(key string, defaultValue map[string][]string) map[string][]string {
if value := v.Get(key); value != nil {
return cast.ToStringMapStringSlice(value)
}
return defaultValue
}
// Returns the size of the value associated with the given key // Returns the size of the value associated with the given key
// in bytes. // in bytes.
func GetSizeInBytes(key string) uint { return v.GetSizeInBytes(key) } func GetSizeInBytes(key string) uint { return v.GetSizeInBytes(key) }
@ -1005,7 +1104,7 @@ func keyExists(k string, m map[string]interface{}) string {
} }
func castToMapStringInterface( func castToMapStringInterface(
src map[interface{}]interface{}) map[string]interface{} { src map[interface{}]interface{}) map[string]interface{} {
tgt := map[string]interface{}{} tgt := map[string]interface{}{}
for k, v := range src { for k, v := range src {
tgt[fmt.Sprintf("%v", k)] = v tgt[fmt.Sprintf("%v", k)] = v
@ -1019,7 +1118,7 @@ func castToMapStringInterface(
// deep. Both map types are supported as there is a go-yaml fork that uses // deep. Both map types are supported as there is a go-yaml fork that uses
// `map[string]interface{}` instead. // `map[string]interface{}` instead.
func mergeMaps( func mergeMaps(
src, tgt map[string]interface{}, itgt map[interface{}]interface{}) { src, tgt map[string]interface{}, itgt map[interface{}]interface{}) {
for sk, sv := range src { for sk, sv := range src {
tk := keyExists(sk, tgt) tk := keyExists(sk, tgt)
if tk == "" { if tk == "" {