Uint Support (#681)

* add GetUint/GetUint32/GetUint64

* Add Get(string) support for uint.
This commit is contained in:
Mitch Connors 2019-04-08 07:06:45 -07:00 committed by Steve Francia
parent fccfc2c271
commit 7a605a50e6
2 changed files with 37 additions and 0 deletions

View file

@ -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 {

View file

@ -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))
}