Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,14 @@ func (v *Viper) GetIntSlice(key string) []int {
return cast.ToIntSlice(v.Get(key))
}

// GetInt64Slice returns the value associated with the key as a slice of int64 values.
func GetInt64Slice(key string) []int64 { return v.GetInt64Slice(key) }

// GetInt64Slice returns the value associated with the key as a slice of int64 values.
func (v *Viper) GetInt64Slice(key string) []int64 {
return cast.ToInt64Slice(v.Get(key))
}

// GetStringSlice returns the value associated with the key as a slice of strings.
func GetStringSlice(key string) []string { return v.GetStringSlice(key) }

Expand Down Expand Up @@ -1241,6 +1249,11 @@ func (v *Viper) find(lcaseKey string, flagDefault bool) any {
s = strings.TrimSuffix(s, "]")
res, _ := readAsCSV(s)
return cast.ToIntSlice(res)
case "int64Slice":
s := strings.TrimPrefix(flag.ValueString(), "[")
s = strings.TrimSuffix(s, "]")
res, _ := readAsCSV(s)
return cast.ToInt64Slice(res)
case "uintSlice":
s := strings.TrimPrefix(flag.ValueString(), "[")
s = strings.TrimSuffix(s, "]")
Expand Down Expand Up @@ -1343,6 +1356,11 @@ func (v *Viper) find(lcaseKey string, flagDefault bool) any {
s = strings.TrimSuffix(s, "]")
res, _ := readAsCSV(s)
return cast.ToIntSlice(res)
case "int64Slice":
s := strings.TrimPrefix(flag.ValueString(), "[")
s = strings.TrimSuffix(s, "]")
res, _ := readAsCSV(s)
return cast.ToInt64Slice(res)
case "uintSlice":
s := strings.TrimPrefix(flag.ValueString(), "[")
s = strings.TrimSuffix(s, "]")
Expand Down
36 changes: 36 additions & 0 deletions viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,42 @@ func TestBindPFlagsIntSlice(t *testing.T) {
}
}

func TestBindPFlagsInt64Slice(t *testing.T) {
tests := []struct {
Expected []int64
Value string
}{
{[]int64{}, ""},
{[]int64{1}, "1"},
{[]int64{2, 3}, "2,3"},
}

v := New() // create independent Viper object
defaultVal := []int64{0}
v.SetDefault("int64slice", defaultVal)

for _, testValue := range tests {
flagSet := pflag.NewFlagSet("test", pflag.ContinueOnError)
flagSet.Int64Slice("int64slice", testValue.Expected, "test")

for _, changed := range []bool{true, false} {
flagSet.VisitAll(func(f *pflag.Flag) {
f.Value.Set(testValue.Value)
f.Changed = changed
})

err := v.BindPFlags(flagSet)
require.NoError(t, err, "error binding flag set")

if changed {
assert.Equal(t, testValue.Expected, v.GetInt64Slice("int64slice"))
} else {
assert.Equal(t, defaultVal, v.GetInt64Slice("int64slice"))
}
}
}
}

func TestBindPFlag(t *testing.T) {
v := New()
testString := "testing"
Expand Down