Skip to content

Commit

Permalink
Merge pull request #1 from agtabesh/fix/nested-struct
Browse files Browse the repository at this point in the history
Fix incorrect logic in encoding nested struct index
  • Loading branch information
sonh authored Dec 5, 2022
2 parents 17e6e6c + b6e6a69 commit ea61bea
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion encode_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (listField *listField) formatFnc(field reflect.Value, result resultFunc) er
err := v.formatFnc(elemVal, func(name string, val string) {
var str strings.Builder
str.WriteString(listField.name)
str.WriteString(strconv.FormatInt(int64(count), 10))
str.WriteString(strconv.FormatInt(int64(i), 10))
str.WriteByte(']')
str.WriteByte('[')
str.WriteString(name)
Expand Down
8 changes: 7 additions & 1 deletion encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,10 @@ func TestNestedStruct(t *testing.T) {
Time: tm,
Name: withStr("abc"),
},
{
Time: tm,
Name: withStr("def"),
},
},
}

Expand All @@ -744,7 +748,9 @@ func TestNestedStruct(t *testing.T) {
"nested_ptr[time]": []string{"600"},
"nested_ptr": []string{""},
"nest_list[0][time]": []string{"600"},
"nest_list[1][name]": []string{"abc"},
"nest_list[0][name]": []string{"abc"},
"nest_list[1][time]": []string{"600"},
"nest_list[1][name]": []string{"def"},
}
assert.Equal(t, expected, values)
}
Expand Down

0 comments on commit ea61bea

Please sign in to comment.