diff --git a/viper.go b/viper.go index 9e8cbec..a3d37f8 100644 --- a/viper.go +++ b/viper.go @@ -689,6 +689,12 @@ func (v *Viper) Get(key string) interface{} { return cast.ToString(val) case int32, int16, int8, int: return cast.ToInt(val) + case uint: + return cast.ToUint(val) + case uint32: + return cast.ToUint32(val) + case uint64: + return cast.ToUint64(val) case int64: return cast.ToInt64(val) case float64, float32: @@ -752,6 +758,24 @@ func (v *Viper) GetInt64(key string) int64 { return cast.ToInt64(v.Get(key)) } +// GetUint returns the value associated with the key as an unsigned integer. +func GetUint(key string) uint { return v.GetUint(key) } +func (v *Viper) GetUint(key string) uint { + return cast.ToUint(v.Get(key)) +} + +// GetUint32 returns the value associated with the key as an unsigned integer. +func GetUint32(key string) uint32 { return v.GetUint32(key) } +func (v *Viper) GetUint32(key string) uint32 { + return cast.ToUint32(v.Get(key)) +} + +// GetUint64 returns the value associated with the key as an unsigned integer. +func GetUint64(key string) uint64 { return v.GetUint64(key) } +func (v *Viper) GetUint64(key string) uint64 { + return cast.ToUint64(v.Get(key)) +} + // GetFloat64 returns the value associated with the key as a float64. func GetFloat64(key string) float64 { return v.GetFloat64(key) } func (v *Viper) GetFloat64(key string) float64 { diff --git a/viper_test.go b/viper_test.go index f4263d3..f43a38b 100644 --- a/viper_test.go +++ b/viper_test.go @@ -1117,6 +1117,7 @@ var yamlMergeExampleTgt = []byte(` hello: pop: 37890 lagrenum: 765432101234567 + num2pow63: 9223372036854775808 world: - us - uk @@ -1153,6 +1154,18 @@ func TestMergeConfig(t *testing.T) { t.Fatalf("int64 lagrenum != 765432101234567, = %d", pop) } + if pop := v.GetUint("hello.pop"); pop != 37890 { + t.Fatalf("uint pop != 37890, = %d", pop) + } + + if pop := v.GetUint32("hello.pop"); pop != 37890 { + t.Fatalf("uint32 pop != 37890, = %d", pop) + } + + if pop := v.GetUint64("hello.num2pow63"); pop != 9223372036854775808 { + t.Fatalf("uint64 num2pow63 != 9223372036854775808, = %d", pop) + } + if world := v.GetStringSlice("hello.world"); len(world) != 4 { t.Fatalf("len(world) != 4, = %d", len(world)) }