From fba4535dee4be13f58db58eee5cdef7dc983a166 Mon Sep 17 00:00:00 2001 From: vvatamanov Date: Thu, 25 Apr 2024 16:14:31 +0300 Subject: [PATCH 1/2] fix custom map parshaling --- internal/encoder/compiler.go | 2 +- test/cover/cover_slice_test.go | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/internal/encoder/compiler.go b/internal/encoder/compiler.go index 3ae39ba8..37b7aa38 100644 --- a/internal/encoder/compiler.go +++ b/internal/encoder/compiler.go @@ -480,7 +480,7 @@ func (c *Compiler) mapCode(typ *runtime.Type) (*MapCode, error) { func (c *Compiler) listElemCode(typ *runtime.Type) (Code, error) { switch { - case c.isPtrMarshalJSONType(typ): + case c.implementsMarshalJSONType(typ) || c.implementsMarshalJSONType(runtime.PtrTo(typ)): return c.marshalJSONCode(typ) case !typ.Implements(marshalTextType) && runtime.PtrTo(typ).Implements(marshalTextType): return c.marshalTextCode(typ) diff --git a/test/cover/cover_slice_test.go b/test/cover/cover_slice_test.go index 61de9a5d..557bab75 100644 --- a/test/cover/cover_slice_test.go +++ b/test/cover/cover_slice_test.go @@ -16,6 +16,12 @@ func (coverSliceMarshalJSON) MarshalJSON() ([]byte, error) { return []byte(`"hello"`), nil } +type coverSliceMarshalJSONMap map[string]any + +func (c coverSliceMarshalJSONMap) MarshalJSON() ([]byte, error) { + return json.Marshal(map[string]any(c)) +} + type coverSliceMarshalText struct { A int } @@ -152,6 +158,10 @@ func TestCoverSlice(t *testing.T) { name: "SliceMarshalJSON", data: []coverSliceMarshalJSON{{A: 1}, {A: 2}}, }, + { + name: "SliceMarshalJSONMap", + data: []coverSliceMarshalJSONMap{{"foo": "bar"}, {"some": 1}}, + }, { name: "SliceMarshalText", data: []coverSliceMarshalText{{A: 1}, {A: 2}}, From 90780da0d9977a032f988501704197c6f8ab08df Mon Sep 17 00:00:00 2001 From: viewsharp Date: Thu, 25 Apr 2024 16:32:10 +0300 Subject: [PATCH 2/2] add additional tests --- test/cover/cover_slice_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/cover/cover_slice_test.go b/test/cover/cover_slice_test.go index 557bab75..70a03063 100644 --- a/test/cover/cover_slice_test.go +++ b/test/cover/cover_slice_test.go @@ -22,6 +22,12 @@ func (c coverSliceMarshalJSONMap) MarshalJSON() ([]byte, error) { return json.Marshal(map[string]any(c)) } +type coverSliceMarshalJSONMapPtr map[string]any + +func (c *coverSliceMarshalJSONMapPtr) MarshalJSON() ([]byte, error) { + return json.Marshal(map[string]any(*c)) +} + type coverSliceMarshalText struct { A int } @@ -162,6 +168,18 @@ func TestCoverSlice(t *testing.T) { name: "SliceMarshalJSONMap", data: []coverSliceMarshalJSONMap{{"foo": "bar"}, {"some": 1}}, }, + { + name: "SliceMarshalJSONMap", + data: []*coverSliceMarshalJSONMap{{"foo": "bar"}, {"some": 1}}, + }, + { + name: "SliceMarshalJSONMap", + data: []coverSliceMarshalJSONMapPtr{{"foo": "bar"}, {"some": 1}}, + }, + { + name: "SliceMarshalJSONMap", + data: []*coverSliceMarshalJSONMapPtr{{"foo": "bar"}, {"some": 1}}, + }, { name: "SliceMarshalText", data: []coverSliceMarshalText{{A: 1}, {A: 2}},