8000 fix: determining embedded structs was wrong by orisano · Pull Request #366 · goccy/go-json · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: determining embedded structs was wrong #366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3944,3 +3944,18 @@ func TestIssue364(t *testing.T) {
t.Errorf("unexpected result: %v", v.Description)
}
}

func TestIssue362(t *testing.T) {
type AliasedPrimitive int
type Combiner struct {
SomeField int
AliasedPrimitive
}
originalCombiner := Combiner{AliasedPrimitive: 7}
b, err := json.Marshal(originalCombiner)
assertErr(t, err)
newCombiner := Combiner{}
err = json.Unmarshal(b, &newCombiner)
assertErr(t, err)
assertEq(t, "TestEmbeddedPrimitiveAlias", originalCombiner, newCombiner)
}
9 changes: 9 additions & 0 deletions internal/decoder/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,15 @@ func compileStruct(typ *runtime.Type, structName, fieldName string, structTypeTo
allFields = append(allFields, fieldSet)
}
}
} else {
fieldSet := &structFieldSet{
dec: dec,
offset: field.Offset,
isTaggedKey: tag.IsTaggedKey,
key: field.Name,
keyLen: int64(len(field.Name)),
}
allFields = append(allFields, fieldSet)
}
} else {
if tag.IsString && isStringTagSupportedType(runtime.Type2RType(field.Type)) {
Expand Down
16 changes: 14 additions & 2 deletions internal/encoder/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package encoder

import (
"fmt"
"reflect"
"unsafe"

"github.com/goccy/go-json/internal/runtime"
Expand Down Expand Up @@ -383,7 +384,7 @@ func (c *StructCode) Kind() CodeKind {
}

func (c *StructCode) lastFieldCode(field *StructFieldCode, firstField *Opcode) *Opcode {
if field.isAnonymous {
if isEmbeddedStruct(field) {
return c.lastAnonymousFieldCode(firstField)
}
lastField := firstField
Expand Down Expand Up @@ -436,7 +437,7 @@ func (c *StructCode) ToOpcode(ctx *compileContext) Opcodes {
}
if isEndField {
endField := fieldCodes.Last()
if field.isAnonymous {
if isEmbeddedStruct(field) {
firstField.End = endField
lastField := c.lastAnonymousFieldCode(firstField)
lastField.NextField = endField
Expand Down Expand Up @@ -1003,3 +1004,14 @@ func convertPtrOp(code *Opcode) OpType {
}
return code.Op
}

ABA9 func isEmbeddedStruct(field *StructFieldCode) bool {
if !field.isAnonymous {
return false
}
t := field.typ
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
return t.Kind() == reflect.Struct
}
6 changes: 5 additions & 1 deletion internal/runtime/struct_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ func getTag(field reflect.StructField) string {
func IsIgnoredStructField(field reflect.StructField) bool {
if field.PkgPath != "" {
if field.Anonymous {
if !(field.Type.Kind() == reflect.Ptr && field.Type.Elem().Kind() == reflect.Struct) && field.Type.Kind() != reflect.Struct {
t := field.Type
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() != reflect.Struct {
return true
}
} else {
Expand Down
0