@@ -15,9 +15,10 @@ import (
15
15
"runtime/debug"
16
16
"strconv"
17
17
"testing"
18
+ "time"
18
19
)
19
20
20
- type Optionals struct {
21
+ type OptionalsEmpty struct {
21
22
Sr string `json:"sr"`
22
23
So string `json:"so,omitempty"`
23
24
Sw string `json:"-"`
@@ -45,7 +46,7 @@ type Optionals struct {
45
46
}
46
47
47
48
func TestOmitEmpty (t * testing.T ) {
48
- var want = `{
49
+ const want = `{
49
50
"sr": "",
50
51
"omitempty": 0,
51
52
"slr": null,
@@ -56,7 +57,7 @@ func TestOmitEmpty(t *testing.T) {
56
57
"str": {},
57
58
"sto": {}
58
59
}`
59
- var o Optionals
60
+ var o OptionalsEmpty
60
61
o .Sw = "something"
61
62
o .Mr = map [string ]any {}
62
63
o .Mo = map [string ]any {}
@@ -70,6 +71,187 @@ func TestOmitEmpty(t *testing.T) {
70
71
}
71
72
}
72
73
74
+ type NonZeroStruct struct {}
75
+
76
+ func (nzs NonZeroStruct ) IsZero () bool {
77
+ return false
78
+ }
79
+
80
+ type NoPanicStruct struct {
81
+ Int int `json:"int,omitzero"`
82
+ }
83
+
84
+ func (nps * NoPanicStruct ) IsZero () bool {
85
+ return nps .Int != 0
86
+ }
87
+
88
+ type OptionalsZero struct {
89
+ Sr string `json:"sr"`
90
+ So string `json:"so,omitzero"`
91
+ Sw string `json:"-"`
92
+
93
+ Ir int `json:"omitzero"` // actually named omitzero, not an option
94
+ Io int `json:"io,omitzero"`
95
+
96
+ Slr []string `json:"slr,random"`
97
+ Slo []string `json:"slo,omitzero"`
98
+ SloNonNil []string `json:"slononnil,omitzero"`
99
+
100
+ Mr map [string ]any `json:"mr"`
101
+ Mo map [string ]any `json:",omitzero"`
102
+ Moo map [string ]any `json:"moo,omitzero"`
103
+
104
+ Fr float64 `json:"fr"`
105
+ Fo float64 `json:"fo,omitzero"`
106
+ Foo float64 `json:"foo,omitzero"`
107
+ Foo2 [2 ]float64 `json:"foo2,omitzero"`
108
+
109
+ Br bool `json:"br"`
110
+ Bo bool `json:"bo,omitzero"`
111
+
112
+ Ur uint `json:"ur"`
113
+ Uo uint `json:"uo,omitzero"`
114
+
115
+ Str struct {} `json:"str"`
116
+ Sto struct {} `json:"sto,omitzero"`
117
+
118
+ Time time.Time `json:"time,omitzero"`
119
+ TimeLocal time.Time `json:"timelocal,omitzero"`
120
+ Nzs NonZeroStruct `json:"nzs,omitzero"`
121
+
122
+ NilIsZeroer isZeroer `json:"niliszeroer,omitzero"` // nil interface
123
+ NonNilIsZeroer isZeroer `json:"nonniliszeroer,omitzero"` // non-nil interface
124
+ NoPanicStruct0 isZeroer `json:"nps0,omitzero"` // non-nil interface with nil pointer
125
+ NoPanicStruct1 isZeroer `json:"nps1,omitzero"` // non-nil interface with non-nil pointer
126
+ NoPanicStruct2 * NoPanicStruct `json:"nps2,omitzero"` // nil pointer
127
+ NoPanicStruct3 * NoPanicStruct `json:"nps3,omitzero"` // non-nil pointer
128
+ NoPanicStruct4 NoPanicStruct `json:"nps4,omitzero"` // concrete type
129
+ }
130
+
131
+ func TestOmitZero (t * testing.T ) {
132
+ const want = `{
133
+ "sr": "",
134
+ "omitzero": 0,
135
+ "slr": null,
136
+ "slononnil": [],
137
+ "mr": {},
138
+ "Mo": {},
139
+ "fr": 0,
140
+ "br": false,
141
+ "ur": 0,
142
+ "str": {},
143
+ "nzs": {},
144
+ "nps1": {},
145
+ "nps3": {},
146
+ "nps4": {}
147
+ }`
148
+ var o OptionalsZero
149
+ o .Sw = "something"
150
+ o .SloNonNil = make ([]string , 0 )
151
+ o .Mr = map [string ]any {}
152
+ o .Mo = map [string ]any {}
153
+
154
+ o .Foo = - 0
155
+ o .Foo2 = [2 ]float64 {+ 0 , - 0 }
156
+
157
+ o .TimeLocal = time.Time {}.Local ()
158
+
159
+ o .NonNilIsZeroer = time.Time {}
160
+ o .NoPanicStruct0 = (* NoPanicStruct )(nil )
161
+ o .NoPanicStruct1 = & NoPanicStruct {}
162
+ o .NoPanicStruct3 = & NoPanicStruct {}
163
+
164
+ got , err := MarshalIndent (& o , "" , " " )
165
+ if err != nil {
166
+ t .Fatalf ("MarshalIndent error: %v" , err )
167
+ }
168
+ if got := string (got ); got != want {
169
+ t .Errorf ("MarshalIndent:\n \t got: %s\n \t want: %s\n " , indentNewlines (got ), indentNewlines (want ))
170
+ }
171
+ }
172
+
173
+ func TestOmitZeroMap (t * testing.T ) {
174
+ const want = `{
175
+ "foo": {
176
+ "sr": "",
177
+ "omitzero": 0,
178
+ "slr": null,
179
+ "mr": null,
180
+ "fr": 0,
181
+ "br": false,
182
+ "ur": 0,
183
+ "str": {},
184
+ "nzs": {},
185
+ "nps4": {}
186
+ }
187
+ }`
188
+ m := map [string ]OptionalsZero {"foo" : {}}
189
+ got , err := MarshalIndent (m , "" , " " )
190
+ if err != nil {
191
+ t .Fatalf ("MarshalIndent error: %v" , err )
192
+ }
193
+ if got := string (got ); got != want {
194
+ fmt .Println (got )
195
+ t .Errorf ("MarshalIndent:\n \t got: %s\n \t want: %s\n " , indentNewlines (got ), indentNewlines (want ))
196
+ }
197
+ }
198
+
199
+ type OptionalsEmptyZero struct {
200
+ Sr string `json:"sr"`
201
+ So string `json:"so,omitempty,omitzero"`
202
+ Sw string `json:"-"`
203
+
204
+ Io int `json:"io,omitempty,omitzero"`
205
+
206
+ Slr []string `json:"slr,random"`
207
+ Slo []string `json:"slo,omitempty,omitzero"`
208
+ SloNonNil []string `json:"slononnil,omitempty,omitzero"`
209
+
210
+ Mr map [string ]any `json:"mr"`
211
+ Mo map [string ]any `json:",omitempty,omitzero"`
212
+
213
+ Fr float64 `json:"fr"`
214
+ Fo float64 `json:"fo,omitempty,omitzero"`
215
+
216
+ Br bool `json:"br"`
217
+ Bo bool `json:"bo,omitempty,omitzero"`
218
+
219
+ Ur uint `json:"ur"`
220
+ Uo uint `json:"uo,omitempty,omitzero"`
221
+
222
+ Str struct {} `json:"str"`
223
+ Sto struct {} `json:"sto,omitempty,omitzero"`
224
+
225
+ Time time.Time `json:"time,omitempty,omitzero"`
226
+ Nzs NonZeroStruct `json:"nzs,omitempty,omitzero"`
227
+ }
228
+
229
+ func TestOmitEmptyZero (t * testing.T ) {
230
+ const want = `{
231
+ "sr": "",
232
+ "slr": null,
233
+ "mr": {},
234
+ "fr": 0,
235
+ "br": false,
236
+ "ur": 0,
237
+ "str": {},
238
+ "nzs": {}
239
+ }`
240
+ var o OptionalsEmptyZero
241
+ o .Sw = "something"
242
+ o .SloNonNil = make ([]string , 0 )
243
+ o .Mr = map [string ]any {}
244
+ o .Mo = map [string ]any {}
245
+
246
+ got , err := MarshalIndent (& o , "" , " " )
247
+ if err != nil {
248
+ t .Fatalf ("MarshalIndent error: %v" , err )
249
+ }
250
+ if got := string (got ); got != want {
251
+ t .Errorf ("MarshalIndent:\n \t got: %s\n \t want: %s\n " , indentNewlines (got ), indentNewlines (want ))
252
+ }
253
+ }
254
+
73
255
type StringTag struct {
74
256
BoolStr bool `json:",string"`
75
257
IntStr int64 `json:",string"`
0 commit comments