Skip to content

Commit

Permalink
Fix tag parse error for embed struct
Browse files Browse the repository at this point in the history
  • Loading branch information
sonh committed Oct 4, 2020
1 parent 241468b commit a9c6547
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
26 changes: 12 additions & 14 deletions encode.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package qs

import (
"bytes"
"github.com/pkg/errors"
"net/url"
"reflect"
Expand Down Expand Up @@ -75,7 +74,7 @@ func NewEncoder(options ...EncoderOption) *Encoder {
tagSize := 5
tags := make([][]byte, 0, tagSize)
for i := 0; i < tagSize; i++ {
tags = append(tags, make([]byte, 0, 64))
tags = append(tags, make([]byte, 0, 56))
}
return &encoder{
e: e,
Expand Down Expand Up @@ -248,28 +247,27 @@ func (e *encoder) getTagNameAndOpts(f reflect.StructField) {
tag := f.Tag.Get(e.e.tagAlias)

// Clear first tag in slice
e.tags[0] = e.tags[0][len(e.tags[0]):]
e.tags[0] = e.tags[0][:0]

if len(tag) == 0 {
// no tag, using struct field name
e.tags[0] = append(e.tags[0], f.Name...)
e.tags[0] = append(e.tags[0][:0], f.Name...)
e.tags = e.tags[:1]
} else {
// Use first tag as temp
e.tags[0] = append(e.tags[0], tag...)
splitTags := bytes.Split(e.tags[0], []byte{','})
e.tags[0] = append(e.tags[0][:0], tag...)

splitTags := strings.Split(tag, ",")
e.tags = e.tags[:len(splitTags)]

if len(splitTags) > 0 {
if len(splitTags[0]) == 0 || string(splitTags[0]) == " " {
splitTags[0] = append(splitTags[0][len(splitTags[0]):], f.Name...)
}
}

for i := 0; i < len(splitTags); i++ {
// Clear this tag and set new tag
e.tags[i] = append(e.tags[i][len(e.tags[i]):], splitTags[i]...)
if i == 0 {
if len(splitTags[0]) == 0 {
e.tags[0] = append(e.tags[i][:0], f.Name...)
continue
}
}
e.tags[i] = append(e.tags[i][:0], splitTags[i]...)
}
}
}
10 changes: 6 additions & 4 deletions encode_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,6 @@ func (e *encoder) newListField(elemTyp reflect.Type, tagName []byte, tagOptions
}
}

if field, ok := listField.cachedField.(*embedField); ok {
e.structCaching(&field.cachedFields, reflect.Zero(elemTyp), nil)
}

switch listField.arrayFormat {
case arrayFormatRepeat, arrayFormatBracket:
if listField.arrayFormat >= arrayFormatBracket {
Expand All @@ -194,9 +190,15 @@ func (e *encoder) newListField(elemTyp reflect.Type, tagName []byte, tagOptions
case arrayFormatIndex:
tagName = append(tagName, '[')
}

listField.baseField = &baseField{
name: string(tagName),
}

if field, ok := listField.cachedField.(*embedField); ok {
e.structCaching(&field.cachedFields, reflect.Zero(elemTyp), nil)
}

return listField
}

Expand Down

0 comments on commit a9c6547

Please sign in to comment.