Skip to content

Commit d13af57

Browse files
committed
Use the CEL planner runtime
Wire the compiler and runtime separately via CelCompilerImpl + CelRuntimeImpl combined through CelFactory.combine, instead of CelFactory.standardCelBuilder. plannerCelBuilder rejects setStandardEnvironmentEnabled(false) on the runtime side, which ValidateLibrary needs to drop stdlib matches in favor of CustomOverload's caching replacement (cel-java#1038), so the two sides are configured independently and combined. CelRuntimeImpl requires enableHeterogeneousNumericComparisons(true); the rest of the options stay at current() defaults. Pin cel to 0.13.1.
1 parent a56c3bc commit d13af57

2 files changed

Lines changed: 35 additions & 18 deletions

File tree

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[versions]
22
assertj = "3.27.7"
33
buf = "1.70.0"
4-
cel = "0.13.0"
4+
cel = "0.13.1"
55
error-prone = "2.49.0"
66
junit = "5.14.4"
77
maven-publish = "0.36.0"

src/main/java/build/buf/protovalidate/ValidateLibrary.java

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,21 @@
1818
import dev.cel.bundle.Cel;
1919
import dev.cel.bundle.CelFactory;
2020
import dev.cel.checker.CelCheckerBuilder;
21+
import dev.cel.checker.CelCheckerLegacyImpl;
2122
import dev.cel.checker.CelStandardDeclarations;
2223
import dev.cel.common.CelOptions;
2324
import dev.cel.common.CelVarDecl;
2425
import dev.cel.common.types.SimpleType;
26+
import dev.cel.compiler.CelCompiler;
27+
import dev.cel.compiler.CelCompilerImpl;
2528
import dev.cel.compiler.CelCompilerLibrary;
2629
import dev.cel.extensions.CelExtensions;
2730
import dev.cel.parser.CelParserBuilder;
31+
import dev.cel.parser.CelParserImpl;
2832
import dev.cel.parser.CelStandardMacro;
33+
import dev.cel.runtime.CelRuntime;
2934
import dev.cel.runtime.CelRuntimeBuilder;
35+
import dev.cel.runtime.CelRuntimeImpl;
3036
import dev.cel.runtime.CelRuntimeLibrary;
3137
import dev.cel.runtime.CelStandardFunctions;
3238
import java.util.concurrent.ConcurrentHashMap;
@@ -38,7 +44,8 @@
3844
*/
3945
final class ValidateLibrary implements CelCompilerLibrary, CelRuntimeLibrary {
4046

41-
private static final CelOptions CEL_OPTIONS = CelOptions.DEFAULT;
47+
private static final CelOptions CEL_OPTIONS =
48+
CelOptions.current().enableHeterogeneousNumericComparisons(true).build();
4249

4350
private final ConcurrentMap<String, Pattern> patternCache = new ConcurrentHashMap<>();
4451

@@ -47,22 +54,32 @@ final class ValidateLibrary implements CelCompilerLibrary, CelRuntimeLibrary {
4754

4855
static Cel newCel() {
4956
ValidateLibrary validateLibrary = new ValidateLibrary();
50-
return CelFactory.standardCelBuilder()
51-
.setOptions(CEL_OPTIONS)
52-
// Drop stdlib matches; CustomOverload provides a caching replacement.
53-
// Ref: https://github.com/google/cel-java/issues/1038
54-
.setStandardEnvironmentEnabled(false)
55-
.setStandardDeclarations(
56-
CelStandardDeclarations.newBuilder()
57-
.excludeFunctions(CelStandardDeclarations.StandardFunction.MATCHES)
58-
.build())
59-
.setStandardFunctions(
60-
CelStandardFunctions.newBuilder()
61-
.excludeFunctions(CelStandardFunctions.StandardFunction.MATCHES)
62-
.build())
63-
.addCompilerLibraries(validateLibrary, CelExtensions.strings())
64-
.addRuntimeLibraries(validateLibrary, CelExtensions.strings())
65-
.build();
57+
// Wired by hand instead of via plannerCelBuilder(): CelRuntimeImpl directs callers to subset
58+
// stdlib via setStandardFunctions rather than setStandardEnvironmentEnabled, so the runtime
59+
// does that while the checker uses setStandardEnvironmentEnabled(false).
60+
CelCompiler compiler =
61+
CelCompilerImpl.newBuilder(
62+
CelParserImpl.newBuilder(),
63+
CelCheckerLegacyImpl.newBuilder().setStandardEnvironmentEnabled(false))
64+
.setOptions(CEL_OPTIONS)
65+
// Drop stdlib matches; CustomOverload provides a caching replacement.
66+
// Ref: https://github.com/google/cel-java/issues/1038
67+
.setStandardDeclarations(
68+
CelStandardDeclarations.newBuilder()
69+
.excludeFunctions(CelStandardDeclarations.StandardFunction.MATCHES)
70+
.build())
71+
.addLibraries(validateLibrary, CelExtensions.strings())
72+
.build();
73+
CelRuntime runtime =
74+
CelRuntimeImpl.newBuilder()
75+
.setOptions(CEL_OPTIONS)
76+
.setStandardFunctions(
77+
CelStandardFunctions.newBuilder()
78+
.excludeFunctions(CelStandardFunctions.StandardFunction.MATCHES)
79+
.build())
80+
.addLibraries(validateLibrary, CelExtensions.strings())
81+
.build();
82+
return CelFactory.combine(compiler, runtime);
6683
}
6784

6885
@Override

0 commit comments

Comments
 (0)