-
Notifications
You must be signed in to change notification settings - Fork 3
/
existingkeys_test.go
193 lines (171 loc) · 4.4 KB
/
existingkeys_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
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
package checkjson
import (
"fmt"
"testing"
)
func TestExistingJSONKeys(t *testing.T) {
fmt.Println("===================== TestExistingJSONKeys ...")
type test struct {
Ok bool
Why string
}
tv := test{}
data := []byte(`{"ok":true,"why":"it's a test"}`)
mems, err := ExistingJSONKeys(data, tv)
if err != nil {
t.Fatalf(err.Error())
}
// returns: [Ok Why]
if len(mems) != 2 {
t.Fatalf(fmt.Sprintf("len(mems) == %d >> %v", len(mems), mems))
}
data = []byte(`{"ok":true}`)
mems, err = ExistingJSONKeys(data, tv)
if err != nil {
t.Fatalf(err.Error())
}
if len(mems) != 1 {
t.Fatalf(fmt.Sprintf("existing mems: %d - %#v", len(mems), mems))
}
if mems[0] != "Ok" {
t.Fatalf(fmt.Sprintf("existing keys: %v", mems))
}
}
func TestExistingJSONSubKeys(t *testing.T) {
fmt.Println("===================== TestExistingJSONSubKeys ...")
type test3 struct {
Something string
Else string
}
type test2 struct {
Why string
Not string
Another test3
}
type test struct {
Ok bool
Why string
More test2
}
tv := test{}
data := []byte(`{"ok":true,"why":"it's a test","more":{"why":"again","not":"no more","another":{"something":"a thing","else":"ok"}}}`)
mems, err := ExistingJSONKeys(data, tv)
if err != nil {
t.Fatalf(err.Error())
}
if len(mems) != 8 {
t.Fatalf(fmt.Sprintf("len(mems) == %d >> %v", len(mems), mems))
}
data = []byte(`{"ok":true,"more":{"why":"again","another":{"else":"ok"}}}`)
mems, err = ExistingJSONKeys(data, tv)
if err != nil {
t.Fatalf(err.Error())
}
if len(mems) != 5 {
t.Fatalf(fmt.Sprintf("existing mems: %d - %#v", len(mems), mems))
}
fmt.Println("existing keys:", mems)
}
func TestExistingJSONKeysWithTags(t *testing.T) {
fmt.Println("===================== TestExistingJSONKeysWithTags ...")
type test struct {
Ok bool
Why string `json:"whynot"`
}
tv := test{}
data := []byte(`{"ok":true,"whynot":"it's a test"}`)
mems, err := ExistingJSONKeys(data, tv)
if err != nil {
t.Fatalf(err.Error())
}
if len(mems) != 2 {
t.Fatalf(fmt.Sprintf("len(mems) == %d >> %v", len(mems), mems))
}
fmt.Println("existing keys:", mems)
data = []byte(`{"ok":true,"why":"it's not a test"}`)
mems, err = ExistingJSONKeys(data, tv)
if err != nil {
t.Fatalf(err.Error())
}
if len(mems) != 1 || mems[0] != "Ok" {
t.Fatalf(fmt.Sprintf("existing mems: %d - %#v", len(mems), mems))
}
fmt.Println("existing keys:", mems)
}
func TestExistingJSONKeysSkipMems(t *testing.T) {
fmt.Println("===================== TestExistingJSONKeysSkipMems ...")
type test3 struct {
Something string
Else string
}
type test2 struct {
Why string
Not string
Another test3
}
type test struct {
Ok bool
Why string
More test2
}
tv := test{}
data := []byte(`{"ok":true,"more":{"why":"again","another":{"else":"ok"}}}`)
SetMembersToIgnore("ok", "more.why")
defer SetMembersToIgnore()
mems, err := ExistingJSONKeys(data, tv)
if err != nil {
t.Fatalf(err.Error())
}
if len(mems) != 3 {
t.Fatalf(fmt.Sprintf("existing mems: %d - %#v", len(mems), mems))
}
fmt.Println("existing keys:", mems)
}
func TestExistingJSONKeysWithIgnoreTag(t *testing.T) {
fmt.Println("===================== TestExistingJSONKeysWithIgnoreTag ...")
type test struct {
Ok bool
Why string
Whynot string `json:"-"`
}
tv := test{}
data := []byte(`{"ok":true,"why":"it's a test"}`)
mems, err := ExistingJSONKeys(data, tv)
if err != nil {
t.Fatalf(err.Error())
}
if len(mems) != 2 {
t.Fatalf(fmt.Sprintf("len(mems) == %d >> %v", len(mems), mems))
}
fmt.Println("existing keys:", mems)
}
func TestExistingJSONKeysWithOmitemptyAttr(t *testing.T) {
fmt.Println("===================== TestExistingJSONKeysWithmitemptyIgnoreAttr ...")
type test struct {
Ok bool
Why string
Whynot string `json:",omitempty"`
}
tv := test{}
data := []byte(`{"ok":true,"why":"it's a test"}`)
IgnoreOmitemptyTag(true) // make sure it's set
mems, err := ExistingJSONKeys(data, tv)
if err != nil {
t.Fatalf(err.Error())
}
if len(mems) != 3 {
t.Fatalf(fmt.Sprintf("len(mems) == %d >> %v", len(mems), mems))
}
if mems[2] != "Whynot" {
t.Fatalf("ExistingJSONKeys did't get: Whynot")
}
IgnoreOmitemptyTag(false) // ignore attribute
defer IgnoreOmitemptyTag(true) // reset on return
mems, err = ExistingJSONKeys(data, tv)
if err != nil {
t.Fatalf(err.Error())
}
if len(mems) != 2 {
t.Fatalf(fmt.Sprintf("len(mems) == %d >> %v", len(mems), mems))
}
}