Debian Codegraph @

cache_test.go

raw symbols SCIP
1 package schemascip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/
2
3 import (
4 "reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/"
5 "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/"
6
7 utilsscip-go gomod github.com/gofiber/utils/v2 v2.4.2 `github.com/gofiber/utils/v2`/ "github.com/gofiber/utils/v2scip-go gomod github.com/gofiber/utils/v2 v2.4.2 `github.com/gofiber/utils/v2`/"
8 )
9
10 func TestNextPathSegmentscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestNextPathSegment().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
11 cases := []struct {
12 path string
13 start int
14 end int
15 segment string
16 wantErr bool
17 }{
18 {"a.b", 0, 1, "a", false},
19 {"a.b", 2, 3, "b", false},
20 {"abc.defgh", 0, 3, "abc", false},
21 // dot exactly on the SWAR word boundary
22 {"abcdefgh.x", 0, 8, "abcdefgh", false},
23 // segments longer than one SWAR word
24 {"averylongsegmentname.next", 0, 20, "averylongsegmentname", false},
25 {"averylongsegmentname.next", 21, 25, "next", false},
26 {"noseparatorhere", 0, 15, "noseparatorhere", false},
27 // non-ASCII bytes must pass through the word scan untouched
28 {"héllo.wörld", 0, 6, "héllo", false},
29 {"", 0, 0, "", true},
30 {".abc", 0, 0, "", true},
31 {"a..b", 2, 0, "", true},
32 }
33 for _, c := range cases {
34 end, segment, err := nextPathSegmentscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/nextPathSegment().(c.path, c.start)
35 if c.wantErr {
36 if err == nil {
37 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("nextPathSegment(%q, %d): expected error", c.path, c.start)
38 }
39 continue
40 }
41 if err != nil {
42 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("nextPathSegment(%q, %d): unexpected error %v", c.path, c.start, err)
43 continue
44 }
45 if end != c.end || segment != c.segment {
46 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("nextPathSegment(%q, %d) = (%d, %q), want (%d, %q)",
47 c.path, c.start, end, segment, c.end, c.segment)
48 }
49 }
50 }
51
52 func BenchmarkNextPathSegmentscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/BenchmarkNextPathSegment().(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#) {
53 const path = "averylongsegmentname.middle.tail"
54 for b.Loopscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Loop().() {
55 for start := 0; ; {
56 end, _, err := nextPathSegmentscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/nextPathSegment().(path, start)
57 if err != nil || end == len(path) {
58 break
59 }
60 start = end + 1
61 }
62 }
63 }
64
65 // The setDefaults fast path must not be defeated by anonymous pointers the
66 // walk can never allocate (unexported ones).
67 func TestNeedsDefaultsWalkscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestNeedsDefaultsWalk().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
68 type hidden struct {
69 X string `schema:"x"`
70 }
71 type onlyUnexported struct {
72 *hidden
73 A string `schema:"a"`
74 }
75
76 d := NewDecoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewDecoder().()
77 if d.cachescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Decoder#cache..getscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/cache#get().(reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/TypeOf().(onlyUnexported{})).needsDefaultsWalkscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/structInfo#needsDefaultsWalk. {
78 t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().("unexported anonymous pointer must not force the defaults walk")
79 }
80
81 type Inner struct {
82 Y string `schema:"y"`
83 }
84 type withExported struct {
85 *Inner
86 A string `schema:"a"`
87 }
88 if !d.cachescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Decoder#cache..getscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/cache#get().(reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/TypeOf().(withExported{})).needsDefaultsWalkscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/structInfo#needsDefaultsWalk. {
89 t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().("exported anonymous pointer must keep the defaults walk (it allocates)")
90 }
91
92 type withDefaults struct {
93 A string `schema:"a,default:z"`
94 }
95 if !d.cachescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Decoder#cache..getscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/cache#get().(reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/TypeOf().(withDefaults{})).needsDefaultsWalkscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/structInfo#needsDefaultsWalk. {
96 t.Errorscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Error().("default tags must keep the defaults walk")
97 }
98 }
99
100 func BenchmarkParsePathCacheMissscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/BenchmarkParsePathCacheMiss().(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#) {
101 type Nested struct {
102 Value string `schema:"value"`
103 }
104 type Outer struct {
105 Items []Nested `schema:"items"`
106 }
107 d := NewDecoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewDecoder().()
108 typ := reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/TypeOf().(Outer{})
109 b.ReportAllocsscip-go gomod github.com/golang/go/src go1.25.0 testing/B#ReportAllocs().()
110 info := d.cachescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Decoder#cache..getscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/cache#get().(typ)
111 for b.Loopscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Loop().() {
112 info.pathsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/structInfo#paths..Clearscip-go gomod github.com/golang/go/src go1.25.0 sync/Map#Clear().()
113 if _, err := d.cachescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Decoder#cache..parsePathscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/cache#parsePath().("items.0.value", typ); err != nil {
114 b.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
115 }
116 }
117 }
118
119 // Decode callers (e.g. Fiber binders) pass map keys aliasing reused request
120 // buffers; pathCache must clone the key so later buffer reuse cannot poison it.
121 func TestParsePathDetachesCacheKeyscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/TestParsePathDetachesCacheKey().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
122 // Use a slice-index path: statically-resolvable keys are served from the
123 // direct map and never stored in the sync.Map path cache.
124 type Item struct {
125 Value string `schema:"value"`
126 }
127 type S struct {
128 Items []Item `schema:"items"`
129 }
130 d := NewDecoderscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/NewDecoder().()
131 buf := []byte("items.0.value")
132 var s S
133 if err := d.Decodescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Decoder#Decode().(&s, map[string][]string{utilsscip-go gomod github.com/gofiber/utils/v2 v2.4.2 `github.com/gofiber/utils/v2`/.UnsafeStringscip-go gomod github.com/gofiber/utils/v2 v2.4.2 `github.com/gofiber/utils/v2`/UnsafeString().(buf): {"x"}}); err != nil {
134 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
135 }
136 copy(buf, "xtems.9.qalue") // simulate fasthttp buffer reuse mutating the key bytes
137
138 count := 0
139 d.cachescip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/Decoder#cache..getscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/cache#get().(reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/TypeOf().(s)).pathsscip-go gomod github.com/gofiber/schema . `github.com/gofiber/schema`/structInfo#paths..Rangescip-go gomod github.com/golang/go/src go1.25.0 sync/Map#Range().(func(key, _ any) bool {
140 count++
141 if k := key.(string); k != "items.0.value" {
142 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("path cache key mutated to %q; key must be cloned before caching", k)
143 }
144 return true
145 })
146 if count != 1 {
147 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("expected 1 cached path, got %d", count)
148 }
149 }