Skip to content

Commit

Permalink
Add CelMatcher.fromEnvoyProto - just to import dev.cel.expr
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiitk committed Oct 30, 2024
1 parent 401da42 commit 011f97f
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions xds/src/main/java/io/grpc/xds/internal/matchers/CelMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
import static com.google.common.base.Preconditions.checkNotNull;

import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.CelProtoAbstractSyntaxTree;
import dev.cel.expr.CheckedExpr;
import dev.cel.runtime.CelEvaluationException;
import io.grpc.xds.client.XdsResourceType.ResourceInvalidException;
import java.util.function.Predicate;

/** Unified Matcher API: xds.type.matcher.v3.CelMatcher. */
Expand All @@ -42,6 +45,31 @@ public static CelMatcher create(CelAbstractSyntaxTree ast, String description)
return new CelMatcher(ast, description);
}

public static CelMatcher fromEnvoyProto(com.github.xds.type.matcher.v3.CelMatcher proto)
throws ResourceInvalidException {
com.github.xds.type.v3.CelExpression exprMatch = proto.getExprMatch();

Check warning on line 50 in xds/src/main/java/io/grpc/xds/internal/matchers/CelMatcher.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/internal/matchers/CelMatcher.java#L50

Added line #L50 was not covered by tests
// TODO(sergiitk): do i need this?
// checkNotNull(exprMatch);

if (!exprMatch.hasCelExprChecked()) {
throw ResourceInvalidException.ofResource(proto, "cel_expr_checked is required");

Check warning on line 55 in xds/src/main/java/io/grpc/xds/internal/matchers/CelMatcher.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/internal/matchers/CelMatcher.java#L55

Added line #L55 was not covered by tests
}

// Canonical CEL.
CheckedExpr celExprChecked = exprMatch.getCelExprChecked();

Check warning on line 59 in xds/src/main/java/io/grpc/xds/internal/matchers/CelMatcher.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/internal/matchers/CelMatcher.java#L59

Added line #L59 was not covered by tests

// TODO(sergiitk): catch tree build errors?
CelAbstractSyntaxTree ast = CelProtoAbstractSyntaxTree.fromCheckedExpr(celExprChecked).getAst();

Check warning on line 62 in xds/src/main/java/io/grpc/xds/internal/matchers/CelMatcher.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/internal/matchers/CelMatcher.java#L62

Added line #L62 was not covered by tests

try {
return new CelMatcher(ast, proto.getDescription());
} catch (CelEvaluationException e) {
throw ResourceInvalidException.ofResource(exprMatch,
"Error Building CEL Program cel_expr_checked: " + e.getErrorCode() + " "
+ e.getMessage());

Check warning on line 69 in xds/src/main/java/io/grpc/xds/internal/matchers/CelMatcher.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/internal/matchers/CelMatcher.java#L65-L69

Added lines #L65 - L69 were not covered by tests
}
}

public String description() {
return description;
}
Expand Down

0 comments on commit 011f97f

Please sign in to comment.