Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions 5 common/ast/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ java_library(
exports = ["//common/src/main/java/dev/cel/common/ast:expr_factory"],
)

java_library(
name = "expr_util",
exports = ["//common/src/main/java/dev/cel/common/ast:expr_util"],
)

java_library(
name = "mutable_expr",
exports = ["//common/src/main/java/dev/cel/common/ast:mutable_expr"],
Expand Down
16 changes: 0 additions & 16 deletions 16 common/src/main/java/dev/cel/common/ast/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,6 @@ java_library(
],
)

java_library(
name = "expr_util",
srcs = ["CelExprUtil.java"],
tags = [
],
deps = [
":ast",
"//bundle:cel",
"//common",
"//common:compiler_common",
"//runtime",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
)

java_library(
name = "mutable_expr",
srcs = MUTABLE_EXPR_SOURCES,
Expand Down
71 changes: 0 additions & 71 deletions 71 common/src/main/java/dev/cel/common/ast/CelExprUtil.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ java_library(
"//common:compiler_common",
"//common:mutable_ast",
"//common/ast",
"//common/ast:expr_util",
"//common/ast:mutable_expr",
"//common/navigation:mutable_navigation",
"//extensions:optional_library",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
import dev.cel.bundle.Cel;
import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.CelMutableAst;
import dev.cel.common.CelSource;
import dev.cel.common.CelValidationException;
import dev.cel.common.ast.CelConstant;
import dev.cel.common.ast.CelExpr;
import dev.cel.common.ast.CelExpr.ExprKind.Kind;
import dev.cel.common.ast.CelExprUtil;
import dev.cel.common.ast.CelMutableExpr;
import dev.cel.common.ast.CelMutableExpr.CelMutableCall;
import dev.cel.common.ast.CelMutableExpr.CelMutableList;
Expand Down Expand Up @@ -248,7 +249,7 @@ private Optional<CelMutableAst> maybeFold(
throws CelOptimizationException {
Object result;
try {
result = CelExprUtil.evaluateExpr(cel, CelMutableExprConverter.fromMutableExpr(node.expr()));
result = evaluateExpr(cel, CelMutableExprConverter.fromMutableExpr(node.expr()));
} catch (CelValidationException | CelEvaluationException e) {
throw new CelOptimizationException(
"Constant folding failure. Failed to evaluate subtree due to: " + e.getMessage(), e);
Expand Down Expand Up @@ -591,6 +592,16 @@ private CelMutableAst pruneOptionalStructElements(CelMutableAst ast, CelMutableE
return ast;
}

@CanIgnoreReturnValue
private static Object evaluateExpr(Cel cel, CelExpr expr)
throws CelValidationException, CelEvaluationException {
CelAbstractSyntaxTree ast =
CelAbstractSyntaxTree.newParsedAst(expr, CelSource.newBuilder().build());
ast = cel.check(ast).getAst();

return cel.createProgram(ast).eval();
}

/** Options to configure how Constant Folding behave. */
@AutoValue
public abstract static class ConstantFoldingOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,13 @@ java_library(
visibility = ["//visibility:private"],
deps = [
"//bundle:cel",
"//common",
"//common:compiler_common",
"//common/ast",
"//common/ast:expr_factory",
"//common/ast:expr_util",
"//common/navigation",
"//runtime",
"//validator:ast_validator",
"@maven//:com_google_errorprone_error_prone_annotations",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@

package dev.cel.validator.validators;

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import dev.cel.bundle.Cel;
import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.CelSource;
import dev.cel.common.CelValidationException;
import dev.cel.common.ast.CelExpr;
import dev.cel.common.ast.CelExpr.ExprKind.Kind;
import dev.cel.common.ast.CelExprFactory;
import dev.cel.common.ast.CelExprUtil;
import dev.cel.common.navigation.CelNavigableAst;
import dev.cel.common.navigation.CelNavigableExpr;
import dev.cel.runtime.CelEvaluationException;
import dev.cel.validator.CelAstValidator;

/**
Expand Down Expand Up @@ -57,7 +61,7 @@ public void validate(CelNavigableAst navigableAst, Cel cel, IssuesFactory issues
CelExpr callExpr =
exprFactory.newGlobalCall(functionName, exprFactory.newConstant(expr.constant()));
try {
CelExprUtil.evaluateExpr(cel, callExpr, expectedResultType);
evaluateExpr(cel, callExpr, expectedResultType);
} catch (Exception e) {
issuesFactory.addError(
expr.id(),
Expand All @@ -66,4 +70,21 @@ public void validate(CelNavigableAst navigableAst, Cel cel, IssuesFactory issues
}
});
}

@CanIgnoreReturnValue
private static Object evaluateExpr(Cel cel, CelExpr expr, Class<?> expectedResultType)
throws CelValidationException, CelEvaluationException {
CelAbstractSyntaxTree ast =
CelAbstractSyntaxTree.newParsedAst(expr, CelSource.newBuilder().build());
ast = cel.check(ast).getAst();
Object result = cel.createProgram(ast).eval();

if (!expectedResultType.isInstance(result)) {
throw new IllegalStateException(
String.format(
"Expected %s type but got %s instead",
expectedResultType.getName(), result.getClass().getName()));
}
return result;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.