diff --git a/cli/pflags/api/generator.go b/cli/pflags/api/generator.go index 80b92dc..60649c2 100644 --- a/cli/pflags/api/generator.go +++ b/cli/pflags/api/generator.go @@ -342,6 +342,9 @@ func discoverFieldsRecursive(ctx context.Context, workingDirPkg string, typ *typ case *types.Slice: logger.Infof(ctx, "[%v] is of a slice type with default value [%v].", tag.Name, tag.DefaultValue) defaultValue := tag.DefaultValue + if len(defaultValueAccessor) > 0 { + defaultValue = appendAccessors(defaultValueAccessor, fieldPath, variable.Name()) + } f, err := buildFieldForSlice(logger.WithIndent(ctx, indent), t, tag.Name, variable.Name(), tag.Usage, defaultValue, bindDefaultVar) if err != nil { diff --git a/cli/pflags/api/testdata/testtype.go b/cli/pflags/api/testdata/testtype.go index 2d72c5c..4a86fc1 100755 --- a/cli/pflags/api/testdata/testtype.go +++ b/cli/pflags/api/testdata/testtype.go @@ -53,9 +53,9 @@ func (cfg TestType) GetPFlagSet(prefix string) *pflag.FlagSet { cmdFlags.String(fmt.Sprintf("%v%v", prefix, "str"), DefaultTestType.StringValue, "life is short") cmdFlags.Bool(fmt.Sprintf("%v%v", prefix, "bl"), DefaultTestType.BoolValue, "") cmdFlags.Int(fmt.Sprintf("%v%v", prefix, "nested.i"), DefaultTestType.NestedType.IntValue, "this is an important flag") - cmdFlags.IntSlice(fmt.Sprintf("%v%v", prefix, "ints"), []int{12, 1}, "") - cmdFlags.StringSlice(fmt.Sprintf("%v%v", prefix, "strs"), []string{"12", "1"}, "") - cmdFlags.StringSlice(fmt.Sprintf("%v%v", prefix, "complexArr"), []string{}, "") + cmdFlags.IntSlice(fmt.Sprintf("%v%v", prefix, "ints"), DefaultTestType.IntArray, "") + cmdFlags.StringSlice(fmt.Sprintf("%v%v", prefix, "strs"), DefaultTestType.StringArray, "") + cmdFlags.StringSlice(fmt.Sprintf("%v%v", prefix, "complexArr"), DefaultTestType.ComplexJSONArray, "") cmdFlags.String(fmt.Sprintf("%v%v", prefix, "c"), DefaultTestType.mustJsonMarshal(DefaultTestType.StringToJSON), "I'm a complex type but can be converted from string.") cmdFlags.String(fmt.Sprintf("%v%v", prefix, "storage.type"), DefaultTestType.StorageConfig.Type, "Sets the type of storage to configure [s3/minio/local/mem/stow].") cmdFlags.String(fmt.Sprintf("%v%v", prefix, "storage.connection.endpoint"), DefaultTestType.StorageConfig.Connection.Endpoint.String(), "URL for storage client to connect to.") diff --git a/cli/pflags/api/testdata/testtype_bind.go b/cli/pflags/api/testdata/testtype_bind.go index 12be5bd..e12dc05 100755 --- a/cli/pflags/api/testdata/testtype_bind.go +++ b/cli/pflags/api/testdata/testtype_bind.go @@ -53,9 +53,9 @@ func (cfg TestType) GetPFlagSet(prefix string) *pflag.FlagSet { cmdFlags.StringVar(&DefaultTestType.StringValue, fmt.Sprintf("%v%v", prefix, "str"), DefaultTestType.StringValue, "life is short") cmdFlags.BoolVar(&DefaultTestType.BoolValue, fmt.Sprintf("%v%v", prefix, "bl"), DefaultTestType.BoolValue, "") cmdFlags.IntVar(&DefaultTestType.NestedType.IntValue, fmt.Sprintf("%v%v", prefix, "nested.i"), DefaultTestType.NestedType.IntValue, "this is an important flag") - cmdFlags.IntSliceVar(&[]int{12, 1}, fmt.Sprintf("%v%v", prefix, "ints"), []int{12, 1}, "") - cmdFlags.StringSliceVar(&[]string{"12", "1"}, fmt.Sprintf("%v%v", prefix, "strs"), []string{"12", "1"}, "") - cmdFlags.StringSliceVar(&[]string{}, fmt.Sprintf("%v%v", prefix, "complexArr"), []string{}, "") + cmdFlags.IntSliceVar(&DefaultTestType.IntArray, fmt.Sprintf("%v%v", prefix, "ints"), DefaultTestType.IntArray, "") + cmdFlags.StringSliceVar(&DefaultTestType.StringArray, fmt.Sprintf("%v%v", prefix, "strs"), DefaultTestType.StringArray, "") + cmdFlags.StringSliceVar(&DefaultTestType.ComplexJSONArray, fmt.Sprintf("%v%v", prefix, "complexArr"), DefaultTestType.ComplexJSONArray, "") cmdFlags.String(fmt.Sprintf("%v%v", prefix, "c"), DefaultTestType.mustJsonMarshal(DefaultTestType.StringToJSON), "I'm a complex type but can be converted from string.") cmdFlags.StringVar(&DefaultTestType.StorageConfig.Type, fmt.Sprintf("%v%v", prefix, "storage.type"), DefaultTestType.StorageConfig.Type, "Sets the type of storage to configure [s3/minio/local/mem/stow].") cmdFlags.Var(&DefaultTestType.StorageConfig.Connection.Endpoint, fmt.Sprintf("%v%v", prefix, "storage.connection.endpoint"), "URL for storage client to connect to.") diff --git a/cli/pflags/api/testdata/testtype_bind_test.go b/cli/pflags/api/testdata/testtype_bind_test.go index ea5b38e..6aeacda 100755 --- a/cli/pflags/api/testdata/testtype_bind_test.go +++ b/cli/pflags/api/testdata/testtype_bind_test.go @@ -144,7 +144,7 @@ func TestTestType_SetFlags(t *testing.T) { t.Run("Test_ints", func(t *testing.T) { t.Run("Override", func(t *testing.T) { - testValue := join_TestType([]int{12, 1}, ",") + testValue := join_TestType(DefaultTestType.IntArray, ",") cmdFlags.Set("ints", testValue) if vIntSlice, err := cmdFlags.GetIntSlice("ints"); err == nil { @@ -158,7 +158,7 @@ func TestTestType_SetFlags(t *testing.T) { t.Run("Test_strs", func(t *testing.T) { t.Run("Override", func(t *testing.T) { - testValue := join_TestType([]string{"12", "1"}, ",") + testValue := join_TestType(DefaultTestType.StringArray, ",") cmdFlags.Set("strs", testValue) if vStringSlice, err := cmdFlags.GetStringSlice("strs"); err == nil { @@ -172,7 +172,7 @@ func TestTestType_SetFlags(t *testing.T) { t.Run("Test_complexArr", func(t *testing.T) { t.Run("Override", func(t *testing.T) { - testValue := "1,1" + testValue := DefaultTestType.ComplexJSONArray cmdFlags.Set("complexArr", testValue) if vStringSlice, err := cmdFlags.GetStringSlice("complexArr"); err == nil { diff --git a/cli/pflags/api/testdata/testtype_test.go b/cli/pflags/api/testdata/testtype_test.go index 4d8611d..f59d0a2 100755 --- a/cli/pflags/api/testdata/testtype_test.go +++ b/cli/pflags/api/testdata/testtype_test.go @@ -144,7 +144,7 @@ func TestTestType_SetFlags(t *testing.T) { t.Run("Test_ints", func(t *testing.T) { t.Run("Override", func(t *testing.T) { - testValue := join_TestType([]int{12, 1}, ",") + testValue := join_TestType(DefaultTestType.IntArray, ",") cmdFlags.Set("ints", testValue) if vIntSlice, err := cmdFlags.GetIntSlice("ints"); err == nil { @@ -158,7 +158,7 @@ func TestTestType_SetFlags(t *testing.T) { t.Run("Test_strs", func(t *testing.T) { t.Run("Override", func(t *testing.T) { - testValue := join_TestType([]string{"12", "1"}, ",") + testValue := join_TestType(DefaultTestType.StringArray, ",") cmdFlags.Set("strs", testValue) if vStringSlice, err := cmdFlags.GetStringSlice("strs"); err == nil { @@ -172,7 +172,7 @@ func TestTestType_SetFlags(t *testing.T) { t.Run("Test_complexArr", func(t *testing.T) { t.Run("Override", func(t *testing.T) { - testValue := "1,1" + testValue := DefaultTestType.ComplexJSONArray cmdFlags.Set("complexArr", testValue) if vStringSlice, err := cmdFlags.GetStringSlice("complexArr"); err == nil {