Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions src/main/java/build/buf/protovalidate/RuleCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import dev.cel.bundle.Cel;
import dev.cel.common.types.StructTypeReference;
import dev.cel.runtime.CelEvaluationException;
import dev.cel.runtime.CelRuntime.Program;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -41,11 +42,14 @@
final class RuleCache {
private static class CelRule {
final AstExpression astExpression;
final Program program;
final FieldDescriptor field;
final FieldPath rulePath;

private CelRule(AstExpression astExpression, FieldDescriptor field, FieldPath rulePath) {
private CelRule(
AstExpression astExpression, Program program, FieldDescriptor field, FieldPath rulePath) {
this.astExpression = astExpression;
this.program = program;
this.field = field;
this.rulePath = rulePath;
}
Expand Down Expand Up @@ -119,20 +123,14 @@ List<CompiledProgram> compile(
}
List<CompiledProgram> programs = new ArrayList<>();
for (CelRule rule : completeProgramList) {
Cel ruleCel = getRuleCel(fieldDescriptor, message, rule.field, forItems);
try {
programs.add(
new CompiledProgram(
ruleCel.createProgram(rule.astExpression.ast),
rule.astExpression.source,
rule.rulePath,
new ObjectValue(rule.field, message.getField(rule.field)),
Variable.newRuleVariable(
message, ProtoAdapter.toCel(rule.field, message.getField(rule.field)))));
} catch (CelEvaluationException e) {
throw new CompilationException(
"failed to evaluate rule " + rule.astExpression.source.id, e);
}
programs.add(
new CompiledProgram(
rule.program,
rule.astExpression.source,
rule.rulePath,
new ObjectValue(rule.field, message.getField(rule.field)),
Variable.newRuleVariable(
message, ProtoAdapter.toCel(rule.field, message.getField(rule.field)))));
}
return Collections.unmodifiableList(programs);
}
Expand All @@ -159,9 +157,15 @@ List<CompiledProgram> compile(
.addElements(FieldPathUtils.fieldPathElement(setOneof))
.addElements(FieldPathUtils.fieldPathElement(ruleFieldDesc))
.build();
celRules.add(
new CelRule(
AstExpression.newAstExpression(ruleCel, expression), ruleFieldDesc, rulePath));
AstExpression astExpression = AstExpression.newAstExpression(ruleCel, expression);
Program program;
try {
program = ruleCel.createProgram(astExpression.ast);
} catch (CelEvaluationException e) {
throw new CompilationException(
"failed to create program for rule " + astExpression.source.id, e);
}
celRules.add(new CelRule(astExpression, program, ruleFieldDesc, rulePath));
}
descriptorMap.put(ruleFieldDesc, celRules);
return celRules;
Expand Down
Loading