Skip to content

Commit

Permalink
Migrate from proto-based expr types to native CelExpr types in Parser
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 532671493
  • Loading branch information
l46kok authored and copybara-github committed Jun 23, 2023
1 parent ed8f346 commit 7eed156
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 121 deletions.
43 changes: 42 additions & 1 deletion common/src/main/java/dev/cel/common/CelAbstractSyntaxTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import dev.cel.expr.CheckedExpr;
import dev.cel.expr.Expr;
import dev.cel.expr.ParsedExpr;
import dev.cel.expr.SourceInfo;
import dev.cel.expr.Type;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -52,12 +53,46 @@ public final class CelAbstractSyntaxTree {

private final ImmutableMap<Long, CelType> types;

CelAbstractSyntaxTree(CelExpr celExpr, CelSource celSource) {
this(celExpr, celSource, ImmutableMap.of(), ImmutableMap.of());
}

CelAbstractSyntaxTree(
CelExpr celExpr,
CelSource celSource,
ImmutableMap<Long, CelReference> references,
ImmutableMap<Long, CelType> types) {
this.checkedExpr = null;
// TODO: This exists only for compatibility reason. Move this logic into
// CelProtoAbstractSyntaxTree after the native type migration is complete.
CheckedExpr.Builder checkedExprBuilder =
CheckedExpr.newBuilder()
.setSourceInfo(
SourceInfo.newBuilder()
.setLocation(celSource.getDescription())
.addAllLineOffsets(celSource.getLineOffsets())
.putAllMacroCalls(
celSource.getMacroCalls().entrySet().stream()
.collect(
toImmutableMap(
Entry::getKey,
v -> CelExprConverter.fromCelExpr(v.getValue()))))
.putAllPositions(celSource.getPositionsMap()))
.setExpr(CelExprConverter.fromCelExpr(celExpr));

if (!types.isEmpty()) {
// This is a type-checked AST
checkedExprBuilder.putAllReferenceMap(
references.entrySet().stream()
.collect(
toImmutableMap(
Entry::getKey,
v -> CelExprConverter.celReferenceToExprReference(v.getValue()))));
checkedExprBuilder.putAllTypeMap(
types.entrySet().stream()
.collect(toImmutableMap(Entry::getKey, v -> CelTypes.celTypeToType(v.getValue()))));
}

this.checkedExpr = checkedExprBuilder.build();
this.celExpr = celExpr;
this.celSource = celSource;
this.references = references;
Expand Down Expand Up @@ -199,6 +234,9 @@ public static CelAbstractSyntaxTree fromCheckedExpr(CheckedExpr checkedExpr) {
.addAllLineOffsets(checkedExpr.getSourceInfo().getLineOffsetsList())
.addPositionsMap(checkedExpr.getSourceInfo().getPositionsMap())
.setDescription(checkedExpr.getSourceInfo().getLocation())
.addAllMacroCalls(
CelExprConverter.exprMacroCallsToCelExprMacroCalls(
checkedExpr.getSourceInfo().getMacroCallsMap()))
.build());
}

Expand All @@ -214,6 +252,9 @@ public static CelAbstractSyntaxTree fromParsedExpr(ParsedExpr parsedExpr) {
CelSource.newBuilder()
.addAllLineOffsets(parsedExpr.getSourceInfo().getLineOffsetsList())
.addPositionsMap(parsedExpr.getSourceInfo().getPositionsMap())
.addAllMacroCalls(
CelExprConverter.exprMacroCallsToCelExprMacroCalls(
parsedExpr.getSourceInfo().getMacroCallsMap()))
.setDescription(parsedExpr.getSourceInfo().getLocation())
.build());
}
Expand Down
34 changes: 13 additions & 21 deletions common/src/main/java/dev/cel/common/CelProtoAbstractSyntaxTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ private CelProtoAbstractSyntaxTree(CheckedExpr checkedExpr) {
CelSource.newBuilder()
.addAllLineOffsets(checkedExpr.getSourceInfo().getLineOffsetsList())
.addPositionsMap(checkedExpr.getSourceInfo().getPositionsMap())
.addAllMacroCalls(
CelExprConverter.exprMacroCallsToCelExprMacroCalls(
checkedExpr.getSourceInfo().getMacroCallsMap()))
.setDescription(checkedExpr.getSourceInfo().getLocation())
.build(),
checkedExpr.getReferenceMapMap().entrySet().stream()
Expand All @@ -59,29 +62,18 @@ private CelProtoAbstractSyntaxTree(CheckedExpr checkedExpr) {

private CelProtoAbstractSyntaxTree(CelAbstractSyntaxTree ast) {
this.ast = ast;

CheckedExpr.Builder checkedExprBuilder =
CheckedExpr.newBuilder()
.setSourceInfo(
SourceInfo.newBuilder()
.setLocation(ast.getSource().getDescription())
.addAllLineOffsets(ast.getSource().getLineOffsets().asList())
.putAllPositions(ast.getSource().getPositionsMap()))
.setExpr(CelExprConverter.fromCelExpr(ast.getExpr()));

// TODO: The logic of converting a native CEL AST to Checked expression should be
// moved from CelAbstractSyntaxTree's constructor to here.
if (ast.isChecked()) {
checkedExprBuilder.putAllReferenceMap(
ast.getReferenceMap().entrySet().stream()
.collect(
toImmutableMap(
Entry::getKey,
v -> CelExprConverter.celReferenceToExprReference(v.getValue()))));
checkedExprBuilder.putAllTypeMap(
ast.getTypeMap().entrySet().stream()
.collect(toImmutableMap(Entry::getKey, v -> CelTypes.celTypeToType(v.getValue()))));
this.checkedExpr = ast.toCheckedExpr();
} else {
ParsedExpr parsedExpr = ast.toParsedExpr();
this.checkedExpr =
CheckedExpr.newBuilder()
.setExpr(parsedExpr.getExpr())
.setSourceInfo(parsedExpr.getSourceInfo())
.build();
}

this.checkedExpr = checkedExprBuilder.build();
}

/** Construct an abstract syntax tree from a {@link com.google.api.expr.CheckedExpr}. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ private CelProtoV1Alpha1AbstractSyntaxTree(CheckedExpr checkedExpr) {
CelSource.newBuilder()
.addAllLineOffsets(checkedExpr.getSourceInfo().getLineOffsetsList())
.addPositionsMap(checkedExpr.getSourceInfo().getPositionsMap())
.addAllMacroCalls(
CelExprV1Alpha1Converter.exprMacroCallsToCelExprMacroCalls(
checkedExpr.getSourceInfo().getMacroCallsMap()))
.setDescription(checkedExpr.getSourceInfo().getLocation())
.build(),
checkedExpr.getReferenceMapMap().entrySet().stream()
Expand All @@ -70,7 +73,13 @@ private CelProtoV1Alpha1AbstractSyntaxTree(CelAbstractSyntaxTree ast) {
.setSourceInfo(
SourceInfo.newBuilder()
.setLocation(ast.getSource().getDescription())
.addAllLineOffsets(ast.getSource().getLineOffsets().asList())
.addAllLineOffsets(ast.getSource().getLineOffsets())
.putAllMacroCalls(
ast.getSource().getMacroCalls().entrySet().stream()
.collect(
toImmutableMap(
Entry::getKey,
v -> CelExprV1Alpha1Converter.fromCelExpr(v.getValue()))))
.putAllPositions(ast.getSource().getPositionsMap()))
.setExpr(CelExprV1Alpha1Converter.fromCelExpr(ast.getExpr()));

Expand Down
Loading

0 comments on commit 7eed156

Please sign in to comment.