Repository navigation
Expand file tree
/
Copy pathValidateLibrary.java
More file actions
109 lines (99 loc) · 4.3 KB
/
Copy pathValidateLibrary.java
File metadata and controls
109 lines (99 loc) · 4.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
// Copyright 2023-2026 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 com.google.re2j.Pattern;
import dev.cel.bundle.Cel;
import dev.cel.bundle.CelFactory;
import dev.cel.checker.CelCheckerBuilder;
import dev.cel.checker.CelCheckerLegacyImpl;
import dev.cel.checker.CelStandardDeclarations;
import dev.cel.common.CelOptions;
import dev.cel.common.CelVarDecl;
import dev.cel.common.types.SimpleType;
import dev.cel.compiler.CelCompiler;
import dev.cel.compiler.CelCompilerImpl;
import dev.cel.compiler.CelCompilerLibrary;
import dev.cel.extensions.CelExtensions;
import dev.cel.parser.CelParserBuilder;
import dev.cel.parser.CelParserImpl;
import dev.cel.parser.CelStandardMacro;
import dev.cel.runtime.CelRuntime;
import dev.cel.runtime.CelRuntimeBuilder;
import dev.cel.runtime.CelRuntimeFactory;
import dev.cel.runtime.CelRuntimeLibrary;
import dev.cel.runtime.CelStandardFunctions;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* Custom {@link CelCompilerLibrary} and {@link CelRuntimeLibrary}. Provides all the custom
* extension function definitions and overloads.
*/
final class ValidateLibrary implements CelCompilerLibrary, CelRuntimeLibrary {
private static final CelOptions CEL_OPTIONS =
CelOptions.current().enableHeterogeneousNumericComparisons(true).build();
private final ConcurrentMap<String, Pattern> patternCache = new ConcurrentHashMap<>();
/** Creates a ValidateLibrary with all custom declarations and overloads. */
ValidateLibrary() {}
static Cel newCel() {
ValidateLibrary validateLibrary = new ValidateLibrary();
// Wired by hand instead of via plannerCelBuilder(): CelRuntimeImpl directs callers to subset
// stdlib via setStandardFunctions rather than setStandardEnvironmentEnabled, so the runtime
// does that while the checker uses setStandardEnvironmentEnabled(false).
CelCompiler compiler =
CelCompilerImpl.newBuilder(
CelParserImpl.newBuilder(),
CelCheckerLegacyImpl.newBuilder().setStandardEnvironmentEnabled(false))
.setOptions(CEL_OPTIONS)
// Drop stdlib matches; CustomOverload provides a caching replacement.
// Ref: https://github.com/google/cel-java/issues/1038
.setStandardDeclarations(
CelStandardDeclarations.newBuilder()
.excludeFunctions(CelStandardDeclarations.StandardFunction.MATCHES)
.build())
.addLibraries(validateLibrary, CelExtensions.strings())
.build();
CelRuntime runtime =
CelRuntimeFactory.plannerRuntimeBuilder()
.setOptions(CEL_OPTIONS)
.setStandardFunctions(
CelStandardFunctions.newBuilder()
.excludeFunctions(CelStandardFunctions.StandardFunction.MATCHES)
.build())
.addLibraries(validateLibrary, CelExtensions.strings())
.build();
return CelFactory.combine(compiler, runtime);
}
@Override
public void setParserOptions(CelParserBuilder parserBuilder) {
parserBuilder.setStandardMacros(
CelStandardMacro.ALL,
CelStandardMacro.EXISTS,
CelStandardMacro.EXISTS_ONE,
CelStandardMacro.FILTER,
CelStandardMacro.HAS,
CelStandardMacro.MAP,
CelStandardMacro.MAP_FILTER);
}
@Override
public void setCheckerOptions(CelCheckerBuilder checkerBuilder) {
checkerBuilder
.addVarDeclarations(
CelVarDecl.newVarDeclaration(NowVariable.NOW_NAME, SimpleType.TIMESTAMP))
.addFunctionDeclarations(CustomDeclarations.create());
}
@Override
public void setRuntimeOptions(CelRuntimeBuilder runtimeBuilder) {
runtimeBuilder.addFunctionBindings(CustomOverload.create(patternCache, CEL_OPTIONS));
}
}