-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmapaccess_test.go
More file actions
157 lines (139 loc) · 3.73 KB
/
mapaccess_test.go
File metadata and controls
157 lines (139 loc) · 3.73 KB
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
package mapaccess
import (
"fmt"
"html/template"
"io"
"reflect"
"testing"
)
type Data struct {
Array []string
Nested Nested
}
type Nested struct {
Array []string
}
var typed = Data{
Array: []string{"value"},
Nested: Nested{
Array: []string{"four"},
},
}
var data = map[string]interface{}{
"array": []interface{}{"value"},
"one": "two",
"nested": map[string]interface{}{
"key": "three",
"array": []interface{}{"four"},
},
}
func TestGet(t *testing.T) {
type args struct {
key string
data interface{}
}
tests := []struct {
name string
args args
want interface{}
wantErr bool
}{
{"root", args{"one", data}, "two", false},
{"root array", args{"array[0]", data}, "value", false},
{"nested", args{"nested.key", data}, "three", false},
{"nested array", args{"nested.array[0]", data}, "four", false},
{"spaces", args{" one.two[0]", data}, nil, true},
{"spaces", args{"o.test.", data}, nil, true},
{"spaces", args{"[0]", data}, nil, true},
{"nested array missing", args{"one.two[1]", data}, nil, true},
{"root missing", args{"two", data}, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Get(tt.args.data, tt.args.key)
if (err != nil) != tt.wantErr {
t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Get() = %v, want %v", got, tt.want)
}
})
}
}
func TestGetAs(t *testing.T) {
type args struct {
key string
data interface{}
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{"root", args{"one", data}, "two", false},
{"root array", args{"array[0]", data}, "value", false},
{"nested", args{"nested.key", data}, "three", false},
{"nested array", args{"nested.array[0]", data}, "four", false},
{"spaces", args{" one.two[0]", data}, "", true},
{"spaces", args{"o.test.", data}, "", true},
{"spaces", args{"[0]", data}, "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetAs[string](tt.args.data, tt.args.key)
if (err != nil) != tt.wantErr {
t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Get() = %v, want %v", got, tt.want)
}
})
}
}
func TestGetAsInt(t *testing.T) {
m := map[string]interface{}{
"one": 1,
"two": 2.2,
"three": 0.3,
}
if val, err := GetAs[int](m, "one"); err != nil {
t.Errorf("GetAs() error = %v", err)
} else if val != 1 {
t.Errorf("GetAs() = %v, want %v", val, 1)
}
if val, err := GetAs[float64](m, "two"); err != nil {
t.Errorf("GetAs() error = %v", err)
} else if val != 2.2 {
t.Errorf("GetAs() = %v, want %v", val, 2.2)
}
if val, err := GetAs[float64](m, "three"); err != nil {
t.Errorf("GetAs() error = %v", err)
} else if val != 0.3 {
t.Errorf("GetAs() = %v, want %v", val, 0.3)
}
}
func benchmarkMapaccess(key string, b *testing.B) {
for n := 0; n < b.N; n++ {
Get(data, key)
}
}
func benchmarkGoTemplate(key string, b *testing.B) {
for n := 0; n < b.N; n++ {
t, err := template.New("tmpl").Parse(key)
if err != nil {
fmt.Println(err)
}
t.Execute(io.Discard, typed)
}
}
func BenchmarkMapaccessRootKey(b *testing.B) { benchmarkMapaccess("nested", b) }
func BenchmarkMapaccessNestedKey(b *testing.B) { benchmarkMapaccess("nested.array", b) }
func BenchmarkMapaccessNestedArray(b *testing.B) { benchmarkMapaccess("nested.array[0]", b) }
func BenchmarkGoTemplateRootKey(b *testing.B) { benchmarkGoTemplate("{{ .Nested }}", b) }
func BenchmarkGoTemplateNestedKey(b *testing.B) { benchmarkGoTemplate("{{ .Nested.Array }}", b) }
func BenchmarkGoTemplateNestedArray(b *testing.B) {
benchmarkGoTemplate("{{ index .Nested.Array 0 }}", b)
}