pass all test cases

This commit is contained in:
Masaaki Goshima 2021-11-26 15:07:50 +09:00
parent 125177da35
commit c9a4a47e1c
No known key found for this signature in database
GPG Key ID: 6A53785055537153
1 changed files with 37 additions and 7 deletions

View File

@ -690,7 +690,9 @@ func (c *StructFieldCode) ToAnonymousOpcode(ctx *compileContext, isFirstField, i
}
flags := c.flags()
flags |= AnonymousHeadFlags
flags |= AnonymousKeyFlags
if c.isAnonymous {
flags |= AnonymousKeyFlags
}
field := &Opcode{
Idx: opcodeOffset(ctx.ptrIndex),
Flags: flags,
@ -1304,13 +1306,41 @@ func getFieldMap(fields []*StructFieldCode) map[string][]*StructFieldCode {
fieldMap := map[string][]*StructFieldCode{}
for _, field := range fields {
if field.isAnonymous {
structCode := field.getAnonymousStruct()
if structCode != nil && !structCode.isRecursive {
for k, v := range getFieldMap(structCode.fields) {
fieldMap[k] = append(fieldMap[k], v...)
}
continue
for k, v := range getAnonymousFieldMap(field) {
fieldMap[k] = append(fieldMap[k], v...)
}
continue
}
fieldMap[field.key] = append(fieldMap[field.key], field)
}
return fieldMap
}
func getAnonymousFieldMap(field *StructFieldCode) map[string][]*StructFieldCode {
fieldMap := map[string][]*StructFieldCode{}
structCode := field.getAnonymousStruct()
if structCode == nil || structCode.isRecursive {
fieldMap[field.key] = append(fieldMap[field.key], field)
return fieldMap
}
for k, v := range getFieldMapFromAnonymousParent(structCode.fields) {
fieldMap[k] = append(fieldMap[k], v...)
}
return fieldMap
}
func getFieldMapFromAnonymousParent(fields []*StructFieldCode) map[string][]*StructFieldCode {
fieldMap := map[string][]*StructFieldCode{}
for _, field := range fields {
if field.isAnonymous {
for k, v := range getAnonymousFieldMap(field) {
// Do not handle tagged key when embedding more than once
for _, vv := range v {
vv.isTaggedKey = false
}
fieldMap[k] = append(fieldMap[k], v...)
}
continue
}
fieldMap[field.key] = append(fieldMap[field.key], field)
}