Repository navigation
Expand file tree
/
Copy pathvalues_test.go
More file actions
456 lines (425 loc) Β· 14.8 KB
/
Copy pathvalues_test.go
File metadata and controls
456 lines (425 loc) Β· 14.8 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
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
package schema
import (
"errors"
"math/rand/v2"
"reflect"
"slices"
"strconv"
"strings"
"testing"
"time"
)
// valuesTarget exercises every way a key reaches a field: scalars and their
// pointers, slices built from repeated keys and from commas, nested and
// embedded structs, slices of structs addressed by index, TextUnmarshaler
// and custom-converter types, required keys and defaults.
type valuesTarget struct {
Embedded
Time time.Time `schema:"time"`
Nested valuesNested `schema:"nested"`
PNested *valuesNested `schema:"pnested"`
Custom valuesCustom `schema:"custom"`
PStr *string `schema:"pstr"`
Rude rudeBool `schema:"rude"`
Str string `schema:"str"`
Strs []string `schema:"strs"`
Ints []int `schema:"ints"`
Items []valuesNested `schema:"items"`
Named []IntAlias `schema:"named"`
Float float64 `schema:"float"`
Int int `schema:"int"`
Def int `schema:"def,default:42"`
Req string `schema:"req,required"`
Bool bool `schema:"bool"`
}
type Embedded struct {
Emb string `schema:"emb"`
}
// valuesCustom decodes through a registered converter.
type valuesCustom struct {
S string
}
type valuesNested struct {
A string `schema:"a"`
B []int `schema:"b"`
NReq string `schema:"nreq,required"`
}
// decodeBoth decodes pairs through DecodeValues and through Decode, into
// fresh values of the same type, and returns both results.
func decodeBoth[T any](t *testing.T, d *Decoder, keys, values []string) (viaValues, viaMap T, errValues, errMap error) { //nolint:nonamedreturns,gocritic // the four results read best named
t.Helper()
m := make(map[string][]string, len(keys))
for i, key := range keys {
m[key] = append(m[key], values[i])
}
errValues = d.DecodeValues(&viaValues, keys, values)
errMap = d.Decode(&viaMap, m)
return viaValues, viaMap, errValues, errMap
}
// requireSameErrors requires two decode errors to report the same keys with
// the same messages.
func requireSameErrors(t *testing.T, want, got error, context string) {
t.Helper()
if (want == nil) != (got == nil) {
t.Fatalf("%s: Decode returned %v, DecodeValues %v", context, want, got)
}
if want == nil {
return
}
var wantMulti, gotMulti MultiError
if !errors.As(want, &wantMulti) || !errors.As(got, &gotMulti) {
if want.Error() != got.Error() {
t.Fatalf("%s: Decode returned %q, DecodeValues %q", context, want, got)
}
return
}
if len(wantMulti) != len(gotMulti) {
t.Fatalf("%s: Decode reported %v, DecodeValues %v", context, wantMulti, gotMulti)
}
for key, err := range wantMulti {
if gotErr, ok := gotMulti[key]; !ok || gotErr.Error() != err.Error() {
t.Fatalf("%s: key %q: Decode reported %v, DecodeValues %v", context, key, err, gotMulti[key])
}
}
}
// TestDecodeValuesMatchesDecode is the differential guard for DecodeValues:
// across generated pairs, repeated keys adjacent and apart, empty values,
// unknown and malformed keys, and every decoder option, it must decode what
// Decode decodes from the map the same pairs make, and report the same errors.
func TestDecodeValuesMatchesDecode(t *testing.T) {
t.Parallel()
keyPool := []string{
"str", "Str", "pstr", "strs", "ints", "int", "float", "bool", "rude",
"time", "custom", "named", "emb", "def", "req", "nested.a", "nested.b",
"nested.nreq", "pnested.a", "pnested.nreq", "items.0.a", "items.1.b",
"items.3.nreq", "unknown", "nested.unknown", "items.x.a", "items.1001.a", "",
"nested", "pnested", ".", "a..b",
}
valuePool := []string{
"", "x", "1", "2", "-3", "4.5", "true", "on", "no", "1,2", "a,b,", "nope",
"2006-01-02T15:04:05Z", "99999999999999999999",
}
rng := rand.New(rand.NewPCG(7, 11)) //nolint:gosec // deterministic test input, not security
for _, option := range []func(*Decoder){
func(*Decoder) {},
func(d *Decoder) { d.IgnoreUnknownKeys(true) },
func(d *Decoder) { d.ZeroEmpty(true) },
func(d *Decoder) { d.IgnoreUnknownKeys(true); d.ZeroEmpty(true) },
} {
d := NewDecoder()
option(d)
d.RegisterConverter(valuesCustom{}, func(s string) reflect.Value {
if s == "nope" {
return reflect.Value{}
}
return reflect.ValueOf(valuesCustom{S: s})
})
for round := range 3000 {
n := rng.IntN(12)
keys := make([]string, 0, n)
values := make([]string, 0, n)
for range n {
key := keyPool[rng.IntN(len(keyPool))]
// The keys that differ from "str" only in case name one field
// between them, which Decode resolves in map order: keep one.
if key == "Str" && slices.Contains(keys, "str") || key == "str" && slices.Contains(keys, "Str") {
continue
}
// An empty value under a struct's own key zeroes the whole
// struct with ZeroEmpty, wiping the fields its dotted keys
// set before it or not, by map order: give it a value.
value := func() string {
for {
v := valuePool[rng.IntN(len(valuePool))]
if v != "" || (key != "nested" && key != "pnested") {
return v
}
}
}
keys = append(keys, key)
values = append(values, value())
// Now and then repeat the key, next to it or later on.
if rng.IntN(4) == 0 {
keys = append(keys, key)
values = append(values, value())
}
}
viaValues, viaMap, errValues, errMap := decodeBoth[valuesTarget](t, d, keys, values)
context := "round " + strconv.Itoa(round) + " pairs " + pairsString(keys, values)
requireSameErrors(t, errMap, errValues, context)
if !reflect.DeepEqual(viaMap, viaValues) {
t.Fatalf("%s: Decode decoded %+v, DecodeValues %+v", context, viaMap, viaValues)
}
}
}
}
func pairsString(keys, values []string) string {
var b strings.Builder
for i := range keys {
if i > 0 {
b.WriteByte('&')
}
b.WriteString(keys[i] + "=" + values[i])
}
return b.String()
}
// TestDecodeValuesGroupsKeys pins how pairs of one key become its values:
// in the order given, whether the pairs are adjacent or not, past the pairs
// grouped on the stack, and with the key found again when required keys and
// defaults look it up.
func TestDecodeValuesGroupsKeys(t *testing.T) {
t.Parallel()
type target struct {
A []string `schema:"a"`
B []int `schema:"b"`
C string `schema:"c"`
Req []string `schema:"req,required"`
Def string `schema:"def,default:d"`
}
d := NewDecoder()
var got target
err := d.DecodeValues(&got,
[]string{"a", "b", "a", "a", "c", "b", "c", "req", "req"},
[]string{"1", "2", "3", "4", "first", "5", "last", "", "r"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Without ZeroEmpty an empty value adds no element.
want := target{A: []string{"1", "3", "4"}, B: []int{2, 5}, C: "last", Req: []string{"r"}, Def: "d"}
if !reflect.DeepEqual(want, got) {
t.Fatalf("got %+v, want %+v", got, want)
}
// More pairs than the stack holds, so the grouping is allocated.
var keys, values []string
for i := range maxInlinePairs * 3 {
keys = append(keys, "ab"[i%2:i%2+1])
values = append(values, strconv.Itoa(i))
}
keys = append(keys, "req")
values = append(values, "r")
got = target{}
if err = d.DecodeValues(&got, keys, values); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(got.A) != maxInlinePairs*3/2 || got.A[0] != "0" || got.A[len(got.A)-1] != strconv.Itoa(maxInlinePairs*3-2) {
t.Fatalf("grouped values out of order: %v", got.A)
}
if len(got.B) != maxInlinePairs*3/2 || got.B[0] != 1 {
t.Fatalf("grouped values out of order: %v", got.B)
}
// A required key given only empty values is still missing, and a key
// given no pairs at all takes its default.
got = target{}
err = d.DecodeValues(&got, []string{"c"}, []string{"x"})
var multi MultiError
if !errors.As(err, &multi) || multi["req"] == nil {
t.Fatalf("expected the missing required key to be reported, got %v", err)
}
if got.Def != "d" {
t.Fatalf("expected the default, got %q", got.Def)
}
}
// A nested field's default applies only while no key under its struct's
// prefix provides the field, which the check looks up with the prefix and
// the field's name assembled in a buffer: among a few pairs by comparing
// keys, among more through the hash table. A key given with an empty value
// leaves its field zero but still counts as provided, as it does for Decode.
func TestDecodeValuesNestedDefaults(t *testing.T) {
t.Parallel()
type inner struct {
A string `schema:"a,default:da"`
B string `schema:"b,default:db"`
}
type target struct {
In inner `schema:"in"`
}
d := NewDecoder()
d.IgnoreUnknownKeys(true)
for _, filler := range []int{0, 2 * maxScanPairs} {
keys := []string{"in.a"}
values := []string{""}
for i := range filler {
keys = append(keys, "filler"+strconv.Itoa(i))
values = append(values, "f")
}
viaValues, viaMap, errValues, errMap := decodeBoth[target](t, d, keys, values)
if errValues != nil || errMap != nil {
t.Fatalf("%d pairs: unexpected errors %v, %v", len(keys), errValues, errMap)
}
want := target{In: inner{A: "", B: "db"}}
if viaValues != want || viaMap != want {
t.Fatalf("%d pairs: DecodeValues decoded %+v, Decode %+v, want %+v", len(keys), viaValues, viaMap, want)
}
}
}
// caseVariants returns every spelling of word, which must be lowercase
// letters, in upper and lower case: 1<<len(word) keys naming one field.
func caseVariants(word string) []string {
keys := make([]string, 1<<len(word))
for i := range keys {
b := []byte(word)
for j := range b {
if i>>j&1 == 1 {
b[j] -= 'a' - 'A'
}
}
keys[i] = string(b)
}
return keys
}
// The pairs' keys are the request's, so the table grouping them must not
// let chosen keys pile onto one slot. Keys that differ only in case group
// apart, and a case-folding hash sent them all to the same slot: each
// insert then probed past every key before it, quadratic in their number.
// Built from every case variant of one key, the table keeps every key close
// to its home slot.
func TestPairIndexSpreadsCaseVariants(t *testing.T) {
t.Parallel()
keys := caseVariants("abcdefghij")
n := len(keys)
values := make([]string, n)
p := pairIndex{
keys: keys, values: values,
table: make([]int32, indexSize(n)), next: make([]int32, n), last: make([]int32, n),
}
p.index()
longest := 0
for i, key := range keys {
if !p.first(i) || p.find(key) != i || p.findBytes([]byte(key)) != i {
t.Fatalf("%q is not a key of its own", key)
}
// How far the key sits past the slot its hash names.
for j, d := pairHash(key)&p.mask, 0; ; j, d = (j+1)&p.mask, d+1 {
if int(p.table[j]) == i+1 {
longest = max(longest, d)
break
}
}
}
// A seeded hash at half load keeps the longest probe near log n; the
// case-folding hash put the last variant n-1 slots out.
if longest > 64 {
t.Fatalf("a key sits %d slots past its home slot of %d; the keys pile up", longest, len(p.table))
}
}
// Every case variant of a key names the same field. Each is a key of its
// own, and DecodeValues decodes keys in the order they first appear, so the
// last variant sets the field.
func TestDecodeValuesCaseVariants(t *testing.T) {
t.Parallel()
type target struct {
F string `schema:"abcdefgh"`
}
keys := caseVariants("abcdefgh")
values := make([]string, len(keys))
for i := range values {
values[i] = strconv.Itoa(i)
}
var got target
if err := NewDecoder().DecodeValues(&got, keys, values); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if want := values[len(values)-1]; got.F != want {
t.Fatalf("got %q, want the last variant's value %q", got.F, want)
}
}
func TestDecodeValuesRejectsMismatchedInput(t *testing.T) {
t.Parallel()
d := NewDecoder()
var s struct {
A string `schema:"a"`
}
if err := d.DecodeValues(&s, []string{"a"}, nil); !errors.Is(err, errValuesLength) {
t.Fatalf("expected errValuesLength, got %v", err)
}
if err := d.DecodeValues(s, []string{"a"}, []string{"x"}); !errors.Is(err, errNotPointerToStruct) {
t.Fatalf("expected errNotPointerToStruct, got %v", err)
}
if err := d.DecodeValues(&s, nil, nil); err != nil {
t.Fatalf("expected no error for no pairs, got %v", err)
}
}
// TestDecodeValuesDoesNotAllocate pins that grouping the pairs costs no
// allocation: a struct of scalars decodes without any.
func TestDecodeValuesDoesNotAllocate(t *testing.T) { //nolint:paralleltest // testing.AllocsPerRun panics in a parallel test
type target struct {
A string `schema:"a"`
B int `schema:"b"`
C bool `schema:"c"`
D float64 `schema:"d"`
}
d := NewDecoder()
keys := []string{"a", "b", "c", "d", "b", "unknown"}
values := []string{"x", "1", "true", "2.5", "2", "y"}
d.IgnoreUnknownKeys(true)
var s target
if err := d.DecodeValues(&s, keys, values); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if allocs := testing.AllocsPerRun(100, func() {
_ = d.DecodeValues(&s, keys, values) //nolint:errcheck // checked above
}); allocs != 0 {
t.Fatalf("DecodeValues allocated %v times per run", allocs)
}
}
func BenchmarkDecodeValues(b *testing.B) {
type target struct {
Name string `schema:"name"`
Hobby []string `schema:"hobby"`
ID int `schema:"id"`
}
keys := []string{"id", "name", "hobby", "hobby"}
values := []string{"1", "tom", "basketball", "football"}
m := map[string][]string{"id": {"1"}, "name": {"tom"}, "hobby": {"basketball", "football"}}
d := NewDecoder()
var s target
b.Run("values", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = d.DecodeValues(&s, keys, values) //nolint:errcheck // benchmark
}
})
b.Run("map", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
_ = d.Decode(&s, m) //nolint:errcheck // benchmark
}
})
}
// TestDecodeStackStateDoesNotAllocate pins that what a decode keeps from key
// to key, the required-key bitset and the slice growth tracker, stays on the
// stack for both Decode and DecodeValues: a struct with required keys and an
// indexed slice that needs no growing decodes without allocating.
func TestDecodeStackStateDoesNotAllocate(t *testing.T) { //nolint:paralleltest // testing.AllocsPerRun panics in a parallel test
type item struct {
A string `schema:"a"`
}
type target struct {
Req string `schema:"req,required"`
Items []item `schema:"items"`
}
d := NewDecoder()
keys := []string{"req", "items.0.a", "items.1.a"}
values := []string{"x", "y", "z"}
m := map[string][]string{"req": {"x"}, "items.0.a": {"y"}, "items.1.a": {"z"}}
s := target{Items: make([]item, 2)}
if err := d.DecodeValues(&s, keys, values); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if err := d.Decode(&s, m); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if s.Req != "x" || s.Items[0].A != "y" || s.Items[1].A != "z" {
t.Fatalf("unexpected result %+v", s)
}
if allocs := testing.AllocsPerRun(100, func() {
_ = d.DecodeValues(&s, keys, values) //nolint:errcheck // checked above
}); allocs != 0 {
t.Fatalf("DecodeValues allocated %v times per run", allocs)
}
if allocs := testing.AllocsPerRun(100, func() {
_ = d.Decode(&s, m) //nolint:errcheck // checked above
}); allocs != 0 {
t.Fatalf("Decode allocated %v times per run", allocs)
}
}