Skip to content

Commit

Permalink
Uint Support (#681)
Browse files Browse the repository at this point in the history
* add GetUint/GetUint32/GetUint64

* Add Get(string) support for uint.
  • Loading branch information
therealmitchconnors authored and spf13 committed Apr 8, 2019
1 parent fccfc2c commit 7a605a5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
24 changes: 24 additions & 0 deletions viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,7 @@ var yamlMergeExampleTgt = []byte(`
hello:
pop: 37890
lagrenum: 765432101234567
num2pow63: 9223372036854775808
world:
- us
- uk
Expand Down Expand Up @@ -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))
}
Expand Down

0 comments on commit 7a605a5

Please sign in to comment.