Ensure IntSlice & StringSlice serialization works as expected

This commit is contained in:
Dan Buch
2016-04-29 02:53:58 -04:00
parent d1b0c49a98
commit 1a91f3dce5
2 changed files with 55 additions and 16 deletions

20
flag.go
View File

@@ -141,9 +141,9 @@ func (f *StringSlice) Set(value string) error {
}
if strings.HasPrefix(value, slPfx) {
v := []string{}
_ = json.Unmarshal([]byte(strings.Replace(value, slPfx, "", 1)), v)
f.slice = append(f.slice, v...)
// Deserializing assumes overwrite
_ = json.Unmarshal([]byte(strings.Replace(value, slPfx, "", 1)), &f.slice)
f.hasBeenSet = true
return nil
}
@@ -242,10 +242,10 @@ func (i *IntSlice) Set(value string) error {
i.hasBeenSet = true
}
if strings.HasPrefix(slPfx, value) {
v := []int{}
_ = json.Unmarshal([]byte(strings.Replace(value, slPfx, "", 1)), v)
i.slice = append(i.slice, v...)
if strings.HasPrefix(value, slPfx) {
// Deserializing assumes overwrite
_ = json.Unmarshal([]byte(strings.Replace(value, slPfx, "", 1)), &i.slice)
i.hasBeenSet = true
return nil
}
@@ -263,6 +263,12 @@ func (i *IntSlice) String() string {
return fmt.Sprintf("%v", i.slice)
}
// Serialized allows IntSlice to fulfill Serializeder
func (i *IntSlice) Serialized() string {
jsonBytes, _ := json.Marshal(i.slice)
return fmt.Sprintf("%s%s", slPfx, string(jsonBytes))
}
// Value returns the slice of ints set by this flag
func (i *IntSlice) Value() []int {
return i.slice