-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathEvaluatorBuilder.java
More file actions
551 lines (509 loc) · 21.6 KB
/
EvaluatorBuilder.java
File metadata and controls
551 lines (509 loc) · 21.6 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
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
// 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 build.buf.protovalidate;
import build.buf.protovalidate.exceptions.CompilationException;
import build.buf.validate.FieldPath;
import build.buf.validate.FieldPathElement;
import build.buf.validate.FieldRules;
import build.buf.validate.Ignore;
import build.buf.validate.MessageOneofRule;
import build.buf.validate.MessageRules;
import build.buf.validate.OneofRules;
import build.buf.validate.Rule;
import com.google.protobuf.ByteString;
import com.google.protobuf.Descriptors;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.DynamicMessage;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Message;
import dev.cel.bundle.Cel;
import dev.cel.bundle.CelBuilder;
import dev.cel.common.types.StructTypeReference;
import dev.cel.runtime.CelEvaluationException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import org.jspecify.annotations.Nullable;
/** A build-through cache of message evaluators keyed off the provided descriptor. */
final class EvaluatorBuilder {
private static final FieldPathElement CEL_FIELD_PATH_ELEMENT =
FieldPathUtils.fieldPathElement(
FieldRules.getDescriptor().findFieldByNumber(FieldRules.CEL_FIELD_NUMBER));
private volatile Map<Descriptor, MessageEvaluator> evaluatorCache = Collections.emptyMap();
private final Cel cel;
private final boolean disableLazy;
private final RuleCache rules;
/**
* Constructs a new {@link EvaluatorBuilder}.
*
* @param cel The CEL environment for evaluation.
* @param config The configuration to use for the evaluation.
*/
EvaluatorBuilder(Cel cel, Config config) {
this.cel = cel;
this.disableLazy = false;
this.rules = new RuleCache(cel, config);
}
/**
* Constructs a new {@link EvaluatorBuilder}.
*
* @param cel The CEL environment for evaluation.
* @param config The configuration to use for the evaluation.
*/
EvaluatorBuilder(Cel cel, Config config, List<Descriptor> descriptors, boolean disableLazy)
throws CompilationException {
Objects.requireNonNull(descriptors, "descriptors must not be null");
this.cel = cel;
this.disableLazy = disableLazy;
this.rules = new RuleCache(cel, config);
for (Descriptor descriptor : descriptors) {
this.build(descriptor);
}
}
/**
* Returns a pre-cached {@link Evaluator} for the given descriptor or, if the descriptor is
* unknown, returns an evaluator that always throws a {@link CompilationException}.
*
* @param desc Protobuf descriptor type.
* @return An evaluator for the descriptor type.
* @throws CompilationException If an evaluator can't be created for the specified descriptor.
*/
Evaluator load(Descriptor desc) throws CompilationException {
Evaluator evaluator = evaluatorCache.get(desc);
if (evaluator == null && disableLazy) {
return new UnknownDescriptorEvaluator(desc);
}
return build(desc);
}
/**
* Either returns a memoized {@link Evaluator} for the given descriptor, or lazily constructs a
* new one.
*/
private Evaluator build(Descriptor desc) throws CompilationException {
Evaluator eval = evaluatorCache.get(desc);
if (eval != null) {
return eval;
}
synchronized (this) {
// Check again (we may have lost race with another thread which populated the map with this
// descriptor).
eval = evaluatorCache.get(desc);
if (eval != null) {
return eval;
}
// Rebuild cache with this descriptor (and any of its dependencies).
Map<Descriptor, MessageEvaluator> updatedCache =
new DescriptorCacheBuilder(cel, rules, evaluatorCache).build(desc);
evaluatorCache = updatedCache;
eval = updatedCache.get(desc);
if (eval == null) {
throw new IllegalStateException(
"updated cache missing evaluator for descriptor - should not happen");
}
}
return eval;
}
private static class DescriptorCacheBuilder {
private final RuleResolver resolver = new RuleResolver();
private final Cel cel;
private final RuleCache ruleCache;
private final HashMap<Descriptor, MessageEvaluator> cache;
private DescriptorCacheBuilder(
Cel cel, RuleCache ruleCache, Map<Descriptor, MessageEvaluator> previousCache) {
this.cel = Objects.requireNonNull(cel, "cel");
this.ruleCache = Objects.requireNonNull(ruleCache, "ruleCache");
this.cache = new HashMap<>(previousCache);
}
/**
* Creates an immutable cache containing the descriptor (and any other descriptors it
* references).
*
* @param descriptor Descriptor used to build the cache.
* @return Unmodifiable map of descriptors to evaluators.
* @throws CompilationException If an error occurs compiling a rule on the cache.
*/
Map<Descriptor, MessageEvaluator> build(Descriptor descriptor) throws CompilationException {
createMessageEvaluator(descriptor);
return Collections.unmodifiableMap(cache);
}
private MessageEvaluator createMessageEvaluator(Descriptor desc) throws CompilationException {
MessageEvaluator eval = cache.get(desc);
if (eval != null) {
return eval;
}
MessageEvaluator msgEval = new MessageEvaluator();
cache.put(desc, msgEval);
buildMessage(desc, msgEval);
return msgEval;
}
private void buildMessage(Descriptor desc, MessageEvaluator msgEval)
throws CompilationException {
try {
DynamicMessage defaultInstance = DynamicMessage.newBuilder(desc).buildPartial();
Descriptor descriptor = defaultInstance.getDescriptorForType();
MessageRules msgRules = resolver.resolveMessageRules(descriptor);
if (msgRules.getDisabled()) {
return;
}
processMessageExpressions(descriptor, msgRules, msgEval, defaultInstance);
processMessageOneofRules(descriptor, msgRules, msgEval);
processOneofRules(descriptor, msgEval);
processFields(descriptor, msgEval);
} catch (InvalidProtocolBufferException e) {
throw new CompilationException(
"failed to parse proto definition: " + desc.getFullName(), e);
}
}
private void processMessageExpressions(
Descriptor desc, MessageRules msgRules, MessageEvaluator msgEval, DynamicMessage message)
throws CompilationException {
List<Rule> celList = msgRules.getCelList();
if (celList.isEmpty()) {
return;
}
Cel finalCel =
cel.toCelBuilder()
.addMessageTypes(message.getDescriptorForType())
.addVar(Variable.THIS_NAME, StructTypeReference.create(desc.getFullName()))
.build();
List<CompiledProgram> compiledPrograms = compileRules(celList, finalCel, false);
if (compiledPrograms.isEmpty()) {
throw new CompilationException("compile returned null");
}
msgEval.append(new CelPrograms(null, compiledPrograms));
}
private void processMessageOneofRules(
Descriptor desc, MessageRules msgRules, MessageEvaluator msgEval)
throws CompilationException {
for (MessageOneofRule rule : msgRules.getOneofList()) {
if (rule.getFieldsCount() == 0) {
throw new CompilationException(
String.format(
"at least one field must be specified in oneof rule for the message %s",
desc.getFullName()));
}
Set<FieldDescriptor> fields = new LinkedHashSet<>(rule.getFieldsCount());
for (String name : rule.getFieldsList()) {
FieldDescriptor field = desc.findFieldByName(name);
if (field == null) {
throw new CompilationException(
String.format("field %s not found in %s", name, desc.getFullName()));
}
if (!fields.add(field)) {
throw new CompilationException(
String.format(
"duplicate %s in oneof rule for the message %s", name, desc.getFullName()));
}
}
msgEval.append(new MessageOneofEvaluator(new ArrayList<>(fields), rule.getRequired()));
}
}
private void processOneofRules(Descriptor desc, MessageEvaluator msgEval)
throws InvalidProtocolBufferException, CompilationException {
List<Descriptors.OneofDescriptor> oneofs = desc.getOneofs();
for (Descriptors.OneofDescriptor oneofDesc : oneofs) {
OneofRules oneofRules = resolver.resolveOneofRules(oneofDesc);
OneofEvaluator oneofEvaluatorEval = new OneofEvaluator(oneofDesc, oneofRules.getRequired());
msgEval.append(oneofEvaluatorEval);
}
}
private void processFields(Descriptor desc, MessageEvaluator msgEval)
throws CompilationException, InvalidProtocolBufferException {
List<FieldDescriptor> fields = desc.getFields();
for (FieldDescriptor fieldDescriptor : fields) {
FieldDescriptor descriptor = desc.findFieldByName(fieldDescriptor.getName());
FieldRules fieldRules = resolver.resolveFieldRules(descriptor);
FieldEvaluator fldEval = buildField(descriptor, fieldRules);
msgEval.append(fldEval);
}
}
private FieldEvaluator buildField(FieldDescriptor fieldDescriptor, FieldRules fieldRules)
throws CompilationException {
ValueEvaluator valueEvaluatorEval = new ValueEvaluator(fieldDescriptor, null);
boolean ignoreDefault = fieldDescriptor.hasPresence() && shouldIgnoreDefault(fieldRules);
Object zero = null;
if (ignoreDefault) {
zero = zeroValue(fieldDescriptor, false);
}
FieldEvaluator fieldEvaluator =
new FieldEvaluator(
valueEvaluatorEval,
fieldDescriptor,
fieldRules.getRequired(),
fieldDescriptor.hasPresence(),
fieldRules.getIgnore(),
zero);
buildValue(fieldDescriptor, fieldRules, fieldEvaluator.valueEvaluator);
return fieldEvaluator;
}
private static boolean shouldIgnoreEmpty(FieldRules rules) {
return rules.getIgnore() == Ignore.IGNORE_IF_UNPOPULATED
|| rules.getIgnore() == Ignore.IGNORE_IF_DEFAULT_VALUE;
}
private static boolean shouldIgnoreDefault(FieldRules rules) {
return rules.getIgnore() == Ignore.IGNORE_IF_DEFAULT_VALUE;
}
private void buildValue(
FieldDescriptor fieldDescriptor, FieldRules fieldRules, ValueEvaluator valueEvaluator)
throws CompilationException {
if (fieldRules.getIgnore() == Ignore.IGNORE_ALWAYS) {
return;
}
processIgnoreEmpty(fieldDescriptor, fieldRules, valueEvaluator);
processFieldExpressions(fieldDescriptor, fieldRules, valueEvaluator);
processEmbeddedMessage(fieldDescriptor, valueEvaluator);
processWrapperRules(fieldDescriptor, fieldRules, valueEvaluator);
processStandardRules(fieldDescriptor, fieldRules, valueEvaluator);
processAnyRules(fieldDescriptor, fieldRules, valueEvaluator);
processEnumRules(fieldDescriptor, fieldRules, valueEvaluator);
processMapRules(fieldDescriptor, fieldRules, valueEvaluator);
processRepeatedRules(fieldDescriptor, fieldRules, valueEvaluator);
}
private void processIgnoreEmpty(
FieldDescriptor fieldDescriptor, FieldRules fieldRules, ValueEvaluator valueEvaluatorEval)
throws CompilationException {
if (valueEvaluatorEval.hasNestedRule() && shouldIgnoreEmpty(fieldRules)) {
valueEvaluatorEval.setIgnoreEmpty(zeroValue(fieldDescriptor, true));
}
}
private Object zeroValue(FieldDescriptor fieldDescriptor, boolean forItems)
throws CompilationException {
final Object zero;
if (forItems && fieldDescriptor.isRepeated()) {
switch (fieldDescriptor.getType().getJavaType()) {
case INT:
case LONG:
zero = 0L;
break;
case FLOAT:
case DOUBLE:
zero = 0D;
break;
case BOOLEAN:
zero = false;
break;
case STRING:
zero = "";
break;
case BYTE_STRING:
zero = ByteString.EMPTY;
break;
case ENUM:
zero = (long) fieldDescriptor.getEnumType().getValues().get(0).getNumber();
break;
case MESSAGE:
zero = createMessageForType(fieldDescriptor.getMessageType());
break;
default:
zero = fieldDescriptor.getDefaultValue();
break;
}
} else if (fieldDescriptor.getJavaType() == FieldDescriptor.JavaType.MESSAGE
&& !fieldDescriptor.isRepeated()) {
zero = createMessageForType(fieldDescriptor.getMessageType());
} else {
zero =
ProtoAdapter.scalarToCel(fieldDescriptor.getType(), fieldDescriptor.getDefaultValue());
}
return zero;
}
private Message createMessageForType(Descriptor messageType) throws CompilationException {
try {
return DynamicMessage.parseFrom(messageType, new byte[0]);
} catch (InvalidProtocolBufferException e) {
throw new CompilationException("field descriptor type is invalid " + e.getMessage(), e);
}
}
private void processFieldExpressions(
FieldDescriptor fieldDescriptor, FieldRules fieldRules, ValueEvaluator valueEvaluatorEval)
throws CompilationException {
List<Rule> rulesCelList = fieldRules.getCelList();
if (rulesCelList.isEmpty()) {
return;
}
CelBuilder builder = cel.toCelBuilder();
builder =
builder.addVar(
Variable.THIS_NAME,
DescriptorMappings.getCELType(fieldDescriptor, valueEvaluatorEval.hasNestedRule()));
if (fieldDescriptor.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
builder = builder.addMessageTypes(fieldDescriptor.getMessageType());
}
Cel finalCel = builder.build();
List<CompiledProgram> compiledPrograms = compileRules(rulesCelList, finalCel, true);
if (!compiledPrograms.isEmpty()) {
valueEvaluatorEval.append(new CelPrograms(valueEvaluatorEval, compiledPrograms));
}
}
private void processEmbeddedMessage(
FieldDescriptor fieldDescriptor, ValueEvaluator valueEvaluatorEval)
throws CompilationException {
if (fieldDescriptor.getJavaType() != FieldDescriptor.JavaType.MESSAGE
|| fieldDescriptor.isMapField()
|| (fieldDescriptor.isRepeated() && !valueEvaluatorEval.hasNestedRule())) {
return;
}
Evaluator embedEval =
new EmbeddedMessageEvaluator(
valueEvaluatorEval, createMessageEvaluator(fieldDescriptor.getMessageType()));
valueEvaluatorEval.append(embedEval);
}
private void processWrapperRules(
FieldDescriptor fieldDescriptor, FieldRules fieldRules, ValueEvaluator valueEvaluatorEval)
throws CompilationException {
if (fieldDescriptor.getJavaType() != FieldDescriptor.JavaType.MESSAGE
|| fieldDescriptor.isMapField()
|| (fieldDescriptor.isRepeated() && !valueEvaluatorEval.hasNestedRule())) {
return;
}
FieldDescriptor expectedWrapperDescriptor =
DescriptorMappings.expectedWrapperRules(fieldDescriptor.getMessageType().getFullName());
// Verify that the expected wrapper rules for this field are equal to the rules specified on
// the field
if (expectedWrapperDescriptor != null) {
FieldDescriptor oneofFieldDescriptor =
fieldRules.getOneofFieldDescriptor(DescriptorMappings.FIELD_RULES_ONEOF_DESC);
// If there are no field rules set, just return
if (oneofFieldDescriptor == null) {
return;
}
if (!expectedWrapperDescriptor
.getMessageType()
.getFullName()
.equals(oneofFieldDescriptor.getMessageType().getFullName())) {
throw new CompilationException(
String.format(
"mismatched message rules, %s is not a valid rule for field %s",
oneofFieldDescriptor.getName(), fieldDescriptor.getName()));
}
}
if (expectedWrapperDescriptor == null || !fieldRules.hasField(expectedWrapperDescriptor)) {
return;
}
ValueEvaluator unwrapped =
new ValueEvaluator(
valueEvaluatorEval.getDescriptor(), valueEvaluatorEval.getNestedRule());
buildValue(fieldDescriptor.getMessageType().findFieldByName("value"), fieldRules, unwrapped);
valueEvaluatorEval.append(unwrapped);
}
private void processStandardRules(
FieldDescriptor fieldDescriptor, FieldRules fieldRules, ValueEvaluator valueEvaluatorEval)
throws CompilationException {
// If this is a wrapper field, just return. Wrapper fields are handled by
// processWrapperRules and their unwrapped values are passed through the process gauntlet.
if (fieldDescriptor.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
FieldDescriptor expectedWrapperDescriptor =
DescriptorMappings.expectedWrapperRules(fieldDescriptor.getMessageType().getFullName());
if (expectedWrapperDescriptor != null) {
return;
}
}
List<CompiledProgram> compile =
ruleCache.compile(fieldDescriptor, fieldRules, valueEvaluatorEval.hasNestedRule());
if (compile.isEmpty()) {
return;
}
valueEvaluatorEval.append(new CelPrograms(valueEvaluatorEval, compile));
}
private void processAnyRules(
FieldDescriptor fieldDescriptor, FieldRules fieldRules, ValueEvaluator valueEvaluatorEval) {
if ((fieldDescriptor.isRepeated() && !valueEvaluatorEval.hasNestedRule())
|| fieldDescriptor.getJavaType() != FieldDescriptor.JavaType.MESSAGE
|| !fieldDescriptor.getMessageType().getFullName().equals("google.protobuf.Any")) {
return;
}
FieldDescriptor typeURLDesc = fieldDescriptor.getMessageType().findFieldByName("type_url");
valueEvaluatorEval.append(
new AnyEvaluator(
valueEvaluatorEval,
typeURLDesc,
fieldRules.getAny().getInList(),
fieldRules.getAny().getNotInList()));
}
private void processEnumRules(
FieldDescriptor fieldDescriptor, FieldRules fieldRules, ValueEvaluator valueEvaluatorEval) {
if (fieldDescriptor.getJavaType() != FieldDescriptor.JavaType.ENUM) {
return;
}
if (fieldRules.getEnum().getDefinedOnly()) {
Descriptors.EnumDescriptor enumDescriptor = fieldDescriptor.getEnumType();
valueEvaluatorEval.append(
new EnumEvaluator(valueEvaluatorEval, enumDescriptor.getValues()));
}
}
private void processMapRules(
FieldDescriptor fieldDescriptor, FieldRules fieldRules, ValueEvaluator valueEvaluatorEval)
throws CompilationException {
if (!fieldDescriptor.isMapField()) {
return;
}
MapEvaluator mapEval = new MapEvaluator(valueEvaluatorEval, fieldDescriptor);
buildValue(
fieldDescriptor.getMessageType().findFieldByNumber(1),
fieldRules.getMap().getKeys(),
mapEval.getKeyEvaluator());
buildValue(
fieldDescriptor.getMessageType().findFieldByNumber(2),
fieldRules.getMap().getValues(),
mapEval.getValueEvaluator());
valueEvaluatorEval.append(mapEval);
}
private void processRepeatedRules(
FieldDescriptor fieldDescriptor, FieldRules fieldRules, ValueEvaluator valueEvaluatorEval)
throws CompilationException {
if (fieldDescriptor.isMapField()
|| !fieldDescriptor.isRepeated()
|| valueEvaluatorEval.hasNestedRule()) {
return;
}
ListEvaluator listEval = new ListEvaluator(valueEvaluatorEval);
buildValue(fieldDescriptor, fieldRules.getRepeated().getItems(), listEval.itemRules);
valueEvaluatorEval.append(listEval);
}
private static List<CompiledProgram> compileRules(List<Rule> rules, Cel cel, boolean isField)
throws CompilationException {
List<Expression> expressions = Expression.fromRules(rules);
List<CompiledProgram> compiledPrograms = new ArrayList<>();
for (int i = 0; i < expressions.size(); i++) {
Expression expression = expressions.get(i);
AstExpression astExpression = AstExpression.newAstExpression(cel, expression);
@Nullable FieldPath rulePath = null;
if (isField) {
rulePath =
FieldPath.newBuilder()
.addElements(CEL_FIELD_PATH_ELEMENT.toBuilder().setIndex(i))
.build();
}
try {
compiledPrograms.add(
new CompiledProgram(
cel.createProgram(astExpression.ast),
astExpression.source,
rulePath,
new MessageValue(rules.get(i)),
null));
} catch (CelEvaluationException e) {
throw new CompilationException("failed to evaluate rule " + rules.get(i).getId(), e);
}
}
return compiledPrograms;
}
}
}