1
0
Fork 0
mirror of https://github.com/Luzifer/rconfig.git synced 2024-12-21 03:31:19 +00:00

Breaking: Ensure an empty default string does not yield a slice with 1 element

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2018-08-02 09:18:31 +02:00
parent 7aef1d393c
commit 98c74390c0
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
2 changed files with 12 additions and 5 deletions

View file

@ -237,7 +237,10 @@ func execTags(in interface{}, fs *pflag.FlagSet) error {
if len(del) == 0 { if len(del) == 0 {
del = "," del = ","
} }
def := strings.Split(value, del) var def = []string{}
if value != "" {
def = strings.Split(value, del)
}
if len(parts) == 1 { if len(parts) == 1 {
fs.StringSliceVar(valField.Addr().Interface().(*[]string), parts[0], def, typeField.Tag.Get("description")) fs.StringSliceVar(valField.Addr().Interface().(*[]string), parts[0], def, typeField.Tag.Get("description"))
} else { } else {

View file

@ -11,6 +11,7 @@ var _ = Describe("Testing slices", func() {
String []string `default:"a,b,c" flag:"string"` String []string `default:"a,b,c" flag:"string"`
IntP []int `default:"1,2,3" flag:"intp,i"` IntP []int `default:"1,2,3" flag:"intp,i"`
StringP []string `default:"a,b,c" flag:"stringp,s"` StringP []string `default:"a,b,c" flag:"stringp,s"`
EmptyString []string `default:""`
} }
var ( var (
@ -48,4 +49,7 @@ var _ = Describe("Testing slices", func() {
Expect(len(cfg.StringP)).To(Equal(2)) Expect(len(cfg.StringP)).To(Equal(2))
Expect(cfg.StringP).To(Equal([]string{"hallo", "welt"})) 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))
})
}) })