add default for all type

This commit is contained in:
victor 2022-09-16 21:35:05 +08:00
parent ed7da26bb0
commit fa3ca0f752

130
viper.go
View file

@ -511,6 +511,7 @@ func (v *Viper) searchMapWithPathPrefixes(source map[string]interface{}, path []
// isPathShadowedInDeepMap makes sure the given path is not shadowed somewhere // isPathShadowedInDeepMap makes sure the given path is not shadowed somewhere
// on its path in the map. // on its path in the map.
// e.g., if "foo.bar" has a value in the given map, it “shadows” // e.g., if "foo.bar" has a value in the given map, it “shadows”
//
// "foo.bar.baz" in a lower-priority map // "foo.bar.baz" in a lower-priority map
func (v *Viper) isPathShadowedInDeepMap(path []string, m map[string]interface{}) string { func (v *Viper) isPathShadowedInDeepMap(path []string, m map[string]interface{}) string {
var parentVal interface{} var parentVal interface{}
@ -536,6 +537,7 @@ func (v *Viper) isPathShadowedInDeepMap(path []string, m map[string]interface{})
// isPathShadowedInFlatMap makes sure the given path is not shadowed somewhere // isPathShadowedInFlatMap makes sure the given path is not shadowed somewhere
// in a sub-path of the map. // in a sub-path of the map.
// e.g., if "foo.bar" has a value in the given map, it “shadows” // e.g., if "foo.bar" has a value in the given map, it “shadows”
//
// "foo.bar.baz" in a lower-priority map // "foo.bar.baz" in a lower-priority map
func (v *Viper) isPathShadowedInFlatMap(path []string, mi interface{}) string { func (v *Viper) isPathShadowedInFlatMap(path []string, mi interface{}) string {
// unify input map // unify input map
@ -561,6 +563,7 @@ func (v *Viper) isPathShadowedInFlatMap(path []string, mi interface{}) string {
// isPathShadowedInAutoEnv makes sure the given path is not shadowed somewhere // isPathShadowedInAutoEnv makes sure the given path is not shadowed somewhere
// in the environment, when automatic env is on. // in the environment, when automatic env is on.
// e.g., if "foo.bar" has a value in the environment, it “shadows” // e.g., if "foo.bar" has a value in the environment, it “shadows”
//
// "foo.bar.baz" in a lower-priority map // "foo.bar.baz" in a lower-priority map
func (v *Viper) isPathShadowedInAutoEnv(path []string) string { func (v *Viper) isPathShadowedInAutoEnv(path []string) string {
var parentKey string var parentKey string
@ -695,11 +698,14 @@ func (v *Viper) Sub(key string) *Viper {
} }
// GetString returns the value associated with the key as a string. // GetString returns the value associated with the key as a string.
func GetString(key string) string { return v.GetString(key) } func GetString(key string, defaultValue string) string { return v.GetString(key, defaultValue) }
// GetString returns the value associated with the key as a string. // GetString returns the value associated with the key as a string.
func (v *Viper) GetString(key string) string { func (v *Viper) GetString(key string, defaultValue string) string {
if v.IsSet(key) {
return cast.ToString(v.Get(key)) return cast.ToString(v.Get(key))
}
return defaultValue
} }
// GetBool returns the value associated with the key as a boolean. // GetBool returns the value associated with the key as a boolean.
@ -711,126 +717,181 @@ func (v *Viper) GetBool(key string) bool {
} }
// GetInt returns the value associated with the key as an integer. // GetInt returns the value associated with the key as an integer.
func GetInt(key string) int { return v.GetInt(key) } func GetInt(key string, defaultValue int) int { return v.GetInt(key, defaultValue) }
// GetInt returns the value associated with the key as an integer. // GetInt returns the value associated with the key as an integer.
func (v *Viper) GetInt(key string) int { func (v *Viper) GetInt(key string, defaultValue int) int {
if v.IsSet(key) {
return cast.ToInt(v.Get(key)) return cast.ToInt(v.Get(key))
}
return defaultValue
} }
// GetInt32 returns the value associated with the key as an integer. // GetInt32 returns the value associated with the key as an integer.
func GetInt32(key string) int32 { return v.GetInt32(key) } func GetInt32(key string, defaultValue int32) int32 { return v.GetInt32(key, defaultValue) }
// GetInt32 returns the value associated with the key as an integer. // GetInt32 returns the value associated with the key as an integer.
func (v *Viper) GetInt32(key string) int32 { func (v *Viper) GetInt32(key string, defaultValue int32) int32 {
if v.IsSet(key) {
return cast.ToInt32(v.Get(key)) return cast.ToInt32(v.Get(key))
}
return defaultValue
} }
// GetInt64 returns the value associated with the key as an integer. // GetInt64 returns the value associated with the key as an integer.
func GetInt64(key string) int64 { return v.GetInt64(key) } func GetInt64(key string, defaultValue int64) int64 { return v.GetInt64(key, defaultValue) }
// GetInt64 returns the value associated with the key as an integer. // GetInt64 returns the value associated with the key as an integer.
func (v *Viper) GetInt64(key string) int64 { func (v *Viper) GetInt64(key string, defaultValue int64) int64 {
if v.IsSet(key) {
return cast.ToInt64(v.Get(key)) return cast.ToInt64(v.Get(key))
}
return defaultValue
} }
// GetUint returns the value associated with the key as an unsigned integer. // GetUint returns the value associated with the key as an unsigned integer.
func GetUint(key string) uint { return v.GetUint(key) } func GetUint(key string, defaultValue uint) uint { return v.GetUint(key, defaultValue) }
// GetUint returns the value associated with the key as an unsigned integer. // GetUint returns the value associated with the key as an unsigned integer.
func (v *Viper) GetUint(key string) uint { func (v *Viper) GetUint(key string, defaultValue uint) uint {
if v.IsSet(key) {
return cast.ToUint(v.Get(key)) return cast.ToUint(v.Get(key))
}
return defaultValue
} }
// GetUint32 returns the value associated with the key as an unsigned integer. // GetUint32 returns the value associated with the key as an unsigned integer.
func GetUint32(key string) uint32 { return v.GetUint32(key) } func GetUint32(key string, defaultValue uint32) uint32 { return v.GetUint32(key, defaultValue) }
// GetUint32 returns the value associated with the key as an unsigned integer. // GetUint32 returns the value associated with the key as an unsigned integer.
func (v *Viper) GetUint32(key string) uint32 { func (v *Viper) GetUint32(key string, defaultValue uint32) uint32 {
if v.IsSet(key) {
return cast.ToUint32(v.Get(key)) return cast.ToUint32(v.Get(key))
}
return defaultValue
} }
// GetUint64 returns the value associated with the key as an unsigned integer. // GetUint64 returns the value associated with the key as an unsigned integer.
func GetUint64(key string) uint64 { return v.GetUint64(key) } func GetUint64(key string, defaultValue uint64) uint64 { return v.GetUint64(key, defaultValue) }
// GetUint64 returns the value associated with the key as an unsigned integer. // GetUint64 returns the value associated with the key as an unsigned integer.
func (v *Viper) GetUint64(key string) uint64 { func (v *Viper) GetUint64(key string, defaultValue uint64) uint64 {
if v.IsSet(key) {
return cast.ToUint64(v.Get(key)) return cast.ToUint64(v.Get(key))
}
return defaultValue
} }
// GetFloat64 returns the value associated with the key as a float64. // GetFloat64 returns the value associated with the key as a float64.
func GetFloat64(key string) float64 { return v.GetFloat64(key) } func GetFloat64(key string, defaultValue float64) float64 { return v.GetFloat64(key, defaultValue) }
// GetFloat64 returns the value associated with the key as a float64. // GetFloat64 returns the value associated with the key as a float64.
func (v *Viper) GetFloat64(key string) float64 { func (v *Viper) GetFloat64(key string, defaultValue float64) float64 {
if v.IsSet(key) {
return cast.ToFloat64(v.Get(key)) return cast.ToFloat64(v.Get(key))
}
return defaultValue
} }
// GetTime returns the value associated with the key as time. // GetTime returns the value associated with the key as time.
func GetTime(key string) time.Time { return v.GetTime(key) } func GetTime(key string, defaultValue time.Time) time.Time { return v.GetTime(key, defaultValue) }
// GetTime returns the value associated with the key as time. // GetTime returns the value associated with the key as time.
func (v *Viper) GetTime(key string) time.Time { func (v *Viper) GetTime(key string, defaultValue time.Time) time.Time {
if v.IsSet(key) {
return cast.ToTime(v.Get(key)) return cast.ToTime(v.Get(key))
}
return defaultValue
} }
// GetDuration returns the value associated with the key as a duration. // GetDuration returns the value associated with the key as a duration.
func GetDuration(key string) time.Duration { return v.GetDuration(key) } func GetDuration(key string, defaultValue time.Duration) time.Duration {
return v.GetDuration(key, defaultValue)
}
// GetDuration returns the value associated with the key as a duration. // GetDuration returns the value associated with the key as a duration.
func (v *Viper) GetDuration(key string) time.Duration { func (v *Viper) GetDuration(key string, defaultValue time.Duration) time.Duration {
if v.IsSet(key) {
return cast.ToDuration(v.Get(key)) return cast.ToDuration(v.Get(key))
}
return defaultValue
} }
// GetIntSlice returns the value associated with the key as a slice of int values. // GetIntSlice returns the value associated with the key as a slice of int values.
func GetIntSlice(key string) []int { return v.GetIntSlice(key) } func GetIntSlice(key string, defaultValue []int) []int { return v.GetIntSlice(key, defaultValue) }
// GetIntSlice returns the value associated with the key as a slice of int values. // GetIntSlice returns the value associated with the key as a slice of int values.
func (v *Viper) GetIntSlice(key string) []int { func (v *Viper) GetIntSlice(key string, defaultValue []int) []int {
if v.IsSet(key) {
return cast.ToIntSlice(v.Get(key)) return cast.ToIntSlice(v.Get(key))
}
return defaultValue
} }
// GetStringSlice returns the value associated with the key as a slice of strings. // GetStringSlice 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, defaultValue []string) []string {
return v.GetStringSlice(key, defaultValue)
}
// GetStringSlice returns the value associated with the key as a slice of strings. // GetStringSlice returns the value associated with the key as a slice of strings.
func (v *Viper) GetStringSlice(key string) []string { func (v *Viper) GetStringSlice(key string, defaultValue []string) []string {
if v.IsSet(key) {
return cast.ToStringSlice(v.Get(key)) return cast.ToStringSlice(v.Get(key))
}
return defaultValue
} }
// GetStringMap returns the value associated with the key as a map of interfaces. // GetStringMap 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, defaultValue map[string]interface{}) map[string]interface{} {
return v.GetStringMap(key, defaultValue)
}
// GetStringMap returns the value associated with the key as a map of interfaces. // GetStringMap returns the value associated with the key as a map of interfaces.
func (v *Viper) GetStringMap(key string) map[string]interface{} { func (v *Viper) GetStringMap(key string, defaultValue map[string]interface{}) map[string]interface{} {
if v.IsSet(key) {
return cast.ToStringMap(v.Get(key)) return cast.ToStringMap(v.Get(key))
}
return defaultValue
} }
// GetStringMapString returns the value associated with the key as a map of strings. // GetStringMapString 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, defaultValue map[string]string) map[string]string {
return v.GetStringMapString(key, defaultValue)
}
// GetStringMapString returns the value associated with the key as a map of strings. // GetStringMapString returns the value associated with the key as a map of strings.
func (v *Viper) GetStringMapString(key string) map[string]string { func (v *Viper) GetStringMapString(key string, defaultValue map[string]string) map[string]string {
if v.IsSet(key) {
return cast.ToStringMapString(v.Get(key)) return cast.ToStringMapString(v.Get(key))
}
return defaultValue
} }
// GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings. // GetStringMapStringSlice 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, defaultValue map[string][]string) map[string][]string {
return v.GetStringMapStringSlice(key, defaultValue)
}
// GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings. // GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings.
func (v *Viper) GetStringMapStringSlice(key string) map[string][]string { func (v *Viper) GetStringMapStringSlice(key string, defaultValue map[string][]string) map[string][]string {
if v.IsSet(key) {
return cast.ToStringMapStringSlice(v.Get(key)) return cast.ToStringMapStringSlice(v.Get(key))
}
return defaultValue
} }
// GetSizeInBytes returns the size of the value associated with the given key // GetSizeInBytes 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, defaultValue uint) uint { return v.GetSizeInBytes(key, defaultValue) }
// GetSizeInBytes returns the size of the value associated with the given key // GetSizeInBytes returns the size of the value associated with the given key
// in bytes. // in bytes.
func (v *Viper) GetSizeInBytes(key string) uint { func (v *Viper) GetSizeInBytes(key string, defaultValue uint) uint {
if v.IsSet(key) {
sizeStr := cast.ToString(v.Get(key)) sizeStr := cast.ToString(v.Get(key))
return parseSizeInBytes(sizeStr) return parseSizeInBytes(sizeStr)
}
return defaultValue
} }
// UnmarshalKey takes a single key and unmarshals it into a Struct. // UnmarshalKey takes a single key and unmarshals it into a Struct.
@ -912,7 +973,6 @@ func (v *Viper) BindPFlags(flags *pflag.FlagSet) error {
// //
// serverCmd.Flags().Int("port", 1138, "Port to run Application server on") // serverCmd.Flags().Int("port", 1138, "Port to run Application server on")
// Viper.BindPFlag("port", serverCmd.Flags().Lookup("port")) // Viper.BindPFlag("port", serverCmd.Flags().Lookup("port"))
//
func BindPFlag(key string, flag *pflag.Flag) error { return v.BindPFlag(key, flag) } func BindPFlag(key string, flag *pflag.Flag) error { return v.BindPFlag(key, flag) }
// BindPFlag binds a specific key to a pflag (as used by cobra). // BindPFlag binds a specific key to a pflag (as used by cobra).
@ -920,7 +980,6 @@ func BindPFlag(key string, flag *pflag.Flag) error { return v.BindPFlag(key, fla
// //
// serverCmd.Flags().Int("port", 1138, "Port to run Application server on") // serverCmd.Flags().Int("port", 1138, "Port to run Application server on")
// Viper.BindPFlag("port", serverCmd.Flags().Lookup("port")) // Viper.BindPFlag("port", serverCmd.Flags().Lookup("port"))
//
func (v *Viper) BindPFlag(key string, flag *pflag.Flag) error { func (v *Viper) BindPFlag(key string, flag *pflag.Flag) error {
return v.BindFlagValue(key, pflagValue{flag}) return v.BindFlagValue(key, pflagValue{flag})
} }
@ -945,7 +1004,6 @@ func (v *Viper) BindFlagValues(flags FlagValueSet) (err error) {
// //
// serverCmd.Flags().Int("port", 1138, "Port to run Application server on") // serverCmd.Flags().Int("port", 1138, "Port to run Application server on")
// Viper.BindFlagValue("port", serverCmd.Flags().Lookup("port")) // Viper.BindFlagValue("port", serverCmd.Flags().Lookup("port"))
//
func BindFlagValue(key string, flag FlagValue) error { return v.BindFlagValue(key, flag) } func BindFlagValue(key string, flag FlagValue) error { return v.BindFlagValue(key, flag) }
// BindFlagValue binds a specific key to a FlagValue. // BindFlagValue binds a specific key to a FlagValue.
@ -953,7 +1011,6 @@ func BindFlagValue(key string, flag FlagValue) error { return v.BindFlagValue(ke
// //
// serverCmd.Flags().Int("port", 1138, "Port to run Application server on") // serverCmd.Flags().Int("port", 1138, "Port to run Application server on")
// Viper.BindFlagValue("port", serverCmd.Flags().Lookup("port")) // Viper.BindFlagValue("port", serverCmd.Flags().Lookup("port"))
//
func (v *Viper) BindFlagValue(key string, flag FlagValue) error { func (v *Viper) BindFlagValue(key string, flag FlagValue) error {
if flag == nil { if flag == nil {
return fmt.Errorf("flag for %q is nil", key) return fmt.Errorf("flag for %q is nil", key)
@ -1661,6 +1718,7 @@ func (v *Viper) AllKeys() []string {
// - each path is merged into a single key string, delimited with v.keyDelim (= ".") // - each path is merged into a single key string, delimited with v.keyDelim (= ".")
// - if a path is shadowed by an earlier value in the initial shadow map, // - if a path is shadowed by an earlier value in the initial shadow map,
// it is skipped. // it is skipped.
//
// The resulting set of paths is merged to the given shadow set at the same time. // The resulting set of paths is merged to the given shadow set at the same time.
func (v *Viper) flattenAndMergeMap(shadow map[string]bool, m map[string]interface{}, prefix string) map[string]bool { func (v *Viper) flattenAndMergeMap(shadow map[string]bool, m map[string]interface{}, prefix string) map[string]bool {
if shadow != nil && prefix != "" && shadow[prefix] { if shadow != nil && prefix != "" && shadow[prefix] {