Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion string_to_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ func (s *stringToStringValue) String() string {
}

func stringToStringConv(val string) (interface{}, error) {
val = strings.Trim(val, "[]")
if strings.HasPrefix(val, "[") && strings.HasSuffix(val, "]") {
val = strings.TrimPrefix(val, "[")
val = strings.TrimSuffix(val, "]")
}
// An empty string would cause an empty map
if len(val) == 0 {
return map[string]string{}, nil
Expand Down
31 changes: 31 additions & 0 deletions string_to_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,34 @@ func TestS2SCalledTwice(t *testing.T) {
}
}
}

// Regression test for a parsing bug triggered when []
// was the last item processed in (random order) map iteration
// Using [] as the only value makes he bug reproducible
func TestS2SBracketValue(t *testing.T) {
var s2s map[string]string
f := setUpS2SFlagSet(&s2s)

vals := map[string]string{"a": "[]"}
arg := fmt.Sprintf("--s2s=%s", createS2SFlag(vals))
err := f.Parse([]string{arg})
if err != nil {
t.Fatal("expected no error; got", err)
}

for k, v := range s2s {
if vals[k] != v {
t.Fatalf("expected s2s[%s] to be %s but got: %s", k, vals[k], v)
}
}

getS2S, err := f.GetStringToString("s2s")
if err != nil {
t.Fatal("got an error from GetStringToString():", err)
}
for k, v := range getS2S {
if vals[k] != v {
t.Fatalf("expected s2s[%s] to be %s from GetStringToString but got: %s", k, vals[k], v)
}
}
}