-
Notifications
You must be signed in to change notification settings - Fork 11
/
dig_test.go
462 lines (403 loc) · 9.86 KB
/
dig_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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// Copyright (c) 2013-today José Nieto, https://xiam.dev
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package dig
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
var jsonTest = `{
"Bool": false,
"Int": 0,
"Int8": 0,
"Int16": 0,
"Int32": 0,
"Int64": 0,
"Uint": 0,
"Uint8": 0,
"Uint16": 0,
"Uint32": 0,
"Uint64": 0,
"Uintptr": 0,
"Float32": 0,
"Float64": 0,
"bar": "",
"bar2": "",
"IntStr": "0",
"PBool": true,
"PInt": 2,
"PInt8": 3,
"PInt16": 4,
"PInt32": 5,
"PInt64": 6,
"PUint": 7,
"PUint8": 8,
"PUint16": 9,
"PUint32": 10,
"PUint64": 11,
"PUintptr": 12,
"PFloat32": 14.1,
"PFloat64": 15.1,
"String": "",
"PString": "16",
"Map": null,
"MapP": null,
"PMap": {
"17": {
"Tag": "tag17"
},
"18": {
"Tag": "tag18"
}
},
"PMapP": {
"19": {
"Tag": "tag19"
},
"20": null
},
"EmptyMap": null,
"NilMap": null,
"Slice": null,
"SliceP": null,
"PSlice": [
{
"Tag": "tag20"
},
{
"Tag": "tag21"
}
],
"PSliceP": [
{
"Tag": "tag22"
},
null,
{
"Tag": "tag23"
}
],
"EmptySlice": null,
"NilSlice": null,
"StringSlice": null,
"ByteSlice": null,
"Small": {
"Tag": ""
},
"PSmall": null,
"PPSmall": {
"Tag": "tag31"
},
"Interface": null,
"PInterface": 5.2
}`
func TestList(t *testing.T) {
var err error
var i int
list := []int{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
}
err = Get(&list, &i, 5)
assert.NoError(t, err)
assert.Equal(t, 5, i)
}
func TestMatrix(t *testing.T) {
var err error
var i int
m33 := [][]int{
[]int{0, 1, 2},
[]int{3, 4, 5},
[]int{6, 7, 8},
}
err = Get(&m33, &i, 1, 1)
assert.NoError(t, err)
assert.Equal(t, 4, i)
err = Get(&m33, &i, 2, 0)
assert.NoError(t, err)
assert.Equal(t, 6, i)
// Path does not exist
err = Get(&m33, &i, 9, 9)
assert.Error(t, err)
assert.Equal(t, 0, i)
// Not assignable
err = Get(&m33, &i, 1)
assert.Error(t, err)
assert.Equal(t, 0, i)
}
func TestFloatMatrix(t *testing.T) {
var err error
var f float64
mf33 := [][]float64{
[]float64{0.0, 1.0, 2.0},
[]float64{3.0, 4.0, 5.0},
[]float64{6.0, 7.0, 8.0},
}
err = Get(&mf33, &f, 1, 2)
assert.NoError(t, err)
assert.Equal(t, 5.0, f)
}
func TestReddit(t *testing.T) {
var s = `
{
"kind":"Listing",
"data":{
"modhash":"",
"children":[
{
"kind":"t3",
"data":{
"domain":"github.com",
"banned_by":null,
"media_embed":{
},
"subreddit":"golang",
"selftext_html":null,
"selftext":"",
"likes":null,
"link_flair_text":null,
"id":"1bep1x",
"clicked":false,
"title":"Easy GOPATH management for Go(lang) projects",
"media":null,
"score":9,
"approved_by":null,
"over_18":false,
"hidden":false,
"thumbnail":"",
"subreddit_id":"t5_2rc7j",
"edited":false,
"link_flair_css_class":null,
"author_flair_css_class":null,
"downs":4,
"saved":false,
"is_self":false,
"permalink":"/r/golang/comments/1bep1x/easy_gopath_management_for_golang_projects/",
"name":"t3_1bep1x",
"created":1364825519.0,
"url":"https://github.com/divoxx/goproj",
"author_flair_text":null,
"author":"divoxx",
"created_utc":1364796719.0,
"ups":13,
"num_comments":17,
"num_reports":null,
"distinguished":null
}
},
{
"kind":"t3",
"data":{
"domain":"xaprb.com",
"banned_by":null,
"media_embed":{
},
"subreddit":"golang",
"selftext_html":null,
"selftext":"",
"likes":null,
"link_flair_text":null,
"id":"1anxb9",
"clicked":false,
"title":"Building MySQL Database Applications with Go",
"media":null,
"score":18,
"approved_by":null,
"over_18":false,
"hidden":false,
"thumbnail":"",
"subreddit_id":"t5_2rc7j",
"edited":false,
"link_flair_css_class":null,
"author_flair_css_class":null,
"downs":3,
"saved":false,
"is_self":false,
"permalink":"/r/golang/comments/1anxb9/building_mysql_database_applications_with_go/",
"name":"t3_1anxb9",
"created":1363819085.0,
"url":"http://www.xaprb.com/blog/2013/03/20/building-mysql-database-applications-with-go/",
"author_flair_text":null,
"author":"franciscosouza",
"created_utc":1363790285.0,
"ups":21,
"num_comments":6,
"num_reports":null,
"distinguished":null
}
}
],
"after":"t3_1aewpb",
"before":null
}
}
`
var m map[string]interface{}
err := json.Unmarshal([]byte(s), &m)
assert.NoError(t, err)
var children []interface{}
err = Get(&m, &children, "data", "children")
assert.NoError(t, err)
for i := range children {
child := children[i].(map[string]interface{})
val := Float64(&child, "data", "created")
assert.NotZero(t, val)
}
}
func TestMap(t *testing.T) {
var err error
var s string
m := map[string]string{
"Hello": "World",
}
// Simple assignment
err = Get(&m, &s, "Hello")
assert.NoError(t, err)
assert.Equal(t, "World", s)
m2 := map[string]map[string]map[string]string{
"first": map[string]map[string]string{
"first.1": map[string]string{
"col.1": "a",
"col.2": "b",
"col.3": "c",
},
},
"second": map[string]map[string]string{
"second.2": map[string]string{
"col.4": "d",
"col.5": "e",
"col.6": "f",
},
},
}
// Nested keys
err = Get(&m2, &s, "second", "second.2", "col.4")
assert.NoError(t, err)
assert.Equal(t, "d", s)
// Non existent key
err = Get(&m2, &s, "third", "doest", "not", "exists")
assert.Error(t, err)
assert.Equal(t, "", s)
}
func TestJSON(t *testing.T) {
var m map[string]interface{}
err := json.Unmarshal([]byte(jsonTest), &m)
assert.NoError(t, err)
var s string
err = Get(&m, &s, "PMap", "17", "Tag")
assert.NoError(t, err)
assert.Equal(t, "tag17", s)
var f32 float32
err = Get(&m, &f32, "PFloat32")
assert.NoError(t, err)
assert.Equal(t, float32(14.1), f32)
var f64 float64
err = Get(&m, &f64, "PFloat32")
assert.NoError(t, err)
assert.Equal(t, float64(14.1), f64)
var b bool
err = Get(&m, &b, "PBool")
assert.NoError(t, err)
assert.Equal(t, true, b)
var ui64 uint64
err = Get(&m, &ui64, "PUint64")
assert.NoError(t, err)
assert.Equal(t, uint64(11), ui64)
var i int
err = Get(&m, &ui64, "String")
assert.NoError(t, err)
assert.Zero(t, i)
err = Get(&m, &s, "PSlice", 1, "Tag")
assert.NoError(t, err)
assert.Equal(t, "tag21", s)
}
func TestJSON2(t *testing.T) {
var m map[string]interface{}
err := json.Unmarshal([]byte(jsonTest), &m)
assert.NoError(t, err)
foo := map[string]string{
"test": String(&m, "PMap", "17", "Tag"),
}
assert.NoError(t, err)
assert.Equal(t, "tag17", foo["test"])
}
func TestSet(t *testing.T) {
var i int
var m = map[string]interface{}{
"path": map[string]interface{}{
"to": map[string]interface{}{
"variable": 2,
},
},
}
err := Set(&m, 1, "path", "to", "variable")
assert.NoError(t, err)
err = Get(&m, &i, "path", "to", "variable")
assert.NoError(t, err)
assert.Equal(t, 1, i)
err = Set(&m, 42, "path", "to", "non", "existent", "key")
assert.Error(t, err)
}
func TestSetN(t *testing.T) {
var m = map[string]map[string]map[string]int{
"path": map[string]map[string]int{
"to": map[string]int{
"variable": 2,
},
},
}
var i int
err := Set(&m, 42, "path", "to", "variable")
assert.NoError(t, err)
err = Get(&m, &i, "path", "to", "variable")
assert.NoError(t, err)
assert.Equal(t, 42, i)
}
func TestSetSimpleMap(t *testing.T) {
var m = map[string]int{
"key": 2,
}
var i int
err := Set(&m, 42, "key")
assert.NoError(t, err)
err = Get(&m, &i, "key")
assert.NoError(t, err)
assert.Equal(t, 42, i)
err = Set(&m, 43, "does", "not-exists")
assert.Error(t, err)
err = Set(&m, 44, "new-key")
assert.NoError(t, err)
err = Get(&m, &i, "new-key")
assert.NoError(t, err)
assert.Equal(t, 44, i)
err = Set(&m, 44)
assert.Error(t, err)
}
func TestDig(t *testing.T) {
var i int
m := map[string]interface{}{}
err := Dig(&m, "foo", "bar", "baz")
assert.NoError(t, err)
err = Set(&m, 42, "foo", "bar", "baz")
assert.NoError(t, err)
err = Get(&m, &i, "foo", "bar", "baz")
assert.NoError(t, err)
assert.Equal(t, 42, i)
}