-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathbuilder.go
558 lines (509 loc) · 14.8 KB
/
builder.go
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// Copyright 2023-2024 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package protovalidate
import (
"fmt"
"sync"
"sync/atomic"
"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate"
pvcel "github.com/bufbuild/protovalidate-go/cel"
"github.com/bufbuild/protovalidate-go/resolve"
"github.com/google/cel-go/cel"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/types/dynamicpb"
)
//nolint:gochecknoglobals
var (
celRuleDescriptor = (&validate.FieldConstraints{}).ProtoReflect().Descriptor().Fields().ByName("cel")
celRuleField = fieldPathElement(celRuleDescriptor)
)
// builder is a build-through cache of message evaluators keyed off the provided
// descriptor.
type builder struct {
mtx sync.Mutex // serializes cache writes.
cache atomic.Pointer[messageCache] // copy-on-write cache.
env *cel.Env
constraints cache
extensionTypeResolver protoregistry.ExtensionTypeResolver
allowUnknownFields bool
Load func(desc protoreflect.MessageDescriptor) messageEvaluator
}
// newBuilder initializes a new Builder.
func newBuilder(
env *cel.Env,
disableLazy bool,
extensionTypeResolver protoregistry.ExtensionTypeResolver,
allowUnknownFields bool,
seedDesc ...protoreflect.MessageDescriptor,
) *builder {
bldr := &builder{
env: env,
constraints: newCache(),
extensionTypeResolver: extensionTypeResolver,
allowUnknownFields: allowUnknownFields,
}
if disableLazy {
bldr.Load = bldr.load
} else {
bldr.Load = bldr.loadOrBuild
}
cache := make(messageCache, len(seedDesc))
for _, desc := range seedDesc {
bldr.build(desc, cache)
}
bldr.cache.Store(&cache)
return bldr
}
// load returns a pre-cached MessageEvaluator for the given descriptor or, if
// the descriptor is unknown, returns an evaluator that always resolves to a
// errors.CompilationError.
func (bldr *builder) load(desc protoreflect.MessageDescriptor) messageEvaluator {
if eval, ok := (*bldr.cache.Load())[desc]; ok {
return eval
}
return unknownMessage{desc: desc}
}
// loadOrBuild either returns a memoized MessageEvaluator for the given
// descriptor or lazily constructs a new one. This method is thread-safe via
// locking.
func (bldr *builder) loadOrBuild(desc protoreflect.MessageDescriptor) messageEvaluator {
if eval, ok := (*bldr.cache.Load())[desc]; ok {
return eval
}
bldr.mtx.Lock()
defer bldr.mtx.Unlock()
cache := *bldr.cache.Load()
if eval, ok := cache[desc]; ok {
return eval
}
newCache := cache.Clone()
msgEval := bldr.build(desc, newCache)
bldr.cache.Store(&newCache)
return msgEval
}
func (bldr *builder) build(
desc protoreflect.MessageDescriptor,
cache messageCache,
) *message {
if eval, ok := cache[desc]; ok {
return eval
}
msgEval := &message{}
cache[desc] = msgEval
bldr.buildMessage(desc, msgEval, cache)
return msgEval
}
func (bldr *builder) buildMessage(
desc protoreflect.MessageDescriptor, msgEval *message,
cache messageCache,
) {
msgConstraints := resolve.MessageConstraints(desc)
if msgConstraints.GetDisabled() {
return
}
steps := []func(
desc protoreflect.MessageDescriptor,
msgConstraints *validate.MessageConstraints,
msg *message,
cache messageCache,
){
bldr.processMessageExpressions,
bldr.processOneofConstraints,
bldr.processFields,
}
for _, step := range steps {
if step(desc, msgConstraints, msgEval, cache); msgEval.Err != nil {
break
}
}
}
func (bldr *builder) processMessageExpressions(
desc protoreflect.MessageDescriptor,
msgConstraints *validate.MessageConstraints,
msgEval *message,
_ messageCache,
) {
exprs := expressions{
Constraints: msgConstraints.GetCel(),
}
compiledExprs, err := compile(
exprs,
bldr.env,
cel.Types(dynamicpb.NewMessage(desc)),
cel.Variable("this", cel.ObjectType(string(desc.FullName()))),
)
if err != nil {
msgEval.Err = err
return
}
msgEval.Append(celPrograms{
programSet: compiledExprs,
})
}
func (bldr *builder) processOneofConstraints(
desc protoreflect.MessageDescriptor,
_ *validate.MessageConstraints,
msgEval *message,
_ messageCache,
) {
oneofs := desc.Oneofs()
for i := 0; i < oneofs.Len(); i++ {
oneofDesc := oneofs.Get(i)
oneofConstraints := resolve.OneofConstraints(oneofDesc)
oneofEval := oneof{
Descriptor: oneofDesc,
Required: oneofConstraints.GetRequired(),
}
msgEval.Append(oneofEval)
}
}
func (bldr *builder) processFields(
desc protoreflect.MessageDescriptor,
_ *validate.MessageConstraints,
msgEval *message,
cache messageCache,
) {
fields := desc.Fields()
for i := 0; i < fields.Len(); i++ {
fdesc := fields.Get(i)
fieldConstraints := resolve.FieldConstraints(fdesc)
fldEval, err := bldr.buildField(fdesc, fieldConstraints, cache)
if err != nil {
msgEval.Err = err
return
}
msgEval.Append(fldEval)
}
}
func (bldr *builder) buildField(
fieldDescriptor protoreflect.FieldDescriptor,
fieldConstraints *validate.FieldConstraints,
cache messageCache,
) (field, error) {
fld := field{
Value: value{
Descriptor: fieldDescriptor,
},
Required: fieldConstraints.GetRequired(),
IgnoreEmpty: fieldDescriptor.HasPresence() ||
bldr.shouldIgnoreEmpty(fieldConstraints),
IgnoreDefault: fieldDescriptor.HasPresence() &&
bldr.shouldIgnoreDefault(fieldConstraints),
}
if fld.IgnoreDefault {
fld.Zero = bldr.zeroValue(fieldDescriptor, false)
}
err := bldr.buildValue(fieldDescriptor, fieldConstraints, &fld.Value, cache)
return fld, err
}
func (bldr *builder) buildValue(
fdesc protoreflect.FieldDescriptor,
constraints *validate.FieldConstraints,
valEval *value,
cache messageCache,
) (err error) {
steps := []func(
fdesc protoreflect.FieldDescriptor,
fieldConstraints *validate.FieldConstraints,
valEval *value,
cache messageCache,
) error{
bldr.processIgnoreEmpty,
bldr.processFieldExpressions,
bldr.processEmbeddedMessage,
bldr.processWrapperConstraints,
bldr.processStandardConstraints,
bldr.processAnyConstraints,
bldr.processEnumConstraints,
bldr.processMapConstraints,
bldr.processRepeatedConstraints,
}
for _, step := range steps {
if err = step(fdesc, constraints, valEval, cache); err != nil {
return err
}
}
return nil
}
func (bldr *builder) processIgnoreEmpty(
fdesc protoreflect.FieldDescriptor,
constraints *validate.FieldConstraints,
val *value,
_ messageCache,
) error {
// the only time we need to ignore empty on a value is if it's evaluating a
// field item (repeated element or map key/value).
val.IgnoreEmpty = val.NestedRule != nil && bldr.shouldIgnoreEmpty(constraints)
if val.IgnoreEmpty {
val.Zero = bldr.zeroValue(fdesc, val.NestedRule != nil)
}
return nil
}
func (bldr *builder) processFieldExpressions(
fieldDesc protoreflect.FieldDescriptor,
fieldConstraints *validate.FieldConstraints,
eval *value,
_ messageCache,
) error {
exprs := expressions{
Constraints: fieldConstraints.GetCel(),
}
celTyp := pvcel.ProtoFieldToType(fieldDesc, false, eval.NestedRule != nil)
opts := append(
pvcel.RequiredEnvOptions(fieldDesc),
cel.Variable("this", celTyp),
)
compiledExpressions, err := compile(exprs, bldr.env, opts...)
if err != nil {
return err
}
for i := range compiledExpressions {
compiledExpressions[i].Path = []*validate.FieldPathElement{
{
FieldNumber: proto.Int32(celRuleField.GetFieldNumber()),
FieldType: celRuleField.GetFieldType().Enum(),
FieldName: proto.String(celRuleField.GetFieldName()),
Subscript: &validate.FieldPathElement_Index{
Index: uint64(i),
},
},
}
compiledExpressions[i].Descriptor = celRuleDescriptor
}
if len(compiledExpressions) > 0 {
eval.Constraints = append(eval.Constraints,
celPrograms{
base: newBase(eval),
programSet: compiledExpressions,
},
)
}
return nil
}
func (bldr *builder) processEmbeddedMessage(
fdesc protoreflect.FieldDescriptor,
rules *validate.FieldConstraints,
valEval *value,
cache messageCache,
) error {
if !isMessageField(fdesc) ||
bldr.shouldSkip(rules) ||
fdesc.IsMap() ||
(fdesc.IsList() && valEval.NestedRule == nil) {
return nil
}
embedEval := bldr.build(fdesc.Message(), cache)
if err := embedEval.Err; err != nil {
return &CompilationError{cause: fmt.Errorf(
"failed to compile embedded type %s for %s: %w",
fdesc.Message().FullName(), fdesc.FullName(), err)}
}
valEval.Append(&embeddedMessage{
base: newBase(valEval),
message: embedEval,
})
return nil
}
func (bldr *builder) processWrapperConstraints(
fdesc protoreflect.FieldDescriptor,
rules *validate.FieldConstraints,
valEval *value,
cache messageCache,
) error {
if !isMessageField(fdesc) ||
bldr.shouldSkip(rules) ||
fdesc.IsMap() ||
(fdesc.IsList() && valEval.NestedRule == nil) {
return nil
}
expectedWrapperDescriptor, ok := expectedWrapperConstraints(fdesc.Message().FullName())
if !ok || !rules.ProtoReflect().Has(expectedWrapperDescriptor) {
return nil
}
unwrapped := value{
Descriptor: valEval.Descriptor,
NestedRule: valEval.NestedRule,
}
err := bldr.buildValue(fdesc.Message().Fields().ByName("value"), rules, &unwrapped, cache)
if err != nil {
return err
}
valEval.Append(unwrapped.Constraints)
return nil
}
func (bldr *builder) processStandardConstraints(
fdesc protoreflect.FieldDescriptor,
constraints *validate.FieldConstraints,
valEval *value,
_ messageCache,
) error {
stdConstraints, err := bldr.constraints.Build(
bldr.env,
fdesc,
constraints,
bldr.extensionTypeResolver,
bldr.allowUnknownFields,
valEval.NestedRule != nil,
)
if err != nil {
return err
}
valEval.Append(celPrograms{
base: newBase(valEval),
programSet: stdConstraints,
})
return nil
}
func (bldr *builder) processAnyConstraints(
fdesc protoreflect.FieldDescriptor,
fieldConstraints *validate.FieldConstraints,
valEval *value,
_ messageCache,
) error {
if (fdesc.IsList() && valEval.NestedRule == nil) ||
!isMessageField(fdesc) ||
fdesc.Message().FullName() != "google.protobuf.Any" {
return nil
}
typeURLDesc := fdesc.Message().Fields().ByName("type_url")
anyPbDesc := (&validate.AnyRules{}).ProtoReflect().Descriptor()
inField := anyPbDesc.Fields().ByName("in")
notInField := anyPbDesc.Fields().ByName("not_in")
anyEval := anyPB{
base: newBase(valEval),
TypeURLDescriptor: typeURLDesc,
In: stringsToSet(fieldConstraints.GetAny().GetIn()),
NotIn: stringsToSet(fieldConstraints.GetAny().GetNotIn()),
InValue: fieldConstraints.GetAny().ProtoReflect().Get(inField),
NotInValue: fieldConstraints.GetAny().ProtoReflect().Get(notInField),
}
valEval.Append(anyEval)
return nil
}
func (bldr *builder) processEnumConstraints(
fdesc protoreflect.FieldDescriptor,
fieldConstraints *validate.FieldConstraints,
valEval *value,
_ messageCache,
) error {
if fdesc.Kind() != protoreflect.EnumKind {
return nil
}
if fieldConstraints.GetEnum().GetDefinedOnly() {
valEval.Append(definedEnum{
base: newBase(valEval),
ValueDescriptors: fdesc.Enum().Values(),
})
}
return nil
}
func (bldr *builder) processMapConstraints(
fieldDesc protoreflect.FieldDescriptor,
constraints *validate.FieldConstraints,
valEval *value,
cache messageCache,
) error {
if !fieldDesc.IsMap() {
return nil
}
mapEval := newKVPairs(valEval)
err := bldr.buildValue(
fieldDesc.MapKey(),
constraints.GetMap().GetKeys(),
&mapEval.KeyConstraints,
cache)
if err != nil {
return &CompilationError{cause: fmt.Errorf(
"failed to compile key constraints for map %s: %w",
fieldDesc.FullName(), err)}
}
err = bldr.buildValue(
fieldDesc.MapValue(),
constraints.GetMap().GetValues(),
&mapEval.ValueConstraints,
cache)
if err != nil {
return &CompilationError{cause: fmt.Errorf(
"failed to compile value constraints for map %s: %w",
fieldDesc.FullName(), err)}
}
valEval.Append(mapEval)
return nil
}
func (bldr *builder) processRepeatedConstraints(
fdesc protoreflect.FieldDescriptor,
fieldConstraints *validate.FieldConstraints,
valEval *value,
cache messageCache,
) error {
if !fdesc.IsList() || valEval.NestedRule != nil {
return nil
}
listEval := newListItems(valEval)
err := bldr.buildValue(fdesc, fieldConstraints.GetRepeated().GetItems(), &listEval.ItemConstraints, cache)
if err != nil {
return &CompilationError{cause: fmt.Errorf(
"failed to compile items constraints for repeated %v: %w", fdesc.FullName(), err)}
}
valEval.Append(listEval)
return nil
}
func (bldr *builder) shouldSkip(constraints *validate.FieldConstraints) bool {
return constraints.GetSkipped() ||
constraints.GetIgnore() == validate.Ignore_IGNORE_ALWAYS
}
func (bldr *builder) shouldIgnoreEmpty(constraints *validate.FieldConstraints) bool {
return constraints.GetIgnoreEmpty() ||
constraints.GetIgnore() == validate.Ignore_IGNORE_IF_UNPOPULATED ||
constraints.GetIgnore() == validate.Ignore_IGNORE_IF_DEFAULT_VALUE
}
func (bldr *builder) shouldIgnoreDefault(constraints *validate.FieldConstraints) bool {
return constraints.GetIgnore() == validate.Ignore_IGNORE_IF_DEFAULT_VALUE
}
func (bldr *builder) zeroValue(fdesc protoreflect.FieldDescriptor, forItems bool) protoreflect.Value {
switch {
case forItems && fdesc.IsList():
msg := dynamicpb.NewMessage(fdesc.ContainingMessage())
return msg.Get(fdesc).List().NewElement()
case isMessageField(fdesc) &&
fdesc.Cardinality() != protoreflect.Repeated:
msg := dynamicpb.NewMessage(fdesc.Message())
return protoreflect.ValueOfMessage(msg)
default:
return fdesc.Default()
}
}
type messageCache map[protoreflect.MessageDescriptor]*message
func (c messageCache) Clone() messageCache {
newCache := make(messageCache, len(c)+1)
c.SyncTo(newCache)
return newCache
}
func (c messageCache) SyncTo(other messageCache) {
for k, v := range c {
other[k] = v
}
}
// isMessageField returns true if the field descriptor fdesc describes a field
// containing a submessage. Although they are represented differently on the
// wire, group fields are treated like message fields in protoreflect and have
// similar properties. In the 2023 edition of protobuf, message fields with the
// delimited encoding feature will be detected as groups, but should otherwise
// be treated the same.
func isMessageField(fdesc protoreflect.FieldDescriptor) bool {
return fdesc.Kind() == protoreflect.MessageKind ||
fdesc.Kind() == protoreflect.GroupKind
}