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 Nov 5, 2024
1 parent bdc782b commit ef1bd05
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();
// 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;
}
Expand Down

0 comments on commit ef1bd05

Please sign in to comment.