diff --git a/xds/src/main/java/io/grpc/xds/internal/matchers/CelMatcher.java b/xds/src/main/java/io/grpc/xds/internal/matchers/CelMatcher.java index 8659d6845ae..186cc64b439 100644 --- a/xds/src/main/java/io/grpc/xds/internal/matchers/CelMatcher.java +++ b/xds/src/main/java/io/grpc/xds/internal/matchers/CelMatcher.java @@ -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. */ @@ -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(); + // TODO(sergiitk): do i need this? + // checkNotNull(exprMatch); + + if (!exprMatch.hasCelExprChecked()) { + throw ResourceInvalidException.ofResource(proto, "cel_expr_checked is required"); + } + + // Canonical CEL. + CheckedExpr celExprChecked = exprMatch.getCelExprChecked(); + + // TODO(sergiitk): catch tree build errors? + CelAbstractSyntaxTree ast = CelProtoAbstractSyntaxTree.fromCheckedExpr(celExprChecked).getAst(); + + 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()); + } + } + public String description() { return description; }