Skip to content

Commit ccfd0c1

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 97591f0 commit ccfd0c1

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
@@ -991,6 +991,13 @@ func (v *Viper) GetUint(key string) uint {
991991
return cast.ToUint(v.Get(key))
992992
}
993993

994+
// GetUint16 returns the value associated with the key as an unsigned integer.
995+
func GetUint16(key string) uint16 { return v.GetUint16(key) }
996+
997+
func (v *Viper) GetUint16(key string) uint16 {
998+
return cast.ToUint16(v.Get(key))
999+
}
1000+
9941001
// GetUint32 returns the value associated with the key as an unsigned integer.
9951002
func GetUint32(key string) uint32 { return v.GetUint32(key) }
9961003

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)