Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow value splitting pattern to be customized #32

Merged
merged 1 commit into from
Dec 2, 2023
Merged
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
31 changes: 22 additions & 9 deletions flagset.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func (f *FlagSetFiller) processStringSlice(fieldRef interface{}, hasDefaultTag b
_ = f.processCustom(
fieldRef,
func(s string) (interface{}, error) {
return parseStringSlice(s), nil
return parseStringSlice(s, f.options.valueSplitPattern), nil
},
hasDefaultTag,
tagDefault,
Expand All @@ -314,12 +314,20 @@ func (f *FlagSetFiller) processStringSlice(fieldRef interface{}, hasDefaultTag b
return
}
if hasDefaultTag {
*casted = parseStringSlice(tagDefault)
*casted = parseStringSlice(tagDefault, f.options.valueSplitPattern)
}
flagSet.Var(&strSliceVar{ref: casted, override: override}, renamed, usage)
flagSet.Var(&strSliceVar{
ref: casted,
override: override,
valueSplitPattern: f.options.valueSplitPattern,
}, renamed, usage)
if aliases != "" {
for _, alias := range strings.Split(aliases, ",") {
flagSet.Var(&strSliceVar{ref: casted, override: override}, alias, usage)
flagSet.Var(&strSliceVar{
ref: casted,
override: override,
valueSplitPattern: f.options.valueSplitPattern,
}, alias, usage)
}
}
}
Expand Down Expand Up @@ -634,8 +642,9 @@ func (f *FlagSetFiller) processCustom(fieldRef interface{}, converter func(strin
}

type strSliceVar struct {
ref *[]string
override bool
ref *[]string
override bool
valueSplitPattern string
}

func (s *strSliceVar) String() string {
Expand All @@ -646,7 +655,7 @@ func (s *strSliceVar) String() string {
}

func (s *strSliceVar) Set(val string) error {
parts := parseStringSlice(val)
parts := parseStringSlice(val, s.valueSplitPattern)

if s.override {
*s.ref = parts
Expand All @@ -658,8 +667,12 @@ func (s *strSliceVar) Set(val string) error {
return nil
}

func parseStringSlice(val string) []string {
splitter := regexp.MustCompile("[\n,]")
func parseStringSlice(val string, valueSplitPattern string) []string {
if valueSplitPattern == "" {
return []string{val}
}

splitter := regexp.MustCompile(valueSplitPattern)
parts := splitter.Split(val, -1)

// trim out blank parts
Expand Down
23 changes: 23 additions & 0 deletions flagset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,29 @@ func TestStringSlice(t *testing.T) {
assert.Equal(t, []string{"three"}, config.TagOverride)
}

func TestStringSliceWithEmptyValuePattern(t *testing.T) {
type Config struct {
NoDefault []string
TagDefault []string `default:"one,two"`
}

var config Config
filler := flagsfiller.New(flagsfiller.WithValueSplitPattern(""))

var flagset flag.FlagSet
err := filler.Fill(&flagset, &config)
require.NoError(t, err)

err = flagset.Parse([]string{
"--no-default", "nd1,nd2",
"--no-default", "nd3",
})
require.NoError(t, err)

assert.Equal(t, []string{"nd1,nd2", "nd3"}, config.NoDefault)
assert.Equal(t, []string{"one,two"}, config.TagDefault)
}

func TestStringToStringMap(t *testing.T) {
type Config struct {
NoDefault map[string]string
Expand Down
19 changes: 15 additions & 4 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ var DefaultFieldRenamer = KebabRenamer()
type FillerOption func(opt *fillerOptions)

type fillerOptions struct {
fieldRenamer []Renamer
envRenamer []Renamer
noSetFromEnv bool
fieldRenamer []Renamer
envRenamer []Renamer
noSetFromEnv bool
valueSplitPattern string
}

// WithFieldRenamer declares an option to customize the Renamer used to convert field names
Expand Down Expand Up @@ -51,6 +52,14 @@ func NoSetFromEnv() FillerOption {
}
}

// WithValueSplitPattern allows for changing the default value splitting regex pattern from newlines and commas.
// Any empty string can be provided for pattern to disable value splitting.
func WithValueSplitPattern(pattern string) FillerOption {
return func(opt *fillerOptions) {
opt.valueSplitPattern = pattern
}
}

func (o *fillerOptions) renameLongName(name string) string {
if len(o.fieldRenamer) == 0 {
return DefaultFieldRenamer(name)
Expand All @@ -63,7 +72,9 @@ func (o *fillerOptions) renameLongName(name string) string {
}

func newFillerOptions(options ...FillerOption) *fillerOptions {
v := &fillerOptions{}
v := &fillerOptions{
valueSplitPattern: "[\n,]",
}
for _, opt := range options {
opt(v)
}
Expand Down