From a7beb7a722b89e72895deea31fc42925b15b5ef0 Mon Sep 17 00:00:00 2001 From: Sokwhan Huh Date: Wed, 20 Sep 2023 17:52:48 -0700 Subject: [PATCH] No public description PiperOrigin-RevId: 567132204 --- bundle/src/test/java/dev/cel/bundle/CelImplTest.java | 9 +++++---- .../test/java/dev/cel/checker/ExprCheckerTest.java | 12 +++++++++--- .../java/dev/cel/runtime/CelAttributeParser.java | 4 +++- .../java/dev/cel/testing/BaseInterpreterTest.java | 4 ++-- .../src/test/java/dev/cel/testing/EvalSyncTest.java | 11 +++++++++-- 5 files changed, 28 insertions(+), 12 deletions(-) diff --git a/bundle/src/test/java/dev/cel/bundle/CelImplTest.java b/bundle/src/test/java/dev/cel/bundle/CelImplTest.java index 88a768d08..43e515a16 100644 --- a/bundle/src/test/java/dev/cel/bundle/CelImplTest.java +++ b/bundle/src/test/java/dev/cel/bundle/CelImplTest.java @@ -56,6 +56,7 @@ import dev.cel.common.CelAbstractSyntaxTree; import dev.cel.common.CelIssue; import dev.cel.common.CelOptions; +import dev.cel.common.CelProtoAbstractSyntaxTree; import dev.cel.common.CelValidationException; import dev.cel.common.CelValidationResult; import dev.cel.common.CelVarDecl; @@ -1053,7 +1054,7 @@ public void setOptions() throws Exception { Cel cel = standardCelBuilderWithMacros().build(); CelValidationResult result = cel.parse("!!!true"); assertThat(result.hasError()).isFalse(); - assertThat(result.getAst().toParsedExpr().getExpr()) + assertThat(CelProtoAbstractSyntaxTree.fromCelAst(result.getAst()).toParsedExpr().getExpr()) .ignoringFieldDescriptors(Expr.getDescriptor().findFieldByName("id")) .isEqualTo(NOT_EXPR); @@ -1063,7 +1064,7 @@ public void setOptions() throws Exception { .build(); result = cel.parse("!!!true"); assertThat(result.hasError()).isFalse(); - assertThat(result.getAst().toParsedExpr().getExpr()) + assertThat(CelProtoAbstractSyntaxTree.fromCelAst(result.getAst()).toParsedExpr().getExpr()) .ignoringFieldDescriptors(Expr.getDescriptor().findFieldByName("id")) .isEqualTo(NOT_NOT_NOT_EXPR); } @@ -1078,7 +1079,7 @@ private void assertValidationResult(CelValidationResult result, ParsedExpr parse throws Exception { assertThat(result.hasError()).isFalse(); assertThat(result.getAllIssues()).isEmpty(); - assertThat(result.getAst().toParsedExpr()) + assertThat(CelProtoAbstractSyntaxTree.fromCelAst(result.getAst()).toParsedExpr()) .ignoringFieldDescriptors( Expr.getDescriptor().findFieldByName("id"), ParsedExpr.getDescriptor().findFieldByName("source_info")) @@ -1089,7 +1090,7 @@ private void assertValidationResult(CelValidationResult result, ParsedExpr parse private void assertValidationResult(CelValidationResult result, CheckedExpr checkedExpr) throws Exception { assertThat(result.hasError()).isFalse(); - assertThat(result.getAst().toCheckedExpr()) + assertThat(CelProtoAbstractSyntaxTree.fromCelAst(result.getAst()).toCheckedExpr()) .ignoringFieldDescriptors( Expr.getDescriptor().findFieldByName("id"), CheckedExpr.getDescriptor().findFieldByName("source_info")) diff --git a/checker/src/test/java/dev/cel/checker/ExprCheckerTest.java b/checker/src/test/java/dev/cel/checker/ExprCheckerTest.java index 560d43941..461eb3574 100644 --- a/checker/src/test/java/dev/cel/checker/ExprCheckerTest.java +++ b/checker/src/test/java/dev/cel/checker/ExprCheckerTest.java @@ -37,6 +37,7 @@ import com.google.protobuf.DescriptorProtos.FileDescriptorSet; // import com.google.testing.testsize.MediumTest; import dev.cel.common.CelAbstractSyntaxTree; +import dev.cel.common.CelProtoAbstractSyntaxTree; import dev.cel.common.internal.EnvVisitable; import dev.cel.common.internal.Errors; import dev.cel.common.types.CelTypes; @@ -78,7 +79,9 @@ private void runTest() throws Exception { testOutput() .println( CelDebug.toAdornedDebugString( - ast.getProtoExpr(), new CheckedExprAdorner(ast.toCheckedExpr()))); + CelProtoAbstractSyntaxTree.fromCelAst(ast).getExpr(), + new CheckedExprAdorner( + CelProtoAbstractSyntaxTree.fromCelAst(ast).toCheckedExpr()))); } testOutput().println(); } @@ -699,7 +702,9 @@ public void globalStandaloneEnumValues() throws Exception { testOutput() .println( CelDebug.toAdornedDebugString( - ast.getProtoExpr(), new CheckedExprAdorner(ast.toCheckedExpr()))); + CelProtoAbstractSyntaxTree.fromCelAst(ast).getExpr(), + new CheckedExprAdorner( + CelProtoAbstractSyntaxTree.fromCelAst(ast).toCheckedExpr()))); } } @@ -949,7 +954,8 @@ public void optionalErrors() throws Exception { source = "a.?b"; declareVariable("a", createMap(CelTypes.STRING, CelTypes.STRING)); prepareCompiler(new ProtoMessageTypeProvider()); - ParsedExpr parsedExpr = celCompiler.parse(source).getAst().toParsedExpr(); + ParsedExpr parsedExpr = + CelProtoAbstractSyntaxTree.fromCelAst(celCompiler.parse(source).getAst()).toParsedExpr(); ParsedExpr.Builder parsedExprBuilder = parsedExpr.toBuilder(); parsedExprBuilder .getExprBuilder() diff --git a/runtime/src/main/java/dev/cel/runtime/CelAttributeParser.java b/runtime/src/main/java/dev/cel/runtime/CelAttributeParser.java index 446f069ee..4ad4722da 100644 --- a/runtime/src/main/java/dev/cel/runtime/CelAttributeParser.java +++ b/runtime/src/main/java/dev/cel/runtime/CelAttributeParser.java @@ -22,6 +22,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.primitives.UnsignedLong; import dev.cel.common.CelAbstractSyntaxTree; +import dev.cel.common.CelProtoAbstractSyntaxTree; import dev.cel.common.CelValidationException; import dev.cel.common.CelValidationResult; import dev.cel.parser.CelParser; @@ -110,7 +111,8 @@ public static CelAttributePattern parsePattern(String attribute) { try { CelAbstractSyntaxTree ast = result.getAst(); ArrayDeque qualifiers = new ArrayDeque<>(); - Expr node = ast.getProtoExpr(); + // TODO: Traverse using CelExpr + Expr node = CelProtoAbstractSyntaxTree.fromCelAst(ast).getExpr(); while (node != null) { switch (node.getExprKindCase()) { case IDENT_EXPR: diff --git a/testing/src/main/java/dev/cel/testing/BaseInterpreterTest.java b/testing/src/main/java/dev/cel/testing/BaseInterpreterTest.java index e42907b95..34ae763b2 100644 --- a/testing/src/main/java/dev/cel/testing/BaseInterpreterTest.java +++ b/testing/src/main/java/dev/cel/testing/BaseInterpreterTest.java @@ -93,7 +93,7 @@ private Object runTest(Activation activation) throws Exception { testOutput().println("bindings: " + activation); Object result = null; try { - result = eval.eval(ast.toCheckedExpr(), activation); + result = eval.eval(CelProtoAbstractSyntaxTree.fromCelAst(ast).toCheckedExpr(), activation); if (result instanceof ByteString) { // Note: this call may fail for printing byte sequences that are not valid UTF-8, but works // pretty well for test purposes. @@ -113,7 +113,7 @@ private Object runTest(Activation activation) throws Exception { * native CelAbstractSyntaxTree */ private void assertAstRoundTrip(CelAbstractSyntaxTree ast) { - CheckedExpr checkedExpr = ast.toCheckedExpr(); + CheckedExpr checkedExpr = CelProtoAbstractSyntaxTree.fromCelAst(ast).toCheckedExpr(); CelProtoAbstractSyntaxTree protoAst = CelProtoAbstractSyntaxTree.fromCelAst(ast); assertThat(checkedExpr).isEqualTo(protoAst.toCheckedExpr()); } diff --git a/testing/src/test/java/dev/cel/testing/EvalSyncTest.java b/testing/src/test/java/dev/cel/testing/EvalSyncTest.java index b50259954..0e15fa310 100644 --- a/testing/src/test/java/dev/cel/testing/EvalSyncTest.java +++ b/testing/src/test/java/dev/cel/testing/EvalSyncTest.java @@ -33,6 +33,7 @@ import com.google.type.Expr; import dev.cel.common.CelAbstractSyntaxTree; import dev.cel.common.CelOptions; +import dev.cel.common.CelProtoAbstractSyntaxTree; import dev.cel.common.types.CelType; import dev.cel.common.types.SimpleType; import dev.cel.compiler.CelCompiler; @@ -140,7 +141,10 @@ public EvalWithoutActivationTests(String expr, Object evaluatedResult) { @Test public void evaluateExpr_returnsExpectedResult() throws Exception { CelAbstractSyntaxTree ast = COMPILER.compile(expr).getAst(); - assertThat(EVAL.eval(ast.toCheckedExpr(), Activation.EMPTY)).isEqualTo(evaluatedResult); + assertThat( + EVAL.eval( + CelProtoAbstractSyntaxTree.fromCelAst(ast).toCheckedExpr(), Activation.EMPTY)) + .isEqualTo(evaluatedResult); } } @@ -178,7 +182,10 @@ public EvalWithActivationTests( @Test public void expr_returnsExpectedResult() throws Exception { CelAbstractSyntaxTree ast = compiler.compile(expr).getAst(); - assertThat(EVAL.eval(ast.toCheckedExpr(), Activation.of("x", paramValue))) + assertThat( + EVAL.eval( + CelProtoAbstractSyntaxTree.fromCelAst(ast).toCheckedExpr(), + Activation.of("x", paramValue))) .isEqualTo(evaluatedResult); } }