From f0e6a549f2f868b6ecbb5e05aa80b52d9baf9aee Mon Sep 17 00:00:00 2001 From: Nao Yonashiro Date: Mon, 4 Jul 2022 14:46:17 +0900 Subject: [PATCH] fix: support for embedding alias of primitive types fix #372 --- decode_test.go | 17 +++++++++++++++++ internal/decoder/compile.go | 9 +++++++++ 2 files changed, 26 insertions(+) diff --git a/decode_test.go b/decode_test.go index f5e2563..c276a32 100644 --- a/decode_test.go +++ b/decode_test.go @@ -3968,3 +3968,20 @@ func TestIssue335(t *testing.T) { t.Errorf("unexpected success") } } + +func TestIssue372(t *testing.T) { + type A int + type T struct { + _ int + *A + } + var v T + err := json.Unmarshal([]byte(`{"A":7}`), &v) + assertErr(t, err) + + got := *v.A + expected := A(7) + if got != expected { + t.Errorf("unexpected result: %v != %v", got, expected) + } +} diff --git a/internal/decoder/compile.go b/internal/decoder/compile.go index f13b43b..48b0217 100644 --- a/internal/decoder/compile.go +++ b/internal/decoder/compile.go @@ -393,6 +393,15 @@ func compileStruct(typ *runtime.Type, structName, fieldName string, structTypeTo } allFields = append(allFields, fieldSet) } + } else { + fieldSet := &structFieldSet{ + dec: pdec, + offset: field.Offset, + isTaggedKey: tag.IsTaggedKey, + key: field.Name, + keyLen: int64(len(field.Name)), + } + allFields = append(allFields, fieldSet) } } else { fieldSet := &structFieldSet{