Debian Codegraph @

encode_pointer_shaped_test.go

raw
1 package json_test
2
3 import (
4 stdjson "encoding/json"
5 "fmt"
6 "testing"
7
8 "github.com/goccy/go-json"
9 )
10
11 // A value whose type consists of a single pointer ( a struct of one pointer field, an array of one pointer,
12 // a map, ... ) is stored in an interface value as the pointer itself, not as a pointer to the value.
13 // The encoder must produce the same result as encoding/json however such a value is passed.
14
15 type pointerShapedItem struct {
16 A string `json:"a"`
17 }
18
19 // the pointee starts with a nested struct value.
20 type pointerShapedDetail struct {
21 I pointerShapedItem `json:"i"`
22 }
23
24 type pointerShapedBody struct {
25 Payload *pointerShapedDetail `json:"p"`
26 }
27
28 // the pointee starts with a pointer field and has more fields.
29 type pointerShapedPtrFields struct {
30 Foo *string `json:"foo"`
31 Bar *string `json:"bar"`
32 }
33
34 type pointerShapedPtrFieldsHolder struct {
35 B *pointerShapedPtrFields `json:"b"`
36 }
37
38 type pointerShapedMixedFields struct {
39 ID *string `json:"id"`
40 Name string `json:"name"`
41 }
42
43 type pointerShapedMixedFieldsHolder struct {
44 Bot *pointerShapedMixedFields `json:"bot"`
45 }
46
47 // the only field is an embedded pointer to a struct which embeds another struct.
48 type pointerShapedDBObject struct {
49 ID int64 `json:"id,omitempty"`
50 }
51
52 type pointerShapedEmbeddedItem struct {
53 pointerShapedDBObject
54 }
55
56 type pointerShapedEmbeddedHolder struct {
57 *pointerShapedEmbeddedItem
58 }
59
60 // omitempty on the pointer: whether the pointee is zero must not matter.
61 type pointerShapedChild struct {
62 Value int `json:"v"`
63 }
64
65 type pointerShapedOmitEmptyHolder struct {
66 Child *pointerShapedChild `json:"c,omitempty"`
67 }
68
69 type pointerShapedStringChild struct {
70 Value string `json:"v"`
71 }
72
73 type pointerShapedOmitEmptyStringHolder struct {
74 Child *pointerShapedStringChild `json:"c,omitempty"`
75 }
76
77 // recursive type passed by value.
78 type pointerShapedRecursive struct {
79 Next *pointerShapedRecursive `json:"next"`
80 }
81
82 type pointerShapedMapHolder struct {
83 M map[string]int `json:"m"`
84 }
85
86 type pointerShapedNested struct {
87 Holder pointerShapedBody `json:"holder"`
88 }
89
90 // a map type which has MarshalJSON with a value receiver.
91 type pointerShapedSet map[string]struct{}
92
93 func (s pointerShapedSet) MarshalJSON() ([]byte, error) {
94 return []byte(fmt.Sprintf(`"len=%d"`, len(s))), nil
95 }
96
97 type pointerShapedSetHolder struct {
98 S pointerShapedSet `json:"s"`
99 }
100
101 // a map type which has MarshalText with a value receiver.
102 type pointerShapedTextSet map[string]struct{}
103
104 func (s pointerShapedTextSet) MarshalText() ([]byte, error) {
105 return []byte(fmt.Sprintf("len=%d", len(s))), nil
106 }
107
108 type pointerShapedTextSetHolder struct {
109 S pointerShapedTextSet `json:"s"`
110 // the second field has opcodes different from the first one.
111 S2 pointerShapedTextSet `json:"s2"`
112 }
113
114 type pointerShapedSetFields struct {
115 A int `json:"a"`
116 S pointerShapedSet `json:"s"`
117 }
118
119 // MarshalJSON with a pointer receiver.
120 type pointerShapedPtrMarshaler struct {
121 A int
122 }
123
124 func (m *pointerShapedPtrMarshaler) MarshalJSON() ([]byte, error) {
125 return []byte(`"ptr receiver"`), nil
126 }
127
128 func TestEncodePointerShapedValue(t *testing.T) {
129 str := "hello"
130 num := 1
131 for _, test := range []struct {
132 name string
133 v any
134 }{
135 {name: "struct of pointer to struct starting with struct", v: pointerShapedBody{Payload: &pointerShapedDetail{I: pointerShapedItem{A: "a"}}}},
136 {name: "struct of nil pointer", v: pointerShapedBody{}},
137 {name: "struct of pointer to struct starting with pointer", v: pointerShapedPtrFieldsHolder{B: &pointerShapedPtrFields{Foo: &str}}},
138 {name: "struct of pointer to struct starting with nil pointer", v: pointerShapedPtrFieldsHolder{B: &pointerShapedPtrFields{Bar: &str}}},
139 {name: "struct of pointer to struct of pointer and string", v: pointerShapedMixedFieldsHolder{Bot: &pointerShapedMixedFields{ID: &str, Name: "name"}}},
140 {name: "struct of embedded pointer", v: pointerShapedEmbeddedHolder{&pointerShapedEmbeddedItem{pointerShapedDBObject{ID: 1}}}},
141 {name: "struct of nil embedded pointer", v: pointerShapedEmbeddedHolder{}},
142 {name: "omitempty pointer to zero int struct", v: pointerShapedOmitEmptyHolder{Child: &pointerShapedChild{}}},
143 {name: "omitempty pointer to non-zero int struct", v: pointerShapedOmitEmptyHolder{Child: &pointerShapedChild{Value: 1}}},
144 {name: "omitempty nil pointer", v: pointerShapedOmitEmptyHolder{}},
145 {name: "omitempty pointer to empty string struct", v: pointerShapedOmitEmptyStringHolder{Child: &pointerShapedStringChild{}}},
146 {name: "recursive struct", v: pointerShapedRecursive{Next: &pointerShapedRecursive{}}},
147 {name: "deeper recursive struct", v: pointerShapedRecursive{Next: &pointerShapedRecursive{Next: &pointerShapedRecursive{}}}},
148 {name: "struct of map", v: pointerShapedMapHolder{M: map[string]int{"a": 1}}},
149 {name: "struct of nil map", v: pointerShapedMapHolder{}},
150 {name: "struct of pointer-shaped struct", v: pointerShapedNested{Holder: pointerShapedBody{Payload: &pointerShapedDetail{I: pointerShapedItem{A: "a"}}}}},
151 {name: "array of pointer", v: [1]*int{&num}},
152 {name: "array of nil pointer", v: [1]*int{nil}},
153 {name: "array of pointer-shaped struct", v: [1]pointerShapedBody{{Payload: &pointerShapedDetail{}}}},
154 {name: "map of marshaler map", v: map[string]pointerShapedSet{"foo": {"a": struct{}{}}}},
155 {name: "map of empty marshaler map", v: map[string]pointerShapedSet{"foo": {}}},
156 {name: "int key map of marshaler map", v: map[int]pointerShapedSet{1: {"a": struct{}{}}}},
157 {name: "map of map of marshaler map", v: map[string]map[string]pointerShapedSet{"x": {"foo": {"a": struct{}{}}}}},
158 {name: "marshaler map", v: pointerShapedSet{"a": struct{}{}}},
159 {name: "struct of marshaler map", v: pointerShapedSetHolder{S: pointerShapedSet{"a": struct{}{}}}},
160 // encoding/json writes null only for a nil pointer: the marshaler of a nil map is called.
161 {name: "nil marshaler map", v: pointerShapedSet(nil)},
162 {name: "map of nil marshaler map", v: map[string]pointerShapedSet{"foo": nil}},
163 {name: "slice of nil marshaler map", v: []pointerShapedSet{nil, {"a": struct{}{}}}},
164 {name: "struct of nil marshaler map", v: pointerShapedSetHolder{}},
165 {name: "pointer to struct of nil marshaler map", v: &pointerShapedSetHolder{}},
166 {name: "second field of nil marshaler map", v: pointerShapedSetFields{A: 1}},
167 {name: "pointer to second field of nil marshaler map", v: &pointerShapedSetFields{A: 1}},
168 {name: "nil text marshaler map", v: pointerShapedTextSet(nil)},
169 {name: "map of nil text marshaler map", v: map[string]pointerShapedTextSet{"foo": nil}},
170 {name: "struct of nil text marshaler map", v: pointerShapedTextSetHolder{}},
171 {name: "pointer to struct of nil text marshaler map", v: &pointerShapedTextSetHolder{}},
172 {name: "struct of text marshaler map", v: pointerShapedTextSetHolder{S: pointerShapedTextSet{"a": struct{}{}}}},
173 {name: "value of pointer receiver marshaler", v: pointerShapedPtrMarshaler{A: 1}},
174 {name: "pointer to pointer receiver marshaler", v: &pointerShapedPtrMarshaler{A: 1}},
175 {name: "pointer to pointer to pointer receiver marshaler", v: func() **pointerShapedPtrMarshaler {
176 m := &pointerShapedPtrMarshaler{A: 1}
177 return &m
178 }()},
179 {name: "nil pointer to pointer receiver marshaler", v: (*pointerShapedPtrMarshaler)(nil)},
180 } {
181 t.Run(test.name, func(t *testing.T) {
182 // the same value must be encoded in the same way wherever it is placed.
183 for _, placement := range []struct {
184 name string
185 v any
186 }{
187 {"value", test.v},
188 {"pointer to interface", &test.v},
189 {"slice of interface", []any{test.v}},
190 {"map of interface", map[string]any{"k": test.v}},
191 } {
192 expected, err := stdjson.Marshal(placement.v)
193 if err != nil {
194 t.Fatal(err)
195 }
196 // the second call uses the cached opcode.
197 for i := 0; i < 2; i++ {
198 got, err := json.Marshal(placement.v)
199 if err != nil {
200 t.Fatalf("%s ( call %d ): %v", placement.name, i, err)
201 }
202 if string(got) != string(expected) {
203 t.Fatalf("%s ( call %d ): expected %s but got %s", placement.name, i, expected, got)
204 }
205 }
206 gotIndent, err := json.MarshalIndent(placement.v, "", " ")
207 if err != nil {
208 t.Fatalf("%s ( indent ): %v", placement.name, err)
209 }
210 expectedIndent, err := stdjson.MarshalIndent(placement.v, "", " ")
211 if err != nil {
212 t.Fatal(err)
213 }
214 if string(gotIndent) != string(expectedIndent) {
215 t.Fatalf("%s ( indent ): expected %s but got %s", placement.name, expectedIndent, gotIndent)
216 }
217 }
218 })
219 }
220 }
221
222 // addressRecorder records the receiver with which MarshalJSON is called.
223 type addressRecorder struct {
224 A, B int
225 }
226
227 var recordedAddresses []*addressRecorder
228
229 func (r *addressRecorder) MarshalJSON() ([]byte, error) {
230 recordedAddresses = append(recordedAddresses, r)
231 return []byte("1"), nil
232 }
233
234 type addressRecorderHolder struct {
235 R addressRecorder
236 }
237
238 // A marshaler with a pointer receiver is called with the address of the original value, not of a copy,
239 // as encoding/json does.
240 func TestEncodePointerReceiverMarshalerAddress(t *testing.T) {
241 value := &addressRecorder{}
242 elems := []addressRecorder{{}, {}}
243 holder := &addressRecorderHolder{}
244 for _, test := range []struct {
245 name string
246 v any
247 expected []*addressRecorder
248 }{
249 {"pointer", value, []*addressRecorder{value}},
250 {"elements of slice", elems, []*addressRecorder{&elems[0], &elems[1]}},
251 {"field of pointer to struct", holder, []*addressRecorder{&holder.R}},
252 } {
253 t.Run(test.name, func(t *testing.T) {
254 for _, marshal := range []func(any) ([]byte, error){
255 json.Marshal,
256 func(v any) ([]byte, error) { return json.MarshalIndent(v, "", " ") },
257 } {
258 recordedAddresses = nil
259 if _, err := marshal(test.v); err != nil {
260 t.Fatal(err)
261 }
262 if len(recordedAddresses) != len(test.expected) {
263 t.Fatalf("expected %d calls but got %d", len(test.expected), len(recordedAddresses))
264 }
265 for i, addr := range recordedAddresses {
266 if addr != test.expected[i] {
267 t.Fatalf("call %d: the marshaler was called with a copy %p, not with the original value %p", i, addr, test.expected[i])
268 }
269 }
270 }
271 })
272 }
273 }