Debian Codegraph @

encode_omitempty_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 // omitempty decides that a field is empty by the kind of its value before the marshaler is called,
12 // also for a marshaler with a pointer receiver, which is called with the address of the field.
13
14 type omitEmptyPtrReceiverString string
15
16 func (s *omitEmptyPtrReceiverString) MarshalJSON() ([]byte, error) {
17 return []byte(fmt.Sprintf("%q", "json:"+string(*s))), nil
18 }
19
20 type omitEmptyPtrReceiverInt int
21
22 func (i *omitEmptyPtrReceiverInt) MarshalJSON() ([]byte, error) {
23 return []byte(fmt.Sprintf("%d", int(*i)+100)), nil
24 }
25
26 type omitEmptyPtrReceiverStruct struct {
27 V int
28 }
29
30 func (s *omitEmptyPtrReceiverStruct) MarshalJSON() ([]byte, error) {
31 return []byte(fmt.Sprintf(`{"v":%d}`, s.V)), nil
32 }
33
34 type omitEmptyPtrReceiverFields struct {
35 S omitEmptyPtrReceiverString `json:"s,omitempty"`
36 I omitEmptyPtrReceiverInt `json:"i,omitempty"`
37 St omitEmptyPtrReceiverStruct `json:"st,omitempty"`
38 // without omitempty the marshaler is always called.
39 S2 omitEmptyPtrReceiverString `json:"s2"`
40 }
41
42 type omitEmptyPtrReceiverSingleField struct {
43 S omitEmptyPtrReceiverString `json:"s,omitempty"`
44 }
45
46 func TestEncodeOmitEmptyWithPointerReceiverMarshaler(t *testing.T) {
47 for _, test := range []struct {
48 name string
49 v any
50 }{
51 {"pointer to zero fields", &omitEmptyPtrReceiverFields{}},
52 {"pointer to fields", &omitEmptyPtrReceiverFields{S: "a", I: 1, St: omitEmptyPtrReceiverStruct{V: 1}, S2: "c"}},
53 {"zero fields", omitEmptyPtrReceiverFields{}},
54 {"fields", omitEmptyPtrReceiverFields{S: "a", I: 1, St: omitEmptyPtrReceiverStruct{V: 1}, S2: "c"}},
55 {"pointer to zero single field", &omitEmptyPtrReceiverSingleField{}},
56 {"pointer to single field", &omitEmptyPtrReceiverSingleField{S: "a"}},
57 {"zero single field", omitEmptyPtrReceiverSingleField{}},
58 {"slice of zero and non-zero", []omitEmptyPtrReceiverSingleField{{}, {S: "a"}}},
59 } {
60 t.Run(test.name, func(t *testing.T) {
61 expected, err := stdjson.Marshal(test.v)
62 if err != nil {
63 t.Fatal(err)
64 }
65 for i := 0; i < 2; i++ {
66 got, err := json.Marshal(test.v)
67 if err != nil {
68 t.Fatalf("call %d: %v", i, err)
69 }
70 if string(got) != string(expected) {
71 t.Fatalf("call %d: expected %s but got %s", i, expected, got)
72 }
73 }
74 })
75 }
76 }
77
78 // omitempty for the types which have MarshalText, with a value receiver and with a pointer receiver.
79
80 type omitEmptyTextString string
81
82 func (s omitEmptyTextString) MarshalText() ([]byte, error) {
83 return []byte("text:" + string(s)), nil
84 }
85
86 type omitEmptyTextInt int
87
88 func (i omitEmptyTextInt) MarshalText() ([]byte, error) {
89 return []byte(fmt.Sprintf("int:%d", int(i))), nil
90 }
91
92 type omitEmptyTextStruct struct {
93 V int
94 }
95
96 func (s omitEmptyTextStruct) MarshalText() ([]byte, error) {
97 return []byte(fmt.Sprintf("struct:%d", s.V)), nil
98 }
99
100 type omitEmptyTextSlice []string
101
102 func (s omitEmptyTextSlice) MarshalText() ([]byte, error) {
103 return []byte(fmt.Sprintf("slice:%d", len(s))), nil
104 }
105
106 type omitEmptyTextMap map[string]int
107
108 func (m omitEmptyTextMap) MarshalText() ([]byte, error) {
109 return []byte(fmt.Sprintf("map:%d", len(m))), nil
110 }
111
112 type omitEmptyPtrReceiverTextString string
113
114 func (s *omitEmptyPtrReceiverTextString) MarshalText() ([]byte, error) {
115 return []byte("ptrtext:" + string(*s)), nil
116 }
117
118 type omitEmptyTextFields struct {
119 S omitEmptyTextString `json:"s,omitempty"`
120 I omitEmptyTextInt `json:"i,omitempty"`
121 St omitEmptyTextStruct `json:"st,omitempty"`
122 Sl omitEmptyTextSlice `json:"sl,omitempty"`
123 M omitEmptyTextMap `json:"m,omitempty"`
124 P *omitEmptyTextString `json:"p,omitempty"`
125 PR omitEmptyPtrReceiverTextString `json:"pr,omitempty"`
126 // without omitempty the marshaler is always called.
127 S2 omitEmptyTextString `json:"s2"`
128 }
129
130 // the field with omitempty is the first and the only field, which has opcodes of its own.
131 type omitEmptyTextSingleField struct {
132 S omitEmptyTextString `json:"s,omitempty"`
133 }
134
135 type omitEmptyTextLastField struct {
136 A int `json:"a"`
137 S omitEmptyTextString `json:"s,omitempty"`
138 }
139
140 func TestEncodeOmitEmptyWithTextMarshaler(t *testing.T) {
141 str := omitEmptyTextString("p")
142 emptyStr := omitEmptyTextString("")
143 for _, test := range []struct {
144 name string
145 v any
146 }{
147 {"zero fields", omitEmptyTextFields{}},
148 {"pointer to zero fields", &omitEmptyTextFields{}},
149 {"fields", omitEmptyTextFields{
150 S: "a", I: 1, St: omitEmptyTextStruct{V: 1}, Sl: omitEmptyTextSlice{"x"}, M: omitEmptyTextMap{"k": 1}, P: &str, PR: "b", S2: "c",
151 }},
152 {"pointer to fields", &omitEmptyTextFields{
153 S: "a", I: 1, St: omitEmptyTextStruct{V: 1}, Sl: omitEmptyTextSlice{"x"}, M: omitEmptyTextMap{"k": 1}, P: &str, PR: "b", S2: "c",
154 }},
155 {"empty but not nil", &omitEmptyTextFields{Sl: omitEmptyTextSlice{}, M: omitEmptyTextMap{}, P: &emptyStr}},
156 {"zero single field", omitEmptyTextSingleField{}},
157 {"pointer to zero single field", &omitEmptyTextSingleField{}},
158 {"single field", omitEmptyTextSingleField{S: "a"}},
159 {"pointer to single field", &omitEmptyTextSingleField{S: "a"}},
160 {"zero last field", omitEmptyTextLastField{A: 1}},
161 {"last field", &omitEmptyTextLastField{A: 1, S: "a"}},
162 {"slice of zero and non-zero", []omitEmptyTextSingleField{{}, {S: "a"}, {}}},
163 } {
164 t.Run(test.name, func(t *testing.T) {
165 expected, err := stdjson.Marshal(test.v)
166 if err != nil {
167 t.Fatal(err)
168 }
169 for i := 0; i < 2; i++ {
170 got, err := json.Marshal(test.v)
171 if err != nil {
172 t.Fatalf("call %d: %v", i, err)
173 }
174 if string(got) != string(expected) {
175 t.Fatalf("call %d: expected %s but got %s", i, expected, got)
176 }
177 }
178 expectedIndent, err := stdjson.MarshalIndent(test.v, "", " ")
179 if err != nil {
180 t.Fatal(err)
181 }
182 gotIndent, err := json.MarshalIndent(test.v, "", " ")
183 if err != nil {
184 t.Fatal(err)
185 }
186 if string(gotIndent) != string(expectedIndent) {
187 t.Fatalf("indent: expected %s but got %s", expectedIndent, gotIndent)
188 }
189 })
190 }
191 }