Debian Codegraph @

encode_direct_marshaler_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 // The types of this file are stored directly in an interface value: their data word is the value itself, not its
12 // address. Their methods are on the value, so the method set of the pointer to them has them too.
13
14 // directMarshalerTarget has the marshalers on the pointer, which directStruct promotes.
15 type directMarshalerTarget struct{ n int }
16
17 func (m *directMarshalerTarget) MarshalJSON() ([]byte, error) { return []byte(fmt.Sprint(m.n)), nil }
18
19 // directStruct is a struct of one pointer.
20 type directStruct struct{ *directMarshalerTarget }
21
22 type directMap map[string]int
23
24 func (m directMap) MarshalJSON() ([]byte, error) { return []byte(fmt.Sprint(len(m))), nil }
25
26 type directTextMap map[string]int
27
28 func (m directTextMap) MarshalText() ([]byte, error) { return []byte(fmt.Sprint("m", len(m))), nil }
29
30 type directFunc func() int
31
32 func (f directFunc) MarshalJSON() ([]byte, error) { return []byte(fmt.Sprint(f())), nil }
33
34 // A pointer to such a type calls the marshaler with the value it points to, in a field of a struct as anywhere
35 // else: the method of the pointer is called with the pointer.
36 func TestEncodePointerToDirectMarshaler(t *testing.T) {
37 s := &directStruct{&directMarshalerTarget{4}}
38 m := &directMap{"a": 1, "b": 2}
39 tm := &directTextMap{"a": 1}
40 f := directFunc(func() int { return 9 })
41 values := map[string]any{
42 "field struct": struct {
43 N int
44 O *directStruct
45 }{1, s},
46 "field struct omitempty": struct {
47 N int
48 O *directStruct `json:",omitempty"`
49 }{1, s},
50 "field struct string": struct {
51 O *directStruct `json:",string"`
52 }{s},
53 "first field struct": struct{ O *directStruct }{s},
54 "field map": struct {
55 N int
56 O *directMap
57 }{1, m},
58 "field map omitempty": struct {
59 N int
60 O *directMap `json:",omitempty"`
61 }{1, m},
62 "field text map": struct {
63 N int
64 O *directTextMap
65 }{1, tm},
66 "field func": struct {
67 N int
68 O *directFunc
69 }{1, &f},
70 "nil fields": struct {
71 S *directStruct
72 M *directMap
73 T *directTextMap
74 F *directFunc
75 }{},
76 "nil fields omitempty": struct {
77 S *directStruct `json:",omitempty"`
78 M *directMap `json:",omitempty"`
79 T *directTextMap `json:",omitempty"`
80 F *directFunc `json:",omitempty"`
81 }{},
82 "pointer to struct": &struct{ O *directStruct }{s},
83 "top level": s,
84 "top level pointer": &s,
85 "top level map": m,
86 "slice": []*directStruct{s, nil},
87 "slice of maps": []*directMap{m},
88 "map value": map[string]*directStruct{"k": s},
89 "map value of maps": map[string]*directMap{"k": m},
90 "interface": struct{ A any }{s},
91 "map key of text map value": map[string]*directTextMap{"k": tm},
92 }
93 for name, v := range values {
94 want, wantErr := stdjson.Marshal(v)
95 if wantErr != nil {
96 t.Fatalf("%s: %v", name, wantErr)
97 }
98 for i, opts := range [][]json.EncodeOptionFunc{nil, {json.UnorderedMap()}, {json.Colorize(&json.ColorScheme{})}} {
99 got, err := json.MarshalWithOption(v, opts...)
100 if err != nil || string(got) != string(want) {
101 t.Errorf("%s ( options %d ): got %s %v, want %s", name, i, got, err, want)
102 }
103 }
104 wantIndent, _ := stdjson.MarshalIndent(v, "", " ")
105 if got, err := json.MarshalIndent(v, "", " "); err != nil || string(got) != string(wantIndent) {
106 t.Errorf("%s with indent: got %s %v, want %s", name, got, err, wantIndent)
107 }
108 }
109 }