Skip to content

Commit 67219c7

Browse files
committed
Adds support for uint16 with GetUint16
We have encountered numerous places where it is convenient to have viper return a `uint16` value, especially in combination with the new `netip` package that represents a port correctly as `uint16` rather than just an `int`. cobra already supports this, but we need a conversion from the existing `GetUint` method in viper.
1 parent 5247643 commit 67219c7

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

viper.go

+7
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,13 @@ func (v *Viper) GetUint(key string) uint {
988988
return cast.ToUint(v.Get(key))
989989
}
990990

991+
// GetUint16 returns the value associated with the key as an unsigned integer.
992+
func GetUint16(key string) uint16 { return v.GetUint16(key) }
993+
994+
func (v *Viper) GetUint16(key string) uint16 {
995+
return cast.ToUint16(v.Get(key))
996+
}
997+
991998
// GetUint32 returns the value associated with the key as an unsigned integer.
992999
func GetUint32(key string) uint32 { return v.GetUint32(key) }
9931000

viper_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -1953,6 +1953,10 @@ func TestMergeConfig(t *testing.T) {
19531953
t.Fatalf("uint pop != 37890, = %d", pop)
19541954
}
19551955

1956+
if pop := v.GetUint16("hello.pop"); pop != uint16(37890) {
1957+
t.Fatalf("uint pop != 37890, = %d", pop)
1958+
}
1959+
19561960
if pop := v.GetUint32("hello.pop"); pop != 37890 {
19571961
t.Fatalf("uint32 pop != 37890, = %d", pop)
19581962
}

0 commit comments

Comments
 (0)