|
| 1 | +/* |
| 2 | + * Copyright 2026 The gRPC Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.grpc.xds.internal.matcher; |
| 18 | + |
| 19 | +import com.google.common.annotations.VisibleForTesting; |
| 20 | +import dev.cel.common.CelAbstractSyntaxTree; |
| 21 | +import dev.cel.common.CelValidationException; |
| 22 | +import dev.cel.common.types.SimpleType; |
| 23 | +import dev.cel.runtime.CelEvaluationException; |
| 24 | +import dev.cel.runtime.CelRuntime; |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +/** |
| 29 | + * Executes compiled CEL expressions. |
| 30 | + */ |
| 31 | +public final class CelMatcher { |
| 32 | + |
| 33 | + |
| 34 | + private final CelRuntime.Program program; |
| 35 | + |
| 36 | + private CelMatcher(CelRuntime.Program program) { |
| 37 | + this.program = program; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Compiles the AST into a CelMatcher. |
| 42 | + */ |
| 43 | + public static CelMatcher compile(CelAbstractSyntaxTree ast) |
| 44 | + throws CelValidationException, CelEvaluationException { |
| 45 | + if (ast.getResultType() != SimpleType.BOOL) { |
| 46 | + throw new IllegalArgumentException( |
| 47 | + "CEL expression must evaluate to boolean, got: " + ast.getResultType()); |
| 48 | + } |
| 49 | + CelCommon.checkAllowedVariables(ast); |
| 50 | + CelRuntime.Program program = CelCommon.RUNTIME.createProgram(ast); |
| 51 | + return new CelMatcher(program); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Compiles the CEL expression string into a CelMatcher. |
| 56 | + */ |
| 57 | + @VisibleForTesting |
| 58 | + public static CelMatcher compile(String expression) |
| 59 | + throws CelValidationException, CelEvaluationException { |
| 60 | + CelAbstractSyntaxTree ast = CelCommon.COMPILER.compile(expression).getAst(); |
| 61 | + return compile(ast); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Evaluates the CEL expression against the input activation. |
| 66 | + */ |
| 67 | + public boolean match(Object input) throws CelEvaluationException { |
| 68 | + Object result; |
| 69 | + if (input instanceof dev.cel.runtime.CelVariableResolver) { |
| 70 | + result = program.eval((dev.cel.runtime.CelVariableResolver) input); |
| 71 | + } else if (input instanceof java.util.Map) { |
| 72 | + @SuppressWarnings("unchecked") |
| 73 | + java.util.Map<String, ?> mapInput = (java.util.Map<String, ?>) input; |
| 74 | + result = program.eval(mapInput); |
| 75 | + } else { |
| 76 | + throw new CelEvaluationException( |
| 77 | + "Unsupported input type for CEL evaluation: " + input.getClass().getName()); |
| 78 | + } |
| 79 | + // Validated to be boolean during compile check ideally, or we cast safely |
| 80 | + if (result instanceof Boolean) { |
| 81 | + return (Boolean) result; |
| 82 | + } |
| 83 | + throw new CelEvaluationException( |
| 84 | + "CEL expression must evaluate to boolean, got: " + result.getClass().getName()); |
| 85 | + } |
| 86 | +} |
0 commit comments