-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjsonpath_test.go
194 lines (167 loc) · 4.56 KB
/
jsonpath_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
194
package jsonpath
import (
"encoding/json"
"reflect"
"testing"
)
var data = map[string]interface{}{
"user": map[string]interface{}{
"firstname": "seth",
"lastname": "rogen",
},
"age": 35,
"filmography": map[string]interface{}{
"movies": []string{
"This Is The End",
"Superbad",
"Neighbors",
},
},
}
func TestGet(t *testing.T) {
result, err := Get(data, "user.firstname")
if err != nil {
t.Errorf("failed to get user.firstname")
}
if result != "seth" {
t.Errorf("wrong get value, wanted %v, got %v", "seth", result)
}
result, err = Get(data, "filmography.movies[1]")
if err != nil {
t.Errorf("failed to get filmography.movies[1]")
}
if result != "Superbad" {
t.Errorf("wrong get value, wanted %v, got %v", "Superbad", result)
}
result, err = Get(data, "age")
if err != nil {
t.Errorf("failed to get age: %v", err)
}
if result != 35 {
t.Errorf("wrong get value, wanted: %v, got: %v", 35, result)
}
result, err = Get(data, "this.does.not[0].exist")
if _, ok := err.(DoesNotExist); result != nil || !ok {
t.Errorf("does not handle non-existent path correctly")
}
}
func TestSet(t *testing.T) {
err := Set(&data, "user.firstname", "chris")
if err != nil {
t.Errorf("failed to set user.firstname: %v", err)
}
firstname := reflect.ValueOf(data["user"]).MapIndex(reflect.ValueOf("firstname")).Interface()
if firstname != "chris" {
t.Errorf("set user.firstname to wrong value, wanted: %v, got: %v", "chris", firstname)
}
err = Set(&data, "filmography.movies[2]", "The Disaster Artist")
if err != nil {
t.Errorf("failed to set filmography.movies[2]: %v", err)
}
secondMovie := reflect.ValueOf(data["filmography"]).MapIndex(reflect.ValueOf("movies")).Elem().Index(2).Interface()
if secondMovie != "The Disaster Artist" {
t.Errorf("set filmography.movies[2] to wrong value, wanted: %v, got %v", "The Disaster Artist", secondMovie)
}
newUser := map[string]interface{}{
"firstname": "james",
"lastname": "franco",
}
err = Set(&data, "user", &newUser)
if err != nil {
t.Errorf("failed to set user: %v", err)
}
user := data["user"]
if !reflect.DeepEqual(newUser, user) {
t.Errorf("set user is not equal, wanted: %v, got %v", newUser, user)
}
newData := map[string]interface{}{
"hello": 12,
}
err = Set(&data, "this.does.not[0].exist", newData)
if err != nil {
t.Errorf("failed to set: %v", err)
} else {
exist := reflect.ValueOf(data["this"]).MapIndex(reflect.ValueOf("does")).Elem().MapIndex(reflect.ValueOf("not")).Elem().Index(0).Elem().MapIndex(reflect.ValueOf("exist")).Interface()
if !reflect.DeepEqual(exist, newData) {
t.Errorf("setting a nonexistant field did not work well, wanted: %#v, got %#v", newData, exist)
}
}
}
func TestJSON(t *testing.T) {
test := `
{
"pet": {
"name": "baxter",
"owner": {
"name": "john doe",
"contact": {
"phone": "859-289-9290"
}
},
"type": "dog",
"age": "4"
},
"tags": [
12,
true,
{
"hello": [
"world"
]
}
]
}
`
var payload interface{}
err := json.Unmarshal([]byte(test), &payload)
if err != nil {
t.Errorf("failed to parse: %v", err)
}
result, _ := Get(payload, "tags[2].hello[0]")
if result != "world" {
t.Errorf("got wrong value from path, wanted: %v, got: %v", "world", result)
}
err = Set(&payload, "tags[2].hello[0]", "bobby")
if err != nil {
t.Errorf("failed to set: %v", err)
}
result, _ = Get(payload, "tags[2].hello[0]")
if result != "bobby" {
t.Errorf("got wrong value after setting, wanted: %v, got: %v", "bobby", result)
}
newContact := map[string]string{
"phone": "555-555-5555",
"email": "baxterowner@johndoe.com",
}
err = Set(&payload, "pet.owner.contact", newContact)
if err != nil {
t.Errorf("failed to set: %v", err)
}
contact, _ := Get(&payload, "pet.owner.contact")
if !reflect.DeepEqual(newContact, contact) {
t.Errorf("contact set do not equal, wanted: %v, got %v", newContact, contact)
}
small := `{}`
err = json.Unmarshal([]byte(small), &payload)
if err != nil {
t.Errorf("failed to parse: %v", err)
}
err = Set(&payload, "this.is.new[3]", map[string]interface{}{
"hello": "world",
})
if err != nil {
t.Errorf("setting a nonexistant field did not work well, %v", err)
}
b, _ := json.Marshal(payload)
output := string(b)
expected := `{"this":{"is":{"new":[null,null,null,{"hello":"world"}]}}}`
if output != expected {
t.Errorf("did not set correctly, wanted: %v, got: %v", expected, output)
}
}
func TestErrors(t *testing.T) {
_, err := Get(data, "where.is.this")
if _, ok := err.(DoesNotExist); !ok && err != nil {
t.Errorf("error retrieving value %v", err)
}
}