Fix detection of indirect field

This commit is contained in:
Masaaki Goshima 2021-03-30 19:37:19 +09:00
parent 7d4316b94a
commit f3aded9b12
2 changed files with 21 additions and 1 deletions

View File

@ -32,6 +32,18 @@ func TestCoverString(t *testing.T) {
name string name string
data interface{} data interface{}
}{ }{
{
name: "PtrHeadStringComplicated",
data: &struct {
X *struct {
A string
B []string
}
}{X: &struct {
A string
B []string
}{A: "hello", B: []string{"a", "b"}}},
},
{ {
name: "String", name: "String",
data: string("a"), data: string("a"),

View File

@ -1399,7 +1399,15 @@ func compileStruct(ctx *compileContext, isPtr bool) (*Opcode, error) {
valueCode.Indirect = indirect valueCode.Indirect = indirect
} }
} else { } else {
if indirect {
// if parent is indirect type, set child indirect property to true
valueCode.Indirect = indirect valueCode.Indirect = indirect
} else {
// if parent is not indirect type and child have only one field, set child indirect property to false
if i == 0 && valueCode.NextField != nil && valueCode.NextField.Op == OpStructEnd {
valueCode.Indirect = indirect
}
}
} }
key := fmt.Sprintf(`"%s":`, tag.Key) key := fmt.Sprintf(`"%s":`, tag.Key)
escapedKey := fmt.Sprintf(`%s:`, string(AppendEscapedString([]byte{}, tag.Key))) escapedKey := fmt.Sprintf(`%s:`, string(AppendEscapedString([]byte{}, tag.Key)))