-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonomit_test.go
More file actions
267 lines (242 loc) · 6.18 KB
/
jsonomit_test.go
File metadata and controls
267 lines (242 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package jsonomit
import (
"encoding/json"
"testing"
"time"
)
type customStruct struct {
Value any
valid bool
}
func (s customStruct) MarshalJSON() ([]byte, error) {
if !s.valid { // logic to set the struct to null.
return []byte("null"), nil
}
return json.Marshal(s.Value)
}
type nestedStruct struct {
T time.Time
TEmpty time.Time
StringTrap string `json:",omitempty"`
Num float64
NumEmpty float64
Custom customStruct
CustomEmpty customStruct
StructEmpty struct{}
}
type testStruct struct {
T time.Time
TEmpty time.Time
StringTrap string `json:",omitempty"`
Num float64
NumEmpty float64 // missing omitempty
Custom customStruct
CustomEmpty customStruct
Nested nestedStruct
NestedEmpty nestedStruct
StructEmpty struct{}
}
type icon struct {
Pixels []uint16 // missing omitempty
ImgSize point // missing omitempty
}
type point struct {
X, Y int // missing omitempty
}
var (
ts = time.Unix(0, 0).UTC()
allTogether = testStruct{
T: ts,
TEmpty: time.Time{},
StringTrap: `"Time":"0001-01-01T00:00:00Z"`,
Num: 0.1,
Custom: customStruct{
Value: "value",
valid: true,
},
CustomEmpty: customStruct{
Value: "empty",
valid: false,
},
Nested: nestedStruct{
T: ts,
TEmpty: time.Time{},
StringTrap: `"MyStruct":null`,
Custom: customStruct{
Value: "value",
valid: true,
},
CustomEmpty: customStruct{
Value: "empty",
valid: false,
},
},
NestedEmpty: nestedStruct{
TEmpty: time.Time{},
CustomEmpty: customStruct{
Value: "empty",
valid: false,
},
},
}
)
func TestMarshal(t *testing.T) {
// Time
b, err := Marshal(
struct {
T time.Time
}{},
)
if err != nil {
t.Fatal(err)
}
want := `{}`
if string(b) != want {
t.Fatalf("Failed 'time' want!\nWanted:\n%s\nGot:\n%s", want, string(b))
} else {
t.Log("Clean time: OK!")
}
// Custom marshal
b, err = Marshal(customStruct{Value: "value", valid: false})
if err != nil {
t.Fatal(err)
}
want = `null`
if string(b) != want {
t.Fatalf("Failed 'custom marshal' want!\nWanted:\n%s\nGot:\n%s", want, string(b))
} else {
t.Logf("Custom marshal: OK!")
}
// Custom in a struct
b, err = Marshal(struct{ Custom customStruct }{Custom: customStruct{Value: "value", valid: false}})
if err != nil {
t.Fatal(err)
}
want = `{}`
if string(b) != want {
t.Fatalf("Failed 'custom marshal in a struct' want!\nWanted:\n%s\nGot:\n%s", want, string(b))
} else {
t.Logf("Custom marshal: OK!")
}
// Nested empty.
b, err = Marshal(struct{ _ struct{ _ struct{} } }{struct{ _ struct{} }{struct{}{}}})
if err != nil {
t.Fatal(err)
}
want = `{}`
if string(b) != want {
t.Fatalf("Failed 'nested empty' want!\nWanted:\n%s\nGot:\n%s", want, string(b))
} else {
t.Logf("Nested empty: OK!")
}
// Slice of empty structs.
b, err = Marshal([]struct{}{{}, {}, {}})
if err != nil {
t.Fatal(err)
}
want = `[{},{},{}]`
if string(b) != want {
t.Fatalf("Failed 'slice of empty structs' want!\n%s\n%s", want, string(b))
} else {
t.Logf("Slice of empty struct: OK!")
}
// Map of empty structs.
b, err = Marshal(map[string]struct{}{"a": {}, "b": {}, "c": {}})
if err != nil {
t.Fatal(err)
}
want = `{}`
if string(b) != want {
t.Fatalf("Failed 'map of empty structs' want!\n%s\n%s", want, string(b))
} else {
t.Logf("Map of empty struct: OK!")
}
// All together
b, err = Marshal(allTogether)
if err != nil {
t.Fatal(err)
}
want = `{"T":"1970-01-01T00:00:00Z","StringTrap":"\"Time\":\"0001-01-01T00:00:00Z\"","Num":0.1,"Custom":"value","Nested":{"T":"1970-01-01T00:00:00Z","StringTrap":"\"MyStruct\":null","Custom":"value"}}`
if string(b) != want {
t.Fatalf("Failed 'all together' want!\n%s\n%s", want, string(b))
} else {
t.Logf("All together: OK!")
}
// Icon
b, err = Marshal(icon{})
if err != nil {
t.Fatal(err)
}
want = `{}`
if string(b) != want {
t.Fatalf("Failed 'icon' want!\n%s\n%s", want, string(b))
} else {
t.Logf("Icon: OK!")
}
}
func TestMarshalIndent(t *testing.T) {
b, err := MarshalIndent(allTogether, "", " ")
if err != nil {
t.Fatal(err)
}
want := `{
"T": "1970-01-01T00:00:00Z",
"StringTrap": "\"Time\":\"0001-01-01T00:00:00Z\"",
"Num": 0.1,
"Custom": "value",
"Nested": {
"T": "1970-01-01T00:00:00Z",
"StringTrap": "\"MyStruct\":null",
"Custom": "value"
}
}`
if string(b) != want {
t.Fatalf("Failed 'all together' want!\n%s\n%s", want, string(b))
}
}
func TestMarshalCustom(t *testing.T) {
// With values
b, err := MarshalCustom(allTogether, OptionTime, OptionZeroNum, OptionStruct)
if err != nil {
t.Fatal(err)
}
want := `{"T":"1970-01-01T00:00:00Z","StringTrap":"\"Time\":\"0001-01-01T00:00:00Z\"","Num":0.1,"Custom":"value","CustomEmpty":null,"Nested":{"T":"1970-01-01T00:00:00Z","StringTrap":"\"MyStruct\":null","Custom":"value","CustomEmpty":null},"NestedEmpty":{"Custom":null,"CustomEmpty":null}}`
if string(b) != want {
t.Fatalf("Failed 'all together' want!\nWanted:\n%s\nGot:\n%s", want, string(b))
} else {
t.Log("With values: OK!")
}
// Empty testStruct
b, err = MarshalCustom(testStruct{}, OptionTime, OptionZeroNum, OptionStruct)
if err != nil {
t.Fatal(err)
}
want = `{"Custom":null,"CustomEmpty":null,"Nested":{"Custom":null,"CustomEmpty":null},"NestedEmpty":{"Custom":null,"CustomEmpty":null}}`
if string(b) != want {
t.Fatalf("Failed 'empty testStruct' want!\nWanted:\n%s\nGot:\n%s", want, string(b))
} else {
t.Log("Empty testStruct: OK!")
}
// Map of time structs.
b, err = MarshalCustom(map[string]struct{ T time.Time }{"a": {ts}, "b": {}, "c": {}}, OptionTime)
if err != nil {
t.Fatal(err)
}
want = `{"a":{"T":"1970-01-01T00:00:00Z"},"b":{},"c":{}}`
if string(b) != want {
t.Fatalf("Failed 'map of time structs' want!\n%s\n%s", want, string(b))
} else {
t.Logf("Map of time struct: OK!")
}
// Map of time structs.
b, err = MarshalCustom(map[string]struct{ T time.Time }{"a": {ts}, "b": {}, "c": {}}, OptionTime, OptionStruct)
if err != nil {
t.Fatal(err)
}
want = `{"a":{"T":"1970-01-01T00:00:00Z"}}`
if string(b) != want {
t.Fatalf("Failed 'map of time structs #2' want!\n%s\n%s", want, string(b))
} else {
t.Logf("Map of time struct #2: OK!")
}
}