-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstruct.go
More file actions
389 lines (362 loc) · 10.3 KB
/
Copy pathstruct.go
File metadata and controls
389 lines (362 loc) · 10.3 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
package validator
import (
"errors"
"reflect"
"slices"
"sort"
"strings"
"time"
)
var timeType = reflect.TypeFor[time.Time]()
type fieldPlan struct {
name string
index []int
rules string
leaf bool
dead bool // ambiguous promotion: unreachable via Go selectors, pruned
}
// structPlan caches a struct type's reflection plan; byName indexes ALL entries
// (cross-field/binding reach untagged fields).
type structPlan struct {
entries []fieldPlan
byName map[string]int
rules map[string]string // field->expression; copy-on-write before mutation
execPlan []compiledField // name-sorted
ambiguous map[string]int // build-time: name -> depth of an ambiguous collision
}
// put resolves name collisions like Go promotion: shallower shadows deeper;
// equal-depth distinct fields are ambiguous (Go rejects the selector), so
// neither is reachable under the promoted name.
func (sp *structPlan) put(fp fieldPlan) {
d := len(fp.index)
if ad, ok := sp.ambiguous[fp.name]; ok {
if d >= ad {
return
}
delete(sp.ambiguous, fp.name) // a shallower field is unambiguous again
}
if i, ok := sp.byName[fp.name]; ok {
existing := sp.entries[i]
switch {
case d < len(existing.index):
sp.entries[i] = fp
case d == len(existing.index) && !slices.Equal(fp.index, existing.index):
sp.ambiguous[fp.name] = d
sp.entries[i].dead = true
}
return
}
sp.byName[fp.name] = len(sp.entries)
sp.entries = append(sp.entries, fp)
}
// prune drops ambiguous entries and ghost subtrees — dotted paths whose
// ancestor entry resolves to a different field (a shadowed, Go-unreachable
// path) — then reindexes. Survivors go to a fresh slice: isGhost reads
// entries/byName, so compacting in place would alias data it still consults.
func (sp *structPlan) prune() {
kept := make([]fieldPlan, 0, len(sp.entries))
for _, e := range sp.entries {
if e.dead || sp.isGhost(e) {
continue
}
kept = append(kept, e)
}
sp.entries = kept
sp.byName = make(map[string]int, len(kept))
for i := range kept {
sp.byName[kept[i].name] = i
}
sp.ambiguous = nil
}
func (sp *structPlan) isGhost(e fieldPlan) bool {
name := e.name
for {
dot := strings.LastIndex(name, ".")
if dot < 0 {
return false
}
name = name[:dot]
if i, ok := sp.byName[name]; ok {
a := sp.entries[i]
if a.dead {
return true
}
if len(a.index) > len(e.index) || !slices.Equal(a.index, e.index[:len(a.index)]) {
return true
}
}
}
}
type compiledField struct {
name string // field/error name and cross-field scope
index []int // struct field path (nil on map/var path)
container *compiled // value rules (nil if none)
element *compiled // per-element dive rules (nil if no dive)
hasDive bool
buildErr error
}
// structSource is the reflection-backed source for a struct value (nil-pointer safe).
type structSource struct {
val reflect.Value
plan *structPlan
}
func (s structSource) lookup(name string) (reflect.Value, bool) {
i, ok := s.plan.byName[name]
if !ok {
return reflect.Value{}, false
}
fv := s.val
for _, idx := range s.plan.entries[i].index {
sv, ok := derefToStruct(fv)
if !ok {
return reflect.Value{}, false
}
fv = sv.Field(idx)
}
return getValueV(fv), true
}
func (v *Validator) getStructPlan(t reflect.Type) *structPlan {
if c, ok := v.typeCache.Load(t); ok {
return c.(*structPlan)
}
plan := v.buildStructPlan(t)
actual, _ := v.typeCache.LoadOrStore(t, plan)
return actual.(*structPlan)
}
func (v *Validator) buildStructPlan(t reflect.Type) *structPlan {
sp := &structPlan{byName: make(map[string]int), ambiguous: map[string]int{}}
v.collectFields(t, nil, "", sp, map[reflect.Type]bool{}, false)
sp.prune()
for _, fp := range sp.entries {
if fp.rules != "" {
if sp.rules == nil {
sp.rules = make(map[string]string, len(sp.entries))
}
sp.rules[fp.name] = fp.rules
}
}
sp.execPlan = v.buildExecPlan(sp)
return sp
}
// buildExecPlan precompiles every rule-bearing field, name-sorted for deterministic output.
func (v *Validator) buildExecPlan(sp *structPlan) []compiledField {
if len(sp.rules) == 0 {
return nil
}
exec := make([]compiledField, 0, len(sp.rules))
for name, expr := range sp.rules {
if strings.TrimSpace(expr) == "" {
continue
}
cf := v.buildCompiledField(name, expr)
if i, ok := sp.byName[name]; ok {
cf.index = sp.entries[i].index
}
exec = append(exec, cf)
}
sort.Slice(exec, func(i, j int) bool { return exec[i].name < exec[j].name })
return exec
}
// buildCompiledField splits one field's expression on a top-level dive and compiles each segment.
func (v *Validator) buildCompiledField(name, expr string) compiledField {
cf := compiledField{name: name}
ds := v.splitDive(expr)
if ds.err != nil {
cf.buildErr = ds.err
return cf
}
if ds.nested {
cf.buildErr = errors.New("multiple top-level dive is not supported")
return cf
}
cf.hasDive = ds.hasDive
if ds.container != "" {
c, err := v.compile(ds.container)
if err != nil {
cf.buildErr = err
return cf
}
cf.container = c
}
if ds.hasDive && ds.element != "" {
e, err := v.compile(ds.element)
if err != nil {
cf.buildErr = err
return cf
}
if e.sometimes {
// elements always exist inside their collection: a silent no-op, so reject
cf.buildErr = errors.New(errSometimesInDive)
return cf
}
cf.element = e
}
return cf
}
// collectFields flattens a struct (dotted nested names, flat embedded promotion);
// seen guards recursion, skipRules drops a `-` subtree's validation but keeps it bindable.
func (v *Validator) collectFields(
t reflect.Type,
prefixIdx []int,
prefixName string,
sp *structPlan,
seen map[reflect.Type]bool,
skipRules bool,
) {
if seen[t] {
return
}
seen[t] = true
defer delete(seen, t)
for i := 0; i < t.NumField(); i++ {
sf := t.Field(i)
ft := derefType(sf.Type)
isStruct := ft.Kind() == reflect.Struct && ft != timeType
embedded := sf.Anonymous && isStruct
// skip unexported non-embedded fields unless private-field validation is on
if sf.PkgPath != "" && !embedded && !v.privateFieldValidation {
continue
}
rules := sf.Tag.Get(v.tagName)
dash := rules == "-"
if dash || skipRules {
rules = "" // not validated, still in the plan
}
childSkip := skipRules || dash
idx := append(append([]int{}, prefixIdx...), i)
name := sf.Name
if v.tagNameFunc != nil {
if n := v.tagNameFunc(sf); n != "" {
name = n
}
}
fullName := name
if prefixName != "" {
fullName = prefixName + "." + name
}
if embedded {
// The embedded field is addressable under its type name: keep it in
// the plan (cross-field/AddRules) and honor its tag as a value rule,
// like a named struct field. Children promote flat (no name segment).
if sf.PkgPath == "" || v.privateFieldValidation {
sp.put(fieldPlan{name: fullName, index: idx, rules: rules, leaf: false})
}
v.collectFields(ft, idx, prefixName, sp, seen, childSkip)
continue
}
if isStruct {
if seen[ft] || !v.hasCollectableField(ft, seen) {
// recursion cut (self-referential type) or opaque struct (no
// reachable subfields, e.g. named time types): a leaf, so Bind
// whole-assigns it and byName resolves it.
sp.put(fieldPlan{name: fullName, index: idx, rules: rules, leaf: true})
continue
}
// always in the plan, tagged or not: cross-field rules, AddRules and
// Bind lookups resolve the struct itself; a rule validates it as a value.
sp.put(fieldPlan{name: fullName, index: idx, rules: rules, leaf: false})
v.collectFields(ft, idx, fullName, sp, seen, childSkip)
} else {
sp.put(fieldPlan{name: fullName, index: idx, rules: rules, leaf: true})
}
}
}
// hasCollectableField reports whether recursing into t would reach any field
// (exported, embedded-promoted, or unexported under private-field validation);
// seen mirrors collectFields' recursion guard.
func (v *Validator) hasCollectableField(t reflect.Type, seen map[reflect.Type]bool) bool {
if seen[t] {
return false
}
seen[t] = true
defer delete(seen, t)
for i := 0; i < t.NumField(); i++ {
sf := t.Field(i)
ft := derefType(sf.Type)
embedded := sf.Anonymous && ft.Kind() == reflect.Struct && ft != timeType
if sf.PkgPath != "" && !embedded && !v.privateFieldValidation {
continue
}
if embedded {
if sf.PkgPath == "" || v.privateFieldValidation {
return true // the embedded entry itself
}
if v.hasCollectableField(ft, seen) {
return true
}
continue
}
return true
}
return false
}
// attachSource picks the data source: struct via ssVal/ssPlan, else src; a nil
// struct/map pointer validates as that type's zero value.
func (v *Validator) attachSource(vd *Validation, data any) {
switch d := data.(type) {
case nil:
vd.src = mapSource{m: map[string]any{}}
return
case map[string]any:
vd.src = mapSource{m: d}
return
}
rv := reflect.ValueOf(data)
// bounded against a recursive named pointer value
for i := 0; i < maxDerefDepth && rv.Kind() == reflect.Pointer; i++ {
if rv.IsNil() {
// nil struct/map pointer: validate as zero value so field rules still apply
switch et := derefType(rv.Type()); et.Kind() {
case reflect.Struct:
if et != timeType {
vd.ssVal, vd.ssPlan = reflect.New(et).Elem(), v.getStructPlan(et)
return
}
case reflect.Map:
vd.src = mapSource{m: map[string]any{}}
return
}
attachVar(vd, data)
return
}
rv = rv.Elem()
}
switch rv.Kind() {
case reflect.Struct:
// unsafe read of unexported composite fields needs addressability
if v.privateFieldValidation && !rv.CanAddr() {
addr := reflect.New(rv.Type()).Elem()
addr.Set(rv)
rv = addr
}
vd.ssVal, vd.ssPlan = rv, v.getStructPlan(rv.Type())
return
case reflect.Map:
if m, ok := asStringMap(rv.Interface()); ok {
vd.src = mapSource{m: m}
return
}
}
attachVar(vd, data)
}
// derefType follows pointer layers to the underlying type; bounded against type P *P.
func derefType(t reflect.Type) reflect.Type {
for i := 0; i < maxDerefDepth && t.Kind() == reflect.Pointer; i++ {
t = t.Elem()
}
return t
}
// derefToStruct follows pointers to a struct (bounded against type P *P); false on
// nil/non-struct so Field never panics.
func derefToStruct(v reflect.Value) (reflect.Value, bool) {
for d := 0; d < maxDerefDepth && v.Kind() == reflect.Pointer; d++ {
if v.IsNil() {
return reflect.Value{}, false
}
v = v.Elem()
}
if v.Kind() != reflect.Struct {
return reflect.Value{}, false
}
return v, true
}