-
Notifications
You must be signed in to change notification settings - Fork 5
/
get_test.go
103 lines (82 loc) · 3.72 KB
/
get_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
package lenspath
import (
"testing"
)
type TestStruct struct {
Name string
Count int
Additional TestStructNested
}
type TestStructNested struct {
Code string
Addi *TestStructNested
}
func TestStructGet(t *testing.T) {
// 1. top level struct field getting
checkGetWithLensPath(t, TestStruct{Name: "test"}, []string{"Name"}, "test", false, false, false)
// 2. nested struct field getting
checkGetWithLensPath(t,
TestStruct{Count: 1, Additional: TestStructNested{Code: "test2"}},
[]string{"Additional", "Code"}, "test2", false, false, false)
// 3. nested struct field getting (with pointers)
t0 := TestStructNested{Code: "test1"}
t1 := TestStructNested{Code: "test2", Addi: &t0}
t2 := TestStructNested{Code: "test3", Addi: &t1}
ts := TestStruct{Count: 1, Additional: t2}
checkGetWithLensPath(t,
ts,
[]string{"Additional", "Addi", "Addi", "Code"},
"test1", false, false, false)
// 4. error expected
checkGetWithLensPath(t, TestStruct{Name: "test"}, []string{}, "", true, false, false)
// 5.1 lenspath not exhausted
checkGetWithLensPath(t, ts, []string{"Additional", "Addi", "Addi", "Addi", "Code"}, nil, false, false, true)
}
func TestMapGet(t *testing.T) {
data := getTestMap()
// 1. top level map field getting
checkGetWithLensPath(t, data, []string{"name"}, "chacha", false, false, false)
// 2. nested map field getting
checkGetWithLensPath(t, data, []string{"additional", "birthmark"}, "cut on the left hand", false, false, false)
// 3. get all array field
checkGetWithLensPath(t, data, []string{"additional", "tagsList", "*", "tag_h"}, []any{"medium", "tall"}, false, false, false)
// 4 get all array field (but not elements have the queried nested field)
checkGetWithLensPath(t, data, []string{"additional", "tagsList", "*", "tag_w"}, []any{nil, "heavy"}, false, false, true)
// 4.1 with assumeNil
checkGetWithLensPath(t, data, []string{"additional", "tagsList", "*", "tag_w"}, []any{nil, "heavy"}, false, false, true)
checkGetWithLensPath(t, data, []string{"additional", "addi", "nonexisting", "extra"}, nil, false, false, true)
checkGetWithLensPath(t, data, []string{"additional", "tagsList2", "*", "tag_n", "tag_n_1"}, []any{1, nil}, false, false, true)
checkGetWithLensPath(t, data, []string{"additional", "tagsList2", "*", "tag_n", "tag_n_2"}, []any{"2-string", "2-string"}, false, false, true)
checkGetWithLensPath(t, data, []string{"additional", "tagsList2", "*", "tag_n", "tag_n_3"}, []any{3.0, "3.0-string"}, false, false, true)
// the leaves of the tree should be reported in a flattened array
checkGetWithLensPath(t, data, []string{"additional", "tagsList5", "*", "tag_n", "tag_n", "*", "tag_n_1"}, []any{1, 3}, false, false, true)
}
func getTestMap() map[string]any {
tagsList := []map[string]string{
{"tag_h": "medium"},
{"tag_w": "heavy", "tag_h": "tall"},
}
data := map[string]any{
"name": "chacha",
"region": "India",
"additional": map[string]any{
"birthmark": "cut on the left hand",
"addi": map[string]string{
"code": "334532",
"landmark": "near the forest entry",
},
"tagsList": tagsList,
"tagsList2": []map[string]any{
{"tag_h": "medium", "tag_n": map[string]any{"tag_n_1": 1, "tag_n_2": "2-string", "tag_n_3": 3.0}},
{"tag_w": "heavy", "tag_h": "tall", "tag_n": map[string]any{"tag_n_2": "2-string", "tag_n_3": "3.0-string"}},
},
"tagsList5": []map[string]any{
{"tag_h": "medium", "tag_n": map[string]any{"tag_n": []map[string]any{{"tag_n_1": 1, "tag_n_2": "2-string", "tag_n_3": 3.0}}}},
{"tag_w": "heavy", "tag_h": "tall", "tag_n": map[string]any{"tag_n": []map[string]any{{"tag_n_1": 3, "tag_n_2": "2-string", "tag_n_3": 3.0}}}},
},
"tagsList3": "hello world",
"tagsList4": []string{"hello", "world", "hw"},
},
}
return data
}