From 98c74390c086c6a53c7e77d04024e053e5d4b6dd Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Thu, 2 Aug 2018 09:18:31 +0200 Subject: [PATCH] Breaking: Ensure an empty default string does not yield a slice with 1 element Signed-off-by: Knut Ahlers --- config.go | 5 ++++- slice_test.go | 12 ++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/config.go b/config.go index 251909d..ee9301e 100644 --- a/config.go +++ b/config.go @@ -237,7 +237,10 @@ func execTags(in interface{}, fs *pflag.FlagSet) error { if len(del) == 0 { del = "," } - def := strings.Split(value, del) + var def = []string{} + if value != "" { + def = strings.Split(value, del) + } if len(parts) == 1 { fs.StringSliceVar(valField.Addr().Interface().(*[]string), parts[0], def, typeField.Tag.Get("description")) } else { diff --git a/slice_test.go b/slice_test.go index 7d9524e..983f87d 100644 --- a/slice_test.go +++ b/slice_test.go @@ -7,10 +7,11 @@ import ( var _ = Describe("Testing slices", func() { type t struct { - Int []int `default:"1,2,3" flag:"int"` - String []string `default:"a,b,c" flag:"string"` - IntP []int `default:"1,2,3" flag:"intp,i"` - StringP []string `default:"a,b,c" flag:"stringp,s"` + Int []int `default:"1,2,3" flag:"int"` + String []string `default:"a,b,c" flag:"string"` + IntP []int `default:"1,2,3" flag:"intp,i"` + StringP []string `default:"a,b,c" flag:"stringp,s"` + EmptyString []string `default:""` } var ( @@ -48,4 +49,7 @@ var _ = Describe("Testing slices", func() { Expect(len(cfg.StringP)).To(Equal(2)) Expect(cfg.StringP).To(Equal([]string{"hallo", "welt"})) }) + It("should have no elements for an empty default string", func() { + Expect(len(cfg.EmptyString)).To(Equal(0)) + }) })