Debian Codegraph @

tpl/collections/where_test.go

raw ◀ ▶
1 Debian patch touch this file
1 // Copyright 2017 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package collections
15
16 import (
17 "context"
18 "fmt"
19 "html/template"
20 "math"
21 "math/rand"
22 "reflect"
23 "strings"
24 "testing"
25 "time"
26
27 "github.com/gohugoio/hugo/common/hmaps"
28 )
29
30 func TestWhere(t *testing.T) {
31 t.Parallel()
32
33 ns := newNs()
34
35 type Mid struct {
36 Tst TstX
37 }
38
39 d1 := time.Now()
40 d2 := d1.Add(1 * time.Hour)
41 d3 := d2.Add(1 * time.Hour)
42 d4 := d3.Add(1 * time.Hour)
43 d5 := d4.Add(1 * time.Hour)
44 d6 := d5.Add(1 * time.Hour)
45
46 type testt struct {
47 seq any
48 key any
49 op string
50 match any
51 expect any
52 }
53
54 createTestVariants := func(test testt) []testt {
55 testVariants := []testt{test}
56 if islice := ToTstXIs(test.seq); islice != nil {
57 variant := test
58 variant.seq = islice
59 expect := ToTstXIs(test.expect)
60 if expect != nil {
61 variant.expect = expect
62 }
63 testVariants = append(testVariants, variant)
64 }
65
66 return testVariants
67 }
68
69 for i, test := range []testt{
70 {
71 seq: []map[int]string{
72 {1: "a", 2: "m"}, {1: "c", 2: "d"}, {1: "e", 3: "m"},
73 },
74 key: 2, match: "m",
75 expect: []map[int]string{
76 {1: "a", 2: "m"},
77 },
78 },
79 {
80 seq: []map[string]int{
81 {"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "x": 4},
82 },
83 key: "b", match: 4,
84 expect: []map[string]int{
85 {"a": 3, "b": 4},
86 },
87 },
88 {
89 seq: []map[string]float64{
90 {"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "x": 4},
91 },
92 key: "b", match: 4.0,
93 expect: []map[string]float64{{"a": 3, "b": 4}},
94 },
95 {
96 seq: []map[string]float64{
97 {"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "x": 4},
98 },
99 key: "b", match: 4.0, op: "!=",
100 expect: []map[string]float64{{"a": 1, "b": 2}, {"a": 5, "x": 4}},
101 },
102 {
103 seq: []map[string]float64{
104 {"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "x": 4},
105 },
106 key: "b", match: 4.0, op: "<",
107 expect: []map[string]float64{{"a": 1, "b": 2}},
108 },
109 {
110 seq: []map[string]float64{
111 {"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "x": 4},
112 },
113 key: "b", match: 4, op: "<",
114 expect: []map[string]float64{{"a": 1, "b": 2}},
115 },
116 {
117 seq: []map[string]int{
118 {"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "x": 4},
119 },
120 key: "b", match: 4.0, op: "<",
121 expect: []map[string]int{{"a": 1, "b": 2}},
122 },
123 {
124 seq: []map[string]int{
125 {"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "x": 4},
126 },
127 key: "b", match: 4.2, op: "<",
128 expect: []map[string]int{{"a": 1, "b": 2}, {"a": 3, "b": 4}},
129 },
130 {
131 seq: []map[string]float64{
132 {"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "x": 4},
133 },
134 key: "b", match: 4.0, op: "<=",
135 expect: []map[string]float64{{"a": 1, "b": 2}, {"a": 3, "b": 4}},
136 },
137 {
138 seq: []map[string]float64{
139 {"a": 1, "b": 2}, {"a": 3, "b": 3}, {"a": 5, "x": 4},
140 },
141 key: "b", match: 2.0, op: ">",
142 expect: []map[string]float64{{"a": 3, "b": 3}},
143 },
144 {
145 seq: []map[string]float64{
146 {"a": 1, "b": 2}, {"a": 3, "b": 3}, {"a": 5, "x": 4},
147 },
148 key: "b", match: 2.0, op: ">=",
149 expect: []map[string]float64{{"a": 1, "b": 2}, {"a": 3, "b": 3}},
150 },
151 // Issue #8353
152 // String type mismatch.
153 {
154 seq: []map[string]any{
155 {"a": "1", "b": "2"}, {"a": "3", "b": template.HTML("4")}, {"a": "5", "x": "4"},
156 },
157 key: "b", match: "4",
158 expect: []map[string]any{
159 {"a": "3", "b": template.HTML("4")},
160 },
161 },
162 {
163 seq: []TstX{
164 {A: "a", B: "b"}, {A: "c", B: "d"}, {A: "e", B: "f"},
165 },
166 key: "B", match: "f",
167 expect: []TstX{
168 {A: "e", B: "f"},
169 },
170 },
171 {
172 seq: []*map[int]string{
173 {1: "a", 2: "m"}, {1: "c", 2: "d"}, {1: "e", 3: "m"},
174 },
175 key: 2, match: "m",
176 expect: []*map[int]string{
177 {1: "a", 2: "m"},
178 },
179 },
180 // Case insensitive hmaps.Params
181 // Slice of structs
182 {
183 seq: []TstParams{{params: hmaps.Params{"i": 0, "color": "indigo"}}, {params: hmaps.Params{"i": 1, "color": "blue"}}, {params: hmaps.Params{"i": 2, "color": "green"}}, {params: hmaps.Params{"i": 3, "color": "blue"}}},
184 key: ".Params.COLOR", match: "blue",
185 expect: []TstParams{{params: hmaps.Params{"i": 1, "color": "blue"}}, {params: hmaps.Params{"i": 3, "color": "blue"}}},
186 },
187 {
188 seq: []TstParams{{params: hmaps.Params{"nested": map[string]any{"color": "indigo"}}}, {params: hmaps.Params{"nested": map[string]any{"color": "blue"}}}},
189 key: ".Params.NEsTED.COLOR", match: "blue",
190 expect: []TstParams{{params: hmaps.Params{"nested": map[string]any{"color": "blue"}}}},
191 },
192 {
193 seq: []TstParams{{params: hmaps.Params{"i": 0, "color": "indigo"}}, {params: hmaps.Params{"i": 1, "color": "blue"}}, {params: hmaps.Params{"i": 2, "color": "green"}}, {params: hmaps.Params{"i": 3, "color": "blue"}}},
194 key: ".Params", match: "blue",
195 expect: []TstParams{},
196 },
197 // Slice of maps
198 {
199 seq: []hmaps.Params{
200 {"a": "a1", "b": "b1"}, {"a": "a2", "b": "b2"},
201 },
202 key: "B", match: "b2",
203 expect: []hmaps.Params{
204 {"a": "a2", "b": "b2"},
205 },
206 },
207 {
208 seq: []hmaps.Params{
209 {
210 "a": map[string]any{
211 "b": "b1",
212 },
213 },
214 {
215 "a": map[string]any{
216 "b": "b2",
217 },
218 },
219 },
220 key: "A.B", match: "b2",
221 expect: []hmaps.Params{
222 {
223 "a": map[string]any{
224 "b": "b2",
225 },
226 },
227 },
228 },
229 {
230 seq: []*TstX{
231 {A: "a", B: "b"}, {A: "c", B: "d"}, {A: "e", B: "f"},
232 },
233 key: "B", match: "f",
234 expect: []*TstX{
235 {A: "e", B: "f"},
236 },
237 },
238 {
239 seq: []*TstX{
240 {A: "a", B: "b"}, {A: "c", B: "d"}, {A: "e", B: "c"},
241 },
242 key: "TstRp", match: "rc",
243 expect: []*TstX{
244 {A: "c", B: "d"},
245 },
246 },
247 {
248 seq: []TstX{
249 {A: "a", B: "b"}, {A: "c", B: "d"}, {A: "e", B: "c"},
250 },
251 key: "TstRv", match: "rc",
252 expect: []TstX{
253 {A: "e", B: "c"},
254 },
255 },
256 {
257 seq: []map[string]TstX{
258 {"foo": TstX{A: "a", B: "b"}}, {"foo": TstX{A: "c", B: "d"}}, {"foo": TstX{A: "e", B: "f"}},
259 },
260 key: "foo.B", match: "d",
261 expect: []map[string]TstX{
262 {"foo": TstX{A: "c", B: "d"}},
263 },
264 },
265 {
266 seq: []map[string]TstX{
267 {"baz": TstX{A: "a", B: "b"}}, {"foo": TstX{A: "a", B: "b"}}, {"foo": TstX{A: "c", B: "d"}}, {"foo": TstX{A: "e", B: "f"}},
268 },
269 key: "foo.B", match: "d",
270 expect: []map[string]TstX{
271 {"foo": TstX{A: "c", B: "d"}},
272 },
273 },
274 {
275 seq: []map[string]TstX{
276 {"foo": TstX{A: "a", B: "b"}}, {"foo": TstX{A: "c", B: "d"}}, {"foo": TstX{A: "e", B: "f"}},
277 },
278 key: ".foo.B", match: "d",
279 expect: []map[string]TstX{
280 {"foo": TstX{A: "c", B: "d"}},
281 },
282 },
283 {
284 seq: []map[string]TstX{
285 {"foo": TstX{A: "a", B: "b"}}, {"foo": TstX{A: "c", B: "d"}}, {"foo": TstX{A: "e", B: "f"}},
286 },
287 key: "foo.TstRv", match: "rd",
288 expect: []map[string]TstX{
289 {"foo": TstX{A: "c", B: "d"}},
290 },
291 },
292 {
293 seq: []map[string]*TstX{
294 {"foo": &TstX{A: "a", B: "b"}}, {"foo": &TstX{A: "c", B: "d"}}, {"foo": &TstX{A: "e", B: "f"}},
295 },
296 key: "foo.TstRp", match: "rc",
297 expect: []map[string]*TstX{
298 {"foo": &TstX{A: "c", B: "d"}},
299 },
300 },
301 {
302 seq: []TstXIHolder{
303 {&TstX{A: "a", B: "b"}}, {&TstX{A: "c", B: "d"}}, {&TstX{A: "e", B: "f"}},
304 },
305 key: "XI.TstRp", match: "rc",
306 expect: []TstXIHolder{
307 {&TstX{A: "c", B: "d"}},
308 },
309 },
310 {
311 seq: []TstXIHolder{
312 {&TstX{A: "a", B: "b"}}, {&TstX{A: "c", B: "d"}}, {&TstX{A: "e", B: "f"}},
313 },
314 key: "XI.A", match: "e",
315 expect: []TstXIHolder{
316 {&TstX{A: "e", B: "f"}},
317 },
318 },
319 {
320 seq: []map[string]Mid{
321 {"foo": Mid{Tst: TstX{A: "a", B: "b"}}}, {"foo": Mid{Tst: TstX{A: "c", B: "d"}}}, {"foo": Mid{Tst: TstX{A: "e", B: "f"}}},
322 },
323 key: "foo.Tst.B", match: "d",
324 expect: []map[string]Mid{
325 {"foo": Mid{Tst: TstX{A: "c", B: "d"}}},
326 },
327 },
328 {
329 seq: []map[string]Mid{
330 {"foo": Mid{Tst: TstX{A: "a", B: "b"}}}, {"foo": Mid{Tst: TstX{A: "c", B: "d"}}}, {"foo": Mid{Tst: TstX{A: "e", B: "f"}}},
331 },
332 key: "foo.Tst.TstRv", match: "rd",
333 expect: []map[string]Mid{
334 {"foo": Mid{Tst: TstX{A: "c", B: "d"}}},
335 },
336 },
337 {
338 seq: []map[string]*Mid{
339 {"foo": &Mid{Tst: TstX{A: "a", B: "b"}}}, {"foo": &Mid{Tst: TstX{A: "c", B: "d"}}}, {"foo": &Mid{Tst: TstX{A: "e", B: "f"}}},
340 },
341 key: "foo.Tst.TstRp", match: "rc",
342 expect: []map[string]*Mid{
343 {"foo": &Mid{Tst: TstX{A: "c", B: "d"}}},
344 },
345 },
346 {
347 seq: []map[string]int{
348 {"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "b": 6},
349 },
350 key: "b", op: ">", match: 3,
351 expect: []map[string]int{
352 {"a": 3, "b": 4}, {"a": 5, "b": 6},
353 },
354 },
355 {
356 seq: []map[string]float64{
357 {"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "b": 6},
358 },
359 key: "b", op: ">", match: 3.0,
360 expect: []map[string]float64{
361 {"a": 3, "b": 4}, {"a": 5, "b": 6},
362 },
363 },
364 {
365 seq: []TstX{
366 {A: "a", B: "b"}, {A: "c", B: "d"}, {A: "e", B: "f"},
367 },
368 key: "B", op: "!=", match: "f",
369 expect: []TstX{
370 {A: "a", B: "b"}, {A: "c", B: "d"},
371 },
372 },
373 {
374 seq: []map[string]int{
375 {"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "b": 6},
376 },
377 key: "b", op: "in", match: []int{3, 4, 5},
378 expect: []map[string]int{
379 {"a": 3, "b": 4},
380 },
381 },
382 {
383 seq: []map[string]float64{
384 {"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "b": 6},
385 },
386 key: "b", op: "in", match: []float64{3, 4, 5},
387 expect: []map[string]float64{
388 {"a": 3, "b": 4},
389 },
390 },
391 {
392 seq: []map[string][]string{
393 {"a": []string{"A", "B", "C"}, "b": []string{"D", "E", "F"}}, {"a": []string{"G", "H", "I"}, "b": []string{"J", "K", "L"}}, {"a": []string{"M", "N", "O"}, "b": []string{"P", "Q", "R"}},
394 },
395 key: "b", op: "intersect", match: []string{"D", "P", "Q"},
396 expect: []map[string][]string{
397 {"a": []string{"A", "B", "C"}, "b": []string{"D", "E", "F"}}, {"a": []string{"M", "N", "O"}, "b": []string{"P", "Q", "R"}},
398 },
399 },
400 {
401 seq: []map[string][]int{
402 {"a": []int{1, 2, 3}, "b": []int{4, 5, 6}}, {"a": []int{7, 8, 9}, "b": []int{10, 11, 12}}, {"a": []int{13, 14, 15}, "b": []int{16, 17, 18}},
403 },
404 key: "b", op: "intersect", match: []int{4, 10, 12},
405 expect: []map[string][]int{
406 {"a": []int{1, 2, 3}, "b": []int{4, 5, 6}}, {"a": []int{7, 8, 9}, "b": []int{10, 11, 12}},
407 },
408 },
409 {
410 seq: []map[string][]int8{
411 {"a": []int8{1, 2, 3}, "b": []int8{4, 5, 6}}, {"a": []int8{7, 8, 9}, "b": []int8{10, 11, 12}}, {"a": []int8{13, 14, 15}, "b": []int8{16, 17, 18}},
412 },
413 key: "b", op: "intersect", match: []int8{4, 10, 12},
414 expect: []map[string][]int8{
415 {"a": []int8{1, 2, 3}, "b": []int8{4, 5, 6}}, {"a": []int8{7, 8, 9}, "b": []int8{10, 11, 12}},
416 },
417 },
418 {
419 seq: []map[string][]int16{
420 {"a": []int16{1, 2, 3}, "b": []int16{4, 5, 6}}, {"a": []int16{7, 8, 9}, "b": []int16{10, 11, 12}}, {"a": []int16{13, 14, 15}, "b": []int16{16, 17, 18}},
421 },
422 key: "b", op: "intersect", match: []int16{4, 10, 12},
423 expect: []map[string][]int16{
424 {"a": []int16{1, 2, 3}, "b": []int16{4, 5, 6}}, {"a": []int16{7, 8, 9}, "b": []int16{10, 11, 12}},
425 },
426 },
427 {
428 seq: []map[string][]int32{
429 {"a": []int32{1, 2, 3}, "b": []int32{4, 5, 6}}, {"a": []int32{7, 8, 9}, "b": []int32{10, 11, 12}}, {"a": []int32{13, 14, 15}, "b": []int32{16, 17, 18}},
430 },
431 key: "b", op: "intersect", match: []int32{4, 10, 12},
432 expect: []map[string][]int32{
433 {"a": []int32{1, 2, 3}, "b": []int32{4, 5, 6}}, {"a": []int32{7, 8, 9}, "b": []int32{10, 11, 12}},
434 },
435 },
436 {
437 seq: []map[string][]int64{
438 {"a": []int64{1, 2, 3}, "b": []int64{4, 5, 6}}, {"a": []int64{7, 8, 9}, "b": []int64{10, 11, 12}}, {"a": []int64{13, 14, 15}, "b": []int64{16, 17, 18}},
439 },
440 key: "b", op: "intersect", match: []int64{4, 10, 12},
441 expect: []map[string][]int64{
442 {"a": []int64{1, 2, 3}, "b": []int64{4, 5, 6}}, {"a": []int64{7, 8, 9}, "b": []int64{10, 11, 12}},
443 },
444 },
445 {
446 seq: []map[string][]float32{
447 {"a": []float32{1.0, 2.0, 3.0}, "b": []float32{4.0, 5.0, 6.0}}, {"a": []float32{7.0, 8.0, 9.0}, "b": []float32{10.0, 11.0, 12.0}}, {"a": []float32{13.0, 14.0, 15.0}, "b": []float32{16.0, 17.0, 18.0}},
448 },
449 key: "b", op: "intersect", match: []float32{4, 10, 12},
450 expect: []map[string][]float32{
451 {"a": []float32{1.0, 2.0, 3.0}, "b": []float32{4.0, 5.0, 6.0}}, {"a": []float32{7.0, 8.0, 9.0}, "b": []float32{10.0, 11.0, 12.0}},
452 },
453 },
454 {
455 seq: []map[string][]float64{
456 {"a": []float64{1.0, 2.0, 3.0}, "b": []float64{4.0, 5.0, 6.0}}, {"a": []float64{7.0, 8.0, 9.0}, "b": []float64{10.0, 11.0, 12.0}}, {"a": []float64{13.0, 14.0, 15.0}, "b": []float64{16.0, 17.0, 18.0}},
457 },
458 key: "b", op: "intersect", match: []float64{4, 10, 12},
459 expect: []map[string][]float64{
460 {"a": []float64{1.0, 2.0, 3.0}, "b": []float64{4.0, 5.0, 6.0}}, {"a": []float64{7.0, 8.0, 9.0}, "b": []float64{10.0, 11.0, 12.0}},
461 },
462 },
463 {
464 seq: []map[string]int{
465 {"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "b": 6},
466 },
467 key: "b", op: "in", match: ns.Slice(3, 4, 5),
468 expect: []map[string]int{
469 {"a": 3, "b": 4},
470 },
471 },
472 {
473 seq: []map[string]float64{
474 {"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "b": 6},
475 },
476 key: "b", op: "in", match: ns.Slice(3.0, 4.0, 5.0),
477 expect: []map[string]float64{
478 {"a": 3, "b": 4},
479 },
480 },
481 {
482 seq: []map[string]time.Time{
483 {"a": d1, "b": d2}, {"a": d3, "b": d4}, {"a": d5, "b": d6},
484 },
485 key: "b", op: "in", match: ns.Slice(d3, d4, d5),
486 expect: []map[string]time.Time{
487 {"a": d3, "b": d4},
488 },
489 },
490 {
491 seq: []TstX{
492 {A: "a", B: "b"}, {A: "c", B: "d"}, {A: "e", B: "f"},
493 },
494 key: "B", op: "not in", match: []string{"c", "d", "e"},
495 expect: []TstX{
496 {A: "a", B: "b"}, {A: "e", B: "f"},
497 },
498 },
499 {
500 seq: []TstX{
501 {A: "a", B: "b"}, {A: "c", B: "d"}, {A: "e", B: "f"},
502 },
503 key: "B", op: "not in", match: ns.Slice("c", t, "d", "e"),
504 expect: []TstX{
505 {A: "a", B: "b"}, {A: "e", B: "f"},
506 },
507 },
508 {
509 seq: []map[string]int{
510 {"a": 1, "b": 2}, {"a": 3}, {"a": 5, "b": 6},
511 },
512 key: "b", op: "", match: nil,
513 expect: []map[string]int{
514 {"a": 3},
515 },
516 },
517 {
518 seq: []map[string]int{
519 {"a": 1, "b": 2}, {"a": 3}, {"a": 5, "b": 6},
520 },
521 key: "b", op: "!=", match: nil,
522 expect: []map[string]int{
523 {"a": 1, "b": 2}, {"a": 5, "b": 6},
524 },
525 },
526 {
527 seq: []map[string]int{
528 {"a": 1, "b": 2}, {"a": 3}, {"a": 5, "b": 6},
529 },
530 key: "b", op: ">", match: nil,
531 expect: []map[string]int{},
532 },
533 {
534 seq: []map[string]float64{
535 {"a": 1, "b": 2}, {"a": 3}, {"a": 5, "b": 6},
536 },
537 key: "b", op: "", match: nil,
538 expect: []map[string]float64{
539 {"a": 3},
540 },
541 },
542 {
543 seq: []map[string]float64{
544 {"a": 1, "b": 2}, {"a": 3}, {"a": 5, "b": 6},
545 },
546 key: "b", op: "!=", match: nil,
547 expect: []map[string]float64{
548 {"a": 1, "b": 2}, {"a": 5, "b": 6},
549 },
550 },
551 {
552 seq: []map[string]float64{
553 {"a": 1, "b": 2}, {"a": 3}, {"a": 5, "b": 6},
554 },
555 key: "b", op: ">", match: nil,
556 expect: []map[string]float64{},
557 },
558 {
559 seq: []map[string]bool{
560 {"a": true, "b": false}, {"c": true, "b": true}, {"d": true, "b": false},
561 },
562 key: "b", op: "", match: true,
563 expect: []map[string]bool{
564 {"c": true, "b": true},
565 },
566 },
567 {
568 seq: []map[string]bool{
569 {"a": true, "b": false}, {"c": true, "b": true}, {"d": true, "b": false},
570 },
571 key: "b", op: "!=", match: true,
572 expect: []map[string]bool{
573 {"a": true, "b": false}, {"d": true, "b": false},
574 },
575 },
576 {
577 seq: []map[string]bool{
578 {"a": true, "b": false}, {"c": true, "b": true}, {"d": true, "b": false},
579 },
580 key: "b", op: ">", match: false,
581 expect: []map[string]bool{},
582 },
583 {
584 seq: []map[string]bool{
585 {"a": true, "b": false}, {"c": true, "b": true}, {"d": true, "b": false},
586 },
587 key: "b.z", match: false,
588 expect: []map[string]bool{},
589 },
590 {seq: (*[]TstX)(nil), key: "A", match: "a", expect: false},
591 {seq: TstX{A: "a", B: "b"}, key: "A", match: "a", expect: false},
592 {seq: []map[string]*TstX{{"foo": nil}}, key: "foo.B", match: "d", expect: []map[string]*TstX{}},
593 {seq: []map[string]*TstX{{"foo": nil}}, key: "foo.B.Z", match: "d", expect: []map[string]*TstX{}},
594 {
595 seq: []TstX{
596 {A: "a", B: "b"}, {A: "c", B: "d"}, {A: "e", B: "f"},
597 },
598 key: "B", op: "op", match: "f",
599 expect: false,
600 },
601 {
602 seq: map[string]any{
603 "foo": []any{map[any]any{"a": 1, "b": 2}},
604 "bar": []any{map[any]any{"a": 3, "b": 4}},
605 "zap": []any{map[any]any{"a": 5, "b": 6}},
606 },
607 key: "b", op: "in", match: ns.Slice(3, 4, 5),
608 expect: map[string]any{
609 "bar": []any{map[any]any{"a": 3, "b": 4}},
610 },
611 },
612 {
613 seq: map[string]any{
614 "foo": []any{map[any]any{"a": 1, "b": 2}},
615 "bar": []any{map[any]any{"a": 3, "b": 4}},
616 "zap": []any{map[any]any{"a": 5, "b": 6}},
617 },
618 key: "b", op: ">", match: 3,
619 expect: map[string]any{
620 "bar": []any{map[any]any{"a": 3, "b": 4}},
621 "zap": []any{map[any]any{"a": 5, "b": 6}},
622 },
623 },
624 {
625 seq: map[string]any{
626 "foo": []any{hmaps.Params{"a": 1, "b": 2}},
627 "bar": []any{hmaps.Params{"a": 3, "b": 4}},
628 "zap": []any{hmaps.Params{"a": 5, "b": 6}},
629 },
630 key: "B", op: ">", match: 3,
631 expect: map[string]any{
632 "bar": []any{hmaps.Params{"a": 3, "b": 4}},
633 "zap": []any{hmaps.Params{"a": 5, "b": 6}},
634 },
635 },
636 } {
637
638 testVariants := createTestVariants(test)
639 for j, test := range testVariants {
640 name := fmt.Sprintf("%d/%d %T %s %s", i, j, test.seq, test.op, test.key)
641 name = strings.ReplaceAll(name, "[]", "slice-of-")
642 t.Run(name, func(t *testing.T) {
643 var results any
644 var err error
645
646 if len(test.op) > 0 {
647 results, err = ns.Where(context.Background(), test.seq, test.key, test.op, test.match)
648 } else {
649 results, err = ns.Where(context.Background(), test.seq, test.key, test.match)
650 }
651 if b, ok := test.expect.(bool); ok && !b {
652 if err == nil {
653 t.Fatalf("[%d] Where didn't return an expected error", i)
654 }
655 } else {
656 if err != nil {
657 t.Fatalf("[%d] failed: %s", i, err)
658 }
659 if !reflect.DeepEqual(results, test.expect) {
660 t.Fatalf("Where clause matching %v with %v in seq %v (%T),\ngot\n%v (%T) but expected\n%v (%T)", test.key, test.match, test.seq, test.seq, results, results, test.expect, test.expect)
661 }
662 }
663 })
664 }
665 }
666
667 var err error
668 _, err = ns.Where(context.Background(), map[string]int{"a": 1, "b": 2}, "a", []byte("="), 1)
669 if err == nil {
670 t.Errorf("Where called with none string op value didn't return an expected error")
671 }
672
673 _, err = ns.Where(context.Background(), map[string]int{"a": 1, "b": 2}, "a", []byte("="), 1, 2)
674 if err == nil {
675 t.Errorf("Where called with more than two variable arguments didn't return an expected error")
676 }
677
678 _, err = ns.Where(context.Background(), map[string]int{"a": 1, "b": 2}, "a")
679 if err == nil {
680 t.Errorf("Where called with no variable arguments didn't return an expected error")
681 }
682 }
683
684 func TestCheckCondition(t *testing.T) {
685 t.Parallel()
686
687 ns := newNs()
688
689 type expect struct {
690 result bool
691 isError bool
692 }
693
694 for i, test := range []struct {
695 value reflect.Value
696 match reflect.Value
697 op string
698 expect
699 }{
700 {reflect.ValueOf(123), reflect.ValueOf(123), "", expect{true, false}},
•701 {reflect.ValueOf(int64(1<<53 + 1)), reflect.ValueOf(int64(1<<53 + 1)), "", expect{true, false}},
702 {reflect.ValueOf(int64(1<<53 + 1)), reflect.ValueOf(int64(1 << 53)), ">", expect{true, false}},
703 {reflect.ValueOf(uint64(1<<53 + 1)), reflect.ValueOf(uint8(1)), ">", expect{true, false}},
704 {reflect.ValueOf(int64(1<<53 + 1)), reflect.ValueOf(float64(1 << 53)), "", expect{false, false}},
705 {reflect.ValueOf(int64(1<<53 + 1)), reflect.ValueOf(float64(1 << 53)), ">", expect{true, false}},
706 {reflect.ValueOf(int64(1<<53 + 1)), reflect.ValueOf(uint64(1<<53 + 1)), "", expect{true, false}},
707 {reflect.ValueOf(-1), reflect.ValueOf(uint64(math.MaxUint64)), "<", expect{true, false}},
708 {reflect.ValueOf(uint64(math.MaxUint64)), reflect.ValueOf(uint8(1)), ">", expect{true, false}},
709 {reflect.ValueOf("foo"), reflect.ValueOf("foo"), "", expect{true, false}},
710 {
711 reflect.ValueOf(time.Date(2015, time.May, 26, 19, 18, 56, 12345, time.UTC)),
712 reflect.ValueOf(time.Date(2015, time.May, 26, 19, 18, 56, 12345, time.UTC)),
713 "",
714 expect{true, false},
715 },
716 {reflect.ValueOf(true), reflect.ValueOf(true), "", expect{true, false}},
717 {reflect.ValueOf(nil), reflect.ValueOf(nil), "", expect{true, false}},
718 {reflect.ValueOf(123), reflect.ValueOf(456), "!=", expect{true, false}},
719 {reflect.ValueOf("foo"), reflect.ValueOf("bar"), "!=", expect{true, false}},
720 {
721 reflect.ValueOf(time.Date(2015, time.May, 26, 19, 18, 56, 12345, time.UTC)),
722 reflect.ValueOf(time.Date(2015, time.April, 26, 19, 18, 56, 12345, time.UTC)),
723 "!=",
724 expect{true, false},
725 },
726 {reflect.ValueOf(true), reflect.ValueOf(false), "!=", expect{true, false}},
727 {reflect.ValueOf(123), reflect.ValueOf(nil), "!=", expect{true, false}},
728 {reflect.ValueOf(456), reflect.ValueOf(123), ">=", expect{true, false}},
729 {reflect.ValueOf("foo"), reflect.ValueOf("bar"), ">=", expect{true, false}},
730 {
731 reflect.ValueOf(time.Date(2015, time.May, 26, 19, 18, 56, 12345, time.UTC)),
732 reflect.ValueOf(time.Date(2015, time.April, 26, 19, 18, 56, 12345, time.UTC)),
733 ">=",
734 expect{true, false},
735 },
736 {reflect.ValueOf(456), reflect.ValueOf(123), ">", expect{true, false}},
737 {reflect.ValueOf("foo"), reflect.ValueOf("bar"), ">", expect{true, false}},
738 {
739 reflect.ValueOf(time.Date(2015, time.May, 26, 19, 18, 56, 12345, time.UTC)),
740 reflect.ValueOf(time.Date(2015, time.April, 26, 19, 18, 56, 12345, time.UTC)),
741 ">",
742 expect{true, false},
743 },
744 {reflect.ValueOf(123), reflect.ValueOf(456), "<=", expect{true, false}},
745 {reflect.ValueOf("bar"), reflect.ValueOf("foo"), "<=", expect{true, false}},
746 {
747 reflect.ValueOf(time.Date(2015, time.April, 26, 19, 18, 56, 12345, time.UTC)),
748 reflect.ValueOf(time.Date(2015, time.May, 26, 19, 18, 56, 12345, time.UTC)),
749 "<=",
750 expect{true, false},
751 },
752 {reflect.ValueOf(123), reflect.ValueOf(456), "<", expect{true, false}},
753 {reflect.ValueOf("bar"), reflect.ValueOf("foo"), "<", expect{true, false}},
754 {
755 reflect.ValueOf(time.Date(2015, time.April, 26, 19, 18, 56, 12345, time.UTC)),
756 reflect.ValueOf(time.Date(2015, time.May, 26, 19, 18, 56, 12345, time.UTC)),
757 "<",
758 expect{true, false},
759 },
760 {reflect.ValueOf(123), reflect.ValueOf([]int{123, 45, 678}), "in", expect{true, false}},
761 {reflect.ValueOf("foo"), reflect.ValueOf([]string{"foo", "bar", "baz"}), "in", expect{true, false}},
762 {
763 reflect.ValueOf(time.Date(2015, time.May, 26, 19, 18, 56, 12345, time.UTC)),
764 reflect.ValueOf([]time.Time{
765 time.Date(2015, time.April, 26, 19, 18, 56, 12345, time.UTC),
766 time.Date(2015, time.May, 26, 19, 18, 56, 12345, time.UTC),
767 time.Date(2015, time.June, 26, 19, 18, 56, 12345, time.UTC),
768 }),
769 "in",
770 expect{true, false},
771 },
772 {reflect.ValueOf(123), reflect.ValueOf([]int{45, 678}), "not in", expect{true, false}},
773 {reflect.ValueOf(123), reflect.ValueOf([]int{}), "not in", expect{true, false}},
774 // Numbers match across types in "in" and "not in".
775 {reflect.ValueOf(int64(123)), reflect.ValueOf([]int{123}), "in", expect{true, false}},
776 {reflect.ValueOf(uint64(123)), reflect.ValueOf([]int{123}), "in", expect{true, false}},
777 {reflect.ValueOf(123.0), reflect.ValueOf([]int{123}), "in", expect{true, false}},
778 {reflect.ValueOf(123), reflect.ValueOf([]float64{123}), "in", expect{true, false}},
779 {reflect.ValueOf(123), reflect.ValueOf([]any{123.0}), "in", expect{true, false}},
780 {reflect.ValueOf(123.5), reflect.ValueOf([]int{123}), "in", expect{false, false}},
781 {reflect.ValueOf(int64(123)), reflect.ValueOf([]string{"123"}), "in", expect{false, false}},
782 {reflect.ValueOf(int64(123)), reflect.ValueOf([]string{"123"}), "not in", expect{true, false}},
•783 {reflect.ValueOf(int64(1<<53 + 1)), reflect.ValueOf([]int64{1<<53 + 1}), "in", expect{true, false}},
784 {reflect.ValueOf(int64(1<<53 + 1)), reflect.ValueOf([]float64{1 << 53}), "in", expect{false, false}},
785 {reflect.ValueOf(int64(1<<53 + 1)), reflect.ValueOf([]any{float64(1 << 53)}), "not in", expect{true, false}},
786 {reflect.ValueOf(uint64(math.MaxUint64)), reflect.ValueOf([]any{uint64(math.MaxUint64)}), "in", expect{true, false}},
787 {reflect.ValueOf(uint64(math.MaxUint64)), reflect.ValueOf([]any{-1}), "in", expect{false, false}},
788 {reflect.ValueOf("foo"), reflect.ValueOf([]string{"bar", "baz"}), "not in", expect{true, false}},
789 {
790 reflect.ValueOf(time.Date(2015, time.May, 26, 19, 18, 56, 12345, time.UTC)),
791 reflect.ValueOf([]time.Time{
792 time.Date(2015, time.February, 26, 19, 18, 56, 12345, time.UTC),
793 time.Date(2015, time.March, 26, 19, 18, 56, 12345, time.UTC),
794 time.Date(2015, time.April, 26, 19, 18, 56, 12345, time.UTC),
795 }),
796 "not in",
797 expect{true, false},
798 },
799 {reflect.ValueOf("foo"), reflect.ValueOf("bar-foo-baz"), "in", expect{true, false}},
800 {reflect.ValueOf("foo"), reflect.ValueOf("bar--baz"), "not in", expect{true, false}},
801 {reflect.Value{}, reflect.ValueOf("foo"), "", expect{false, false}},
802 {reflect.ValueOf("foo"), reflect.Value{}, "", expect{false, false}},
803 {reflect.ValueOf((*TstX)(nil)), reflect.ValueOf("foo"), "", expect{false, false}},
804 {reflect.ValueOf("foo"), reflect.ValueOf((*TstX)(nil)), "", expect{false, false}},
805 {reflect.ValueOf(true), reflect.ValueOf("foo"), "", expect{false, false}},
806 {reflect.ValueOf("foo"), reflect.ValueOf(true), "", expect{false, false}},
807 {reflect.ValueOf("foo"), reflect.ValueOf(map[int]string{}), "", expect{false, false}},
808 {reflect.ValueOf("foo"), reflect.ValueOf([]int{1, 2}), "", expect{false, false}},
809 {reflect.ValueOf((*TstX)(nil)), reflect.ValueOf((*TstX)(nil)), ">", expect{false, false}},
810 {reflect.ValueOf(true), reflect.ValueOf(false), ">", expect{false, false}},
811 {reflect.ValueOf(123), reflect.ValueOf([]int{}), "in", expect{false, false}},
812 {reflect.ValueOf(123), reflect.ValueOf(123), "op", expect{false, true}},
813
814 // Issue #3718
815 {reflect.ValueOf([]any{"a"}), reflect.ValueOf([]string{"a", "b"}), "intersect", expect{true, false}},
816 {reflect.ValueOf([]string{"a"}), reflect.ValueOf([]any{"a", "b"}), "intersect", expect{true, false}},
817 {reflect.ValueOf([]any{1, 2}), reflect.ValueOf([]int{1}), "intersect", expect{true, false}},
818 {reflect.ValueOf([]int{1}), reflect.ValueOf([]any{1, 2}), "intersect", expect{true, false}},
819 } {
820 result, err := ns.checkCondition(test.value, test.match, test.op)
821 if test.expect.isError {
822 if err == nil {
823 t.Errorf("[%d] checkCondition didn't return an expected error", i)
824 }
825 } else {
826 if err != nil {
827 t.Errorf("[%d] failed: %s", i, err)
828 continue
829 }
830 if result != test.expect.result {
831 t.Errorf("[%d] check condition %v %s %v, got %v but expected %v", i, test.value, test.op, test.match, result, test.expect.result)
832 }
833 }
834 }
835 }
836
837 func TestEvaluateSubElem(t *testing.T) {
838 t.Parallel()
839 tstx := TstX{A: "foo", B: "bar"}
840 var inner struct {
841 S fmt.Stringer
842 }
843 inner.S = tstx
844 interfaceValue := reflect.ValueOf(&inner).Elem().Field(0)
845
846 for i, test := range []struct {
847 value reflect.Value
848 key string
849 expect any
850 }{
851 {reflect.ValueOf(tstx), "A", "foo"},
852 {reflect.ValueOf(&tstx), "TstRp", "rfoo"},
853 {reflect.ValueOf(tstx), "TstRv", "rbar"},
854 //{reflect.ValueOf(map[int]string{1: "foo", 2: "bar"}), 1, "foo"},
855 {reflect.ValueOf(map[string]string{"key1": "foo", "key2": "bar"}), "key1", "foo"},
856 {interfaceValue, "String", "A: foo, B: bar"},
857 {reflect.Value{}, "foo", false},
858 //{reflect.ValueOf(map[int]string{1: "foo", 2: "bar"}), 1.2, false},
859 {reflect.ValueOf(tstx), "unexported", false},
860 {reflect.ValueOf(tstx), "unexportedMethod", false},
861 {reflect.ValueOf(tstx), "MethodWithArg", false},
862 {reflect.ValueOf(tstx), "MethodReturnNothing", false},
863 {reflect.ValueOf(tstx), "MethodReturnErrorOnly", false},
864 {reflect.ValueOf(tstx), "MethodReturnTwoValues", false},
865 {reflect.ValueOf(tstx), "MethodReturnValueWithError", false},
866 {reflect.ValueOf((*TstX)(nil)), "A", false},
867 {reflect.ValueOf(tstx), "C", false},
868 {reflect.ValueOf(map[int]string{1: "foo", 2: "bar"}), "1", false},
869 {reflect.ValueOf([]string{"foo", "bar"}), "1", false},
870 } {
871 result, err := evaluateSubElem(reflect.ValueOf(context.Background()), test.value, test.key)
872 if b, ok := test.expect.(bool); ok && !b {
873 if err == nil {
874 t.Errorf("[%d] evaluateSubElem didn't return an expected error", i)
875 }
876 } else {
877 if err != nil {
878 t.Errorf("[%d] failed: %s", i, err)
879 continue
880 }
881 if result.Kind() != reflect.String || result.String() != test.expect {
882 t.Errorf("[%d] evaluateSubElem with %v got %v but expected %v", i, test.key, result, test.expect)
883 }
884 }
885 }
886 }
887
888 func BenchmarkWhereOps(b *testing.B) {
889 ns := newNs()
890 var seq []map[string]string
891 ctx := context.Background()
892 for range 500 {
893 seq = append(seq, map[string]string{"foo": "bar"})
894 }
895 for range 500 {
896 seq = append(seq, map[string]string{"foo": "baz"})
897 }
898 // Shuffle the sequence.
899 for i := range seq {
900 j := rand.Intn(i + 1)
901 seq[i], seq[j] = seq[j], seq[i]
902 }
903 // results, err = ns.Where(context.Background(), test.seq, test.key, test.op, test.match)
904 runOps := func(b *testing.B, op, match string) {
905 _, err := ns.Where(ctx, seq, "foo", op, match)
906 if err != nil {
907 b.Fatal(err)
908 }
909 }
910
911 b.Run("eq", func(b *testing.B) {
912 for b.Loop() {
913 runOps(b, "eq", "bar")
914 }
915 })
916
917 b.Run("ne", func(b *testing.B) {
918 for b.Loop() {
919 runOps(b, "ne", "baz")
920 }
921 })
922
923 b.Run("like", func(b *testing.B) {
924 for b.Loop() {
925 runOps(b, "like", "^bar")
926 }
927 })
928 }
929
930 func BenchmarkWhereSliceOfStructPointersWithMethod(b *testing.B) {
931 // TstRv2
932 ns := newNs()
933 seq := []*TstX{}
934
935 for i := range 1000 {
936 seq = append(seq, &TstX{A: "foo", B: "bar"})
937 if i%2 == 0 {
938 seq = append(seq, &TstX{A: "baz", B: "qux"})
939 }
940 }
941
942 for b.Loop() {
943 _, err := ns.Where(context.Background(), seq, "TstRv2", "eq", "bar")
944 if err != nil {
945 b.Fatal(err)
946 }
947 }
948 }