1
0
Fork 0
mirror of https://github.com/Luzifer/rconfig.git synced 2024-11-09 16:30:04 +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 {
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 {

View file

@ -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))
})
})