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:
parent
7aef1d393c
commit
98c74390c0
2 changed files with 12 additions and 5 deletions
|
@ -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 {
|
||||||
|
|
|
@ -7,10 +7,11 @@ import (
|
||||||
|
|
||||||
var _ = Describe("Testing slices", func() {
|
var _ = Describe("Testing slices", func() {
|
||||||
type t struct {
|
type t struct {
|
||||||
Int []int `default:"1,2,3" flag:"int"`
|
Int []int `default:"1,2,3" flag:"int"`
|
||||||
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))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue