Skip to content

Commit c584cca

Browse files
authored
chore: slice defaulting (#87)
1 parent 668904d commit c584cca

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

config/config.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,14 @@ func traverseStruct(value reflect.Value, flagSet *pflag.FlagSet, prefix string)
167167
}
168168
flagSet.Bool(prefix+tag, defaultBoolValue, description)
169169
case reflect.Slice:
170+
var defaultSliceValue []string
171+
if defaultStrValue != "" {
172+
defaultSliceValue = strings.Split(defaultStrValue, ",")
173+
}
170174
if fieldValue.Type().Elem().Kind() != reflect.String {
171175
return fmt.Errorf("unsupported slice element type %s for field %s", fieldValue.Type().Elem().Kind(), field.Name)
172176
}
173-
flagSet.StringSlice(prefix+tag, []string{}, description)
177+
flagSet.StringSlice(prefix+tag, defaultSliceValue, description)
174178
default:
175179
return fmt.Errorf("unsupported field type %s for field %s", fieldValue.Kind(), field.Name)
176180
}

config/config_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestBindConfigToFlags(t *testing.T) {
3636
CustomFlagStruct2 struct {
3737
CustomFlagDuration time.Duration `mapstructure:"custom-flag-duration-2"`
3838
} `mapstructure:"le-strFlag"`
39-
Slice []string `mapstructure:"slice" description:"This is a slice of strings"`
39+
Slice []string `mapstructure:"slice" description:"This is a slice of strings" default:"one,two,three"`
4040
}
4141

4242
testStruct := test{}
@@ -75,6 +75,10 @@ func TestBindConfigToFlags(t *testing.T) {
7575
assert.NotNil(t, durationFlag)
7676
assert.Equal(t, "This is a custom flag with duration value", durationFlag.Usage)
7777
assert.Equal(t, "1m0s", durationFlag.DefValue)
78+
79+
sliceFlag := cmd.Flags().Lookup("slice")
80+
assert.NotNil(t, sliceFlag)
81+
assert.Equal(t, "[one,two,three]", sliceFlag.DefValue)
7882
}
7983

8084
func TestBindConfigToFlagsWrongTypeInt(t *testing.T) {

context/context.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ const DefaultShutdownTimeout = 3 * time.Second
1818
type ShutdownTimeoutKey struct{}
1919

2020
// Creates a new context and returns context, cancel and shutdown function. It should be directly followed by a defer call to shutdown.
21-
func StartContext(log *logger.Logger, cfg any, timeout time.Duration) (ctx context.Context, cancel context.CancelCauseFunc, shutdown func()) {
22-
parentCtx := context.WithValue(context.Background(), ShutdownTimeoutKey{}, timeout)
21+
func StartContext(log *logger.Logger, cfg any, timeout time.Duration) (context.Context, context.CancelCauseFunc, func()) {
22+
return StartWithContext(context.Background(), log, cfg, timeout)
23+
}
24+
25+
func StartWithContext(ctx context.Context, log *logger.Logger, cfg any, timeout time.Duration) (context.Context, context.CancelCauseFunc, func()) {
26+
parentCtx := context.WithValue(ctx, ShutdownTimeoutKey{}, timeout)
2327
ctxWithCause, cancel := context.WithCancelCause(parentCtx)
2428
ctx, c := NotifyShutdownContext(ctxWithCause)
2529
ctx = logger.SetLoggerInContext(ctx, log)

0 commit comments

Comments
 (0)