|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package dev.cel.extensions; |
| 16 | + |
| 17 | +import static com.google.common.base.Preconditions.checkArgument; |
| 18 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | + |
| 20 | +import com.google.common.collect.ImmutableList; |
| 21 | +import com.google.common.collect.ImmutableSet; |
| 22 | +import dev.cel.common.CelIssue; |
| 23 | +import dev.cel.common.ast.CelExpr; |
| 24 | +import dev.cel.compiler.CelCompilerLibrary; |
| 25 | +import dev.cel.parser.CelMacro; |
| 26 | +import dev.cel.parser.CelMacroExprFactory; |
| 27 | +import dev.cel.parser.CelParserBuilder; |
| 28 | +import dev.cel.parser.Operator; |
| 29 | +import java.util.Optional; |
| 30 | + |
| 31 | +/** Internal implementation of CEL comprehensions extensions. */ |
| 32 | +public final class CelComprehensionsExtensions implements CelCompilerLibrary { |
| 33 | + // TODO: Implement CelExtensionLibrary.FeatureSet interface. |
| 34 | + public ImmutableSet<CelMacro> macros() { |
| 35 | + return ImmutableSet.of( |
| 36 | + CelMacro.newReceiverMacro( |
| 37 | + Operator.ALL.getFunction(), 3, CelComprehensionsExtensions::expandAllMacro)); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void setParserOptions(CelParserBuilder parserBuilder) { |
| 42 | + parserBuilder.addMacros(macros()); |
| 43 | + } |
| 44 | + |
| 45 | + private static Optional<CelExpr> expandAllMacro( |
| 46 | + CelMacroExprFactory exprFactory, CelExpr target, ImmutableList<CelExpr> arguments) { |
| 47 | + checkNotNull(exprFactory); |
| 48 | + checkNotNull(target); |
| 49 | + checkArgument(arguments.size() == 3); |
| 50 | + CelExpr arg0 = checkNotNull(arguments.get(0)); |
| 51 | + if (arg0.exprKind().getKind() != CelExpr.ExprKind.Kind.IDENT) { |
| 52 | + return Optional.of(reportArgumentError(exprFactory, arg0)); |
| 53 | + } |
| 54 | + CelExpr arg1 = checkNotNull(arguments.get(1)); |
| 55 | + if (arg1.exprKind().getKind() != CelExpr.ExprKind.Kind.IDENT) { |
| 56 | + return Optional.of(reportArgumentError(exprFactory, arg1)); |
| 57 | + } |
| 58 | + CelExpr arg2 = checkNotNull(arguments.get(2)); |
| 59 | + CelExpr accuInit = exprFactory.newBoolLiteral(true); |
| 60 | + CelExpr condition = |
| 61 | + exprFactory.newGlobalCall( |
| 62 | + Operator.NOT_STRICTLY_FALSE.getFunction(), |
| 63 | + exprFactory.newIdentifier(exprFactory.getAccumulatorVarName())); |
| 64 | + CelExpr step = |
| 65 | + exprFactory.newGlobalCall( |
| 66 | + Operator.LOGICAL_AND.getFunction(), |
| 67 | + exprFactory.newIdentifier(exprFactory.getAccumulatorVarName()), |
| 68 | + arg2); |
| 69 | + CelExpr result = exprFactory.newIdentifier(exprFactory.getAccumulatorVarName()); |
| 70 | + return Optional.of( |
| 71 | + exprFactory.fold( |
| 72 | + arg0.ident().name(), |
| 73 | + arg1.ident().name(), |
| 74 | + target, |
| 75 | + exprFactory.getAccumulatorVarName(), |
| 76 | + accuInit, |
| 77 | + condition, |
| 78 | + step, |
| 79 | + result)); |
| 80 | + } |
| 81 | + |
| 82 | + private static CelExpr reportArgumentError(CelMacroExprFactory exprFactory, CelExpr argument) { |
| 83 | + return exprFactory.reportError( |
| 84 | + CelIssue.formatError( |
| 85 | + exprFactory.getSourceLocation(argument), "The argument must be a simple name")); |
| 86 | + } |
| 87 | +} |
0 commit comments