-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstorage_test.go
More file actions
298 lines (281 loc) · 6.87 KB
/
storage_test.go
File metadata and controls
298 lines (281 loc) · 6.87 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
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
package fmap
import (
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestFmap(t *testing.T) {
check := func(fields Storage, path string) bool {
_, ok := fields.Find(path)
return ok
}
test := func(t *testing.T, fields Storage) {
assert.Equal(t, check(fields, "String"), true)
assert.Equal(t, check(fields, "Int"), true)
assert.Equal(t, check(fields, "Int8"), true)
assert.Equal(t, check(fields, "Int16"), true)
assert.Equal(t, check(fields, "Int32"), true)
assert.Equal(t, check(fields, "Int64"), true)
assert.Equal(t, check(fields, "Uint"), true)
assert.Equal(t, check(fields, "Uint8"), true)
assert.Equal(t, check(fields, "Uint16"), true)
assert.Equal(t, check(fields, "Uint32"), true)
assert.Equal(t, check(fields, "Uint64"), true)
assert.Equal(t, check(fields, "Float32"), true)
assert.Equal(t, check(fields, "Float64"), true)
assert.Equal(t, check(fields, "Bool"), true)
assert.Equal(t, check(fields, "PtrString"), true)
assert.Equal(t, check(fields, "PtrInt"), true)
assert.Equal(t, check(fields, "PtrInt8"), true)
assert.Equal(t, check(fields, "PtrInt16"), true)
assert.Equal(t, check(fields, "PtrInt32"), true)
assert.Equal(t, check(fields, "PtrInt64"), true)
assert.Equal(t, check(fields, "PtrUint"), true)
assert.Equal(t, check(fields, "PtrUint8"), true)
assert.Equal(t, check(fields, "PtrUint16"), true)
assert.Equal(t, check(fields, "PtrUint32"), true)
assert.Equal(t, check(fields, "PtrUint64"), true)
assert.Equal(t, check(fields, "PtrFloat32"), true)
assert.Equal(t, check(fields, "PtrFloat64"), true)
assert.Equal(t, check(fields, "PtrBool"), true)
assert.Equal(t, check(fields, "NestedStruct"), true)
assert.Equal(t, check(fields, "NestedStruct.String"), true)
assert.Equal(t, check(fields, "NestedStruct.PtrString"), true)
assert.Equal(t, check(fields, "Slice"), true)
assert.Equal(t, check(fields, "PtrSlice"), true)
assert.Equal(t, check(fields, "Time"), true)
assert.Equal(t, check(fields, "PtrTime"), true)
}
t.Run("GetFrom", func(t *testing.T) {
fields, _ := GetFrom(&TestStruct{})
test(t, fields)
})
t.Run("Get_Struct", func(t *testing.T) {
fields, _ := Get[TestStruct]()
test(t, fields)
})
t.Run("Get_PtrStruct", func(t *testing.T) {
fields, _ := Get[*TestStruct]()
test(t, fields)
})
t.Run("Get_NotAStruct", func(t *testing.T) {
fields, err := Get[[]string]()
assert.Error(t, err)
assert.Nil(t, fields)
})
}
func BenchmarkGetFrom(b *testing.B) {
tt := TestStruct{}
for i := 0; i < b.N; i++ {
fields, _ := GetFrom(&tt)
_ = fields
}
}
func BenchmarkFieldGet(b *testing.B) {
tt := TestStruct{}
fields, _ := GetFrom(&tt)
for i := 0; i < b.N; i++ {
val := fields.MustFind("Int").Get(&tt)
_ = val
}
}
func BenchmarkFieldGetPtr(b *testing.B) {
tt := TestStruct{}
fields, _ := GetFrom(&tt)
for i := 0; i < b.N; i++ {
val := fields.MustFind("Int").GetPtr(&tt)
_ = val
}
}
func BenchmarkFieldSet(b *testing.B) {
tt := TestStruct{}
fields, _ := GetFrom(&tt)
for i := 0; i < b.N; i++ {
fields.MustFind("Time").Set(&tt, time.Now())
}
}
func BenchmarkRawFieldGet(b *testing.B) {
tt := TestStruct{}
for i := 0; i < b.N; i++ {
str := tt.String
_ = str
}
}
func Test_storage_GetFieldByPtr(t *testing.T) {
type TestStruct struct {
String string
StringPtr *string
Age uint
NestedStruct struct {
Field string
Slice []string
SlicePtr []*string
MapSlice map[string][]string
}
Slice []string
SlicePtr []*string
MapSlice map[string][]string
}
a, b, c := "a", "b", "c"
test := TestStruct{
String: "meow",
StringPtr: &a,
Age: 17,
NestedStruct: struct {
Field string
Slice []string
SlicePtr []*string
MapSlice map[string][]string
}{
Field: "f",
Slice: []string{"a", "b", "c"},
SlicePtr: []*string{&a, &b, &c},
MapSlice: map[string][]string{
"a": []string{"apricot", "avocado", "apple"},
"b": []string{"banana", "blackberry", "blueberry"},
"c": []string{"coconut", "cherry", "cashew"},
},
},
Slice: []string{"a", "b", "c"},
SlicePtr: []*string{&a, &b, &c},
MapSlice: map[string][]string{
"a": []string{"apricot", "avocado", "apple"},
"b": []string{"banana", "blackberry", "blueberry"},
"c": []string{"coconut", "cherry", "cashew"},
},
}
s, _ := GetFrom(test)
sideValue := 555
type args struct {
structPtr any
fieldPtr any
}
tests := []struct {
name string
getStorage func() Storage
args args
want Field
wantErr bool
}{
{
name: "exist string ptr field",
args: args{
structPtr: &test,
fieldPtr: &test.StringPtr,
},
want: s.MustFind("StringPtr"),
wantErr: false,
},
{
name: "exist uint field",
args: args{
structPtr: &test,
fieldPtr: &test.Age,
},
want: s.MustFind("Age"),
wantErr: false,
},
{
name: "non-exist field",
args: args{
structPtr: &test,
fieldPtr: &sideValue,
},
want: nil,
wantErr: true,
},
{
name: "non-exist field",
args: args{
structPtr: &test,
fieldPtr: &sideValue,
},
want: nil,
wantErr: true,
},
{
name: "exist slice field",
args: args{
structPtr: &test,
fieldPtr: &test.Slice,
},
want: s.MustFind("Slice"),
wantErr: false,
},
{
name: "exist slice ptr field",
args: args{
structPtr: &test,
fieldPtr: &test.SlicePtr,
},
want: s.MustFind("SlicePtr"),
wantErr: false,
},
{
name: "exist map slice field",
args: args{
structPtr: &test,
fieldPtr: &test.MapSlice,
},
want: s.MustFind("MapSlice"),
wantErr: false,
},
{
name: "exist struct",
args: args{
structPtr: &test,
fieldPtr: &test.NestedStruct,
},
want: s.MustFind("NestedStruct"),
wantErr: false,
},
{
name: "exist struct field",
args: args{
structPtr: &test,
fieldPtr: &test.NestedStruct.Field,
},
want: s.MustFind("NestedStruct.Field"),
wantErr: false,
},
{
name: "exist struct slice",
args: args{
structPtr: &test,
fieldPtr: &test.NestedStruct.Slice,
},
want: s.MustFind("NestedStruct.Slice"),
wantErr: false,
},
{
name: "exist struct slice ptr",
args: args{
structPtr: &test,
fieldPtr: &test.NestedStruct.SlicePtr,
},
want: s.MustFind("NestedStruct.SlicePtr"),
wantErr: false,
},
{
name: "exist struct map slice",
args: args{
structPtr: &test,
fieldPtr: &test.NestedStruct.MapSlice,
},
want: s.MustFind("NestedStruct.MapSlice"),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := s.GetFieldByPtr(tt.args.structPtr, tt.args.fieldPtr)
if (err != nil) != tt.wantErr {
t.Errorf("GetFieldByPtr() error = %v, wantErr %v", err, tt.wantErr)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetFieldByPtr() got = %v, want %v", got, tt.want)
}
})
}
}