|
| 1 | +// Copyright 2022 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.common; |
| 16 | + |
| 17 | +import static com.google.common.base.Preconditions.checkState; |
| 18 | +import static com.google.common.collect.ImmutableMap.toImmutableMap; |
| 19 | + |
| 20 | +import dev.cel.expr.CheckedExpr; |
| 21 | +import dev.cel.expr.Expr; |
| 22 | +import dev.cel.expr.ParsedExpr; |
| 23 | +import dev.cel.expr.SourceInfo; |
| 24 | +import dev.cel.expr.Type; |
| 25 | +import com.google.errorprone.annotations.CheckReturnValue; |
| 26 | +import dev.cel.common.ast.CelExprConverter; |
| 27 | +import dev.cel.common.types.CelTypes; |
| 28 | +import java.util.Map.Entry; |
| 29 | + |
| 30 | +/** |
| 31 | + * An Adapter for {@link CelAbstractSyntaxTree} constructed from Canonical Protos of {@link |
| 32 | + * CheckedExpr} or {@link ParsedExpr} expressions. |
| 33 | + * |
| 34 | + * <p>Note: Keep this file in sync with {@link CelProtoV1Alpha1AbstractSyntaxTree} |
| 35 | + */ |
| 36 | +// LINT.IfChange |
| 37 | +public final class CelProtoAbstractSyntaxTree { |
| 38 | + private final CheckedExpr checkedExpr; |
| 39 | + private final CelAbstractSyntaxTree ast; |
| 40 | + |
| 41 | + private CelProtoAbstractSyntaxTree(CheckedExpr checkedExpr) { |
| 42 | + this.checkedExpr = checkedExpr; |
| 43 | + this.ast = |
| 44 | + new CelAbstractSyntaxTree( |
| 45 | + CelExprConverter.fromExpr(checkedExpr.getExpr()), |
| 46 | + CelSource.newBuilder() |
| 47 | + .addAllLineOffsets(checkedExpr.getSourceInfo().getLineOffsetsList()) |
| 48 | + .addPositionsMap(checkedExpr.getSourceInfo().getPositionsMap()) |
| 49 | + .setDescription(checkedExpr.getSourceInfo().getLocation()) |
| 50 | + .build(), |
| 51 | + checkedExpr.getReferenceMapMap().entrySet().stream() |
| 52 | + .collect( |
| 53 | + toImmutableMap( |
| 54 | + Entry::getKey, |
| 55 | + v -> CelExprConverter.exprReferenceToCelReference(v.getValue()))), |
| 56 | + checkedExpr.getTypeMapMap().entrySet().stream() |
| 57 | + .collect(toImmutableMap(Entry::getKey, v -> CelTypes.typeToCelType(v.getValue())))); |
| 58 | + } |
| 59 | + |
| 60 | + /** Construct an abstract syntax tree from a {@link com.google.api.expr.CheckedExpr}. */ |
| 61 | + public static CelProtoAbstractSyntaxTree fromCheckedExpr(CheckedExpr checkedExpr) { |
| 62 | + return new CelProtoAbstractSyntaxTree(checkedExpr); |
| 63 | + } |
| 64 | + |
| 65 | + /** Construct an abstract syntax tree from a {@link com.google.api.expr.ParsedExpr}. */ |
| 66 | + public static CelProtoAbstractSyntaxTree fromParsedExpr(ParsedExpr parsedExpr) { |
| 67 | + return new CelProtoAbstractSyntaxTree( |
| 68 | + CheckedExpr.newBuilder() |
| 69 | + .setExpr(parsedExpr.getExpr()) |
| 70 | + .setSourceInfo(parsedExpr.getSourceInfo()) |
| 71 | + .build()); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Returns the native representation of the abstract syntax tree: {@link CelAbstractSyntaxTree}. |
| 76 | + */ |
| 77 | + @CheckReturnValue |
| 78 | + public CelAbstractSyntaxTree getAst() { |
| 79 | + return ast; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Returns the underlying {@link com.google.api.expr.Expr} representation of the abstract syntax |
| 84 | + * tree. |
| 85 | + */ |
| 86 | + @CheckReturnValue |
| 87 | + public Expr getExpr() { |
| 88 | + return checkedExpr.getExpr(); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Returns the underlying {@link com.google.api.expr.CheckedExpr} representation of the abstract |
| 93 | + * syntax tree. Throws {@link java.lang.IllegalStateException} if {@link |
| 94 | + * CelAbstractSyntaxTree#isChecked} is false. |
| 95 | + */ |
| 96 | + @CheckReturnValue |
| 97 | + public CheckedExpr toCheckedExpr() { |
| 98 | + checkState( |
| 99 | + ast.isChecked(), |
| 100 | + "CelAbstractSyntaxTree must be checked before it can be converted to CheckedExpr"); |
| 101 | + return checkedExpr; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Returns the underlying {@link com.google.api.expr.SourceInfo} representation of the abstract |
| 106 | + * syntax tree. |
| 107 | + */ |
| 108 | + @CheckReturnValue |
| 109 | + public SourceInfo getSourceInfo() { |
| 110 | + return checkedExpr.getSourceInfo(); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Returns the underlying {@link com.google.api.expr.ParsedExpr} representation of the abstract |
| 115 | + * syntax tree. |
| 116 | + */ |
| 117 | + @CheckReturnValue |
| 118 | + public ParsedExpr toParsedExpr() { |
| 119 | + return ParsedExpr.newBuilder().setExpr(getExpr()).setSourceInfo(getSourceInfo()).build(); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * For a type checked abstract syntax tree the resulting type is returned in proto format |
| 124 | + * described in checked.proto. Otherwise, the dynamic type is returned. |
| 125 | + */ |
| 126 | + @CheckReturnValue |
| 127 | + public Type getProtoResultType() { |
| 128 | + return CelTypes.celTypeToType(ast.getResultType()); |
| 129 | + } |
| 130 | +} |
| 131 | +// LINT.ThenChange(CelProtoV1Alpha1AbstractSyntaxTree.java) |
0 commit comments