Skip to content

Commit

Permalink
Merge pull request #3 from sonh/fix/encoding-empty-slice
Browse files Browse the repository at this point in the history
Fix encoding for empty slice that causes panic
  • Loading branch information
sonh authored Dec 5, 2022
2 parents ea61bea + 25f9152 commit b0f2786
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion encode_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (listField *listField) formatFnc(field reflect.Value, result resultFunc) er
}
}
returnStr := str.String()
if returnStr[0] == ',' {
if len(returnStr) > 0 && returnStr[0] == ',' {
returnStr = returnStr[1:]
}
result(listField.name, returnStr)
Expand Down
6 changes: 4 additions & 2 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ func TestArrayFormat_Comma(t *testing.T) {
tm := time.Unix(600, 0).UTC()

s := struct {
EmptyList []string `qs:"empty_list,comma"`
StringList []string `qs:"str_list,comma"`
Times []*time.Time `qs:"times,comma"`
}{
Expand All @@ -614,8 +615,9 @@ func TestArrayFormat_Comma(t *testing.T) {
return
}
expected := url.Values{
"str_list": []string{"a,b,c"},
"times": []string{tm.Format(time.RFC3339)},
"empty_list": []string{""},
"str_list": []string{"a,b,c"},
"times": []string{tm.Format(time.RFC3339)},
}
assert.Equal(t, expected, values)
}
Expand Down

0 comments on commit b0f2786

Please sign in to comment.