From dc26328fb59c973c41378e8074c05e3327afe68e Mon Sep 17 00:00:00 2001 From: aaronschweig Date: Thu, 9 Oct 2025 14:29:40 +0200 Subject: [PATCH 1/2] feat(config): add support for string slice flags in traverseStruct --- config/config.go | 5 +++++ config/config_test.go | 1 + 2 files changed, 6 insertions(+) diff --git a/config/config.go b/config/config.go index 5260f4b..6d327a5 100644 --- a/config/config.go +++ b/config/config.go @@ -166,6 +166,11 @@ func traverseStruct(value reflect.Value, flagSet *pflag.FlagSet, prefix string) defaultBoolValue = b } flagSet.Bool(prefix+tag, defaultBoolValue, description) + case reflect.Slice: + if fieldValue.Type().Elem().Kind() != reflect.String { + return fmt.Errorf("unsupported slice element type %s for field %s", fieldValue.Type().Elem().Kind(), field.Name) + } + flagSet.StringSlice(prefix+tag, []string{}, description) default: return fmt.Errorf("unsupported field type %s for field %s", fieldValue.Kind(), field.Name) } diff --git a/config/config_test.go b/config/config_test.go index 86812c3..03eb708 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -36,6 +36,7 @@ func TestBindConfigToFlags(t *testing.T) { CustomFlagStruct2 struct { CustomFlagDuration time.Duration `mapstructure:"custom-flag-duration-2"` } `mapstructure:"le-strFlag"` + Slice []string `mapstructure:"slice" description:"This is a slice of strings"` } testStruct := test{} From 19325e70011b53fe122f2bfe6fce10935f2a2c2d Mon Sep 17 00:00:00 2001 From: aaronschweig Date: Thu, 9 Oct 2025 14:30:55 +0200 Subject: [PATCH 2/2] chore: fix test --- config/config_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config_test.go b/config/config_test.go index 03eb708..5967097 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -172,7 +172,7 @@ func TestNewDefaultConfig(t *testing.T) { func TestGenerateFlagSetUnsupportedType(t *testing.T) { type test struct { - UnsupportedField []string `mapstructure:"unsupported-field"` + UnsupportedField []int `mapstructure:"unsupported-field"` } testStruct := test{} err := config.BindConfigToFlags(viper.New(), &cobra.Command{}, &testStruct)