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 28, 2024
1 parent 401da42 commit 3b60bdc
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 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 @@ -18,8 +18,12 @@

import static com.google.common.base.Preconditions.checkNotNull;

import com.github.xds.type.v3.CelExpression;
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 +46,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 {
CelExpression exprMatch = proto.getExprMatch();
// TODO(sergiitk): do i need this?
// checkNotNull(exprMatch);

if (!exprMatch.hasCelExprChecked()) {
throw new ResourceInvalidException("RateLimitQuotaFilterConfig domain 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 3b60bdc

Please sign in to comment.