-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjson_omitempty_test.go
152 lines (144 loc) · 4.31 KB
/
json_omitempty_test.go
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
package goson
import (
"encoding/json"
"testing"
)
func TestMakeZeroOmitempty(t *testing.T) {
type Child struct {
ChildA string `json:"childA"`
ChildB string `json:"childB,omitempty"`
ChildStruct struct {
A int `json:"A,omitempty"`
B int `json:"B"`
} `json:"childStruct"`
}
type arr struct {
Arr1 string `json:"arr1"`
Arr2 string `json:"arr2,omitempty"`
}
type obj struct {
Name string `json:"name"`
Additional string `json:"additional,omitempty"`
AddStruct struct {
Field1 string `json:"field1,omitempty"`
Field2 string `json:"field2"`
} `json:"addStruct"`
AddChildPtr *Child `json:"child"`
Slice []arr `json:"slice"`
Nullable *string `json:"nullable,omitempty"`
}
/*
create example object with nested type & struct, json string from value in this object is:
{
"name": "Test",
"additional": "test lagi (value in this field will removed because contains omitempty in json tag)",
"addStruct": {
"field1": "Field 1 (value in this field will removed because contains omitempty in json tag)",
"field2": "Field 2"
},
"child": {
"childA": "Child A",
"childB": "Child B (value in this field will removed because contains omitempty in json tag)",
"childStruct": {
"A": 453, // value in this field will removed because contains omitempty in json tag
"B": 567
}
},
"slice": [
{
"arr1": "Test field slice 1",
"arr2": "Test field slice 2 (value in this field will removed because contains omitempty in json tag)"
}
]
}
result after make zero field contains omitempty is:
{
"name": "Test",
"addStruct": {
"field2": "Field 2"
},
"child": {
"childA": "Child A",
"childStruct": {
"B": 567
}
},
"slice": [
{
"arr1": "Test field slice 1"
}
]
}
*/
objExample := obj{
Name: "Test",
Additional: "test lagi (value in this field will removed because contains omitempty in json tag)",
AddStruct: struct {
Field1 string `json:"field1,omitempty"`
Field2 string `json:"field2"`
}{
Field1: "Field 1 (value in this field will removed because contains omitempty in json tag)",
Field2: "Field 2",
},
AddChildPtr: &Child{
ChildA: "Child A",
ChildB: "Child B (value in this field will removed because contains omitempty in json tag)",
ChildStruct: struct {
A int `json:"A,omitempty"`
B int `json:"B"`
}{
A: 453,
B: 567,
},
},
Slice: []arr{arr{
Arr1: "Test field slice 1",
Arr2: "Test field slice 2 (value in this field will removed because contains omitempty in json tag)",
},
},
}
tests := []struct {
name string
args interface{}
wantResult string
wantErr bool
}{
{
name: "Testcase #1",
args: &objExample,
wantResult: `{"name":"Test","addStruct":{"field2":"Field 2"},"child":{"childA":"Child A","childStruct":{"B":567}},"slice":[{"arr1":"Test field slice 1"}]}`,
},
{
name: "Testcase #2, argument is slice",
args: []*obj{&objExample},
wantResult: `[{"name":"Test","addStruct":{"field2":"Field 2"},"child":{"childA":"Child A","childStruct":{"B":567}},"slice":[{"arr1":"Test field slice 1"}]}]`,
},
{
name: "Testcase #3, argument is map of slice",
args: map[string][]*obj{"aa": []*obj{&objExample}},
wantResult: `{"aa":[{"name":"Test","addStruct":{"field2":"Field 2"},"child":{"childA":"Child A","childStruct":{"B":567}},"slice":[{"arr1":"Test field slice 1"}]}]}`,
},
{
name: "Testcase #4, argument is map of object include struct",
args: map[string]*obj{"aa": &objExample},
wantResult: `{"aa":{"name":"Test","addStruct":{"field2":"Field 2"},"child":{"childA":"Child A","childStruct":{"B":567}},"slice":[{"arr1":"Test field slice 1"}]}}`,
},
{
name: "Testcase #6 want panic, invalid data type argument",
args: obj{Name: "Test", Additional: "test lagi"},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := MakeZeroOmitempty(tt.args)
if (err != nil) != tt.wantErr {
t.Errorf("error is unexpected, expect error is %v, error is: %v", tt.wantErr, err)
}
jsonAfter, _ := json.Marshal(tt.args)
if tt.wantResult != "" && string(jsonAfter) != tt.wantResult {
t.Errorf("expected %s, got %s", tt.wantResult, string(jsonAfter))
}
})
}
}