Skip to content

Commit ef1bd05

Browse files
committed
Add CelMatcher.fromEnvoyProto - just to import dev.cel.expr
1 parent bdc782b commit ef1bd05

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

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

+28
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
import static com.google.common.base.Preconditions.checkNotNull;
2020

2121
import dev.cel.common.CelAbstractSyntaxTree;
22+
import dev.cel.common.CelProtoAbstractSyntaxTree;
23+
import dev.cel.expr.CheckedExpr;
2224
import dev.cel.runtime.CelEvaluationException;
25+
import io.grpc.xds.client.XdsResourceType.ResourceInvalidException;
2326
import java.util.function.Predicate;
2427

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

48+
public static CelMatcher fromEnvoyProto(com.github.xds.type.matcher.v3.CelMatcher proto)
49+
throws ResourceInvalidException {
50+
com.github.xds.type.v3.CelExpression exprMatch = proto.getExprMatch();
51+
// TODO(sergiitk): do i need this?
52+
// checkNotNull(exprMatch);
53+
54+
if (!exprMatch.hasCelExprChecked()) {
55+
throw ResourceInvalidException.ofResource(proto, "cel_expr_checked is required");
56+
}
57+
58+
// Canonical CEL.
59+
CheckedExpr celExprChecked = exprMatch.getCelExprChecked();
60+
61+
// TODO(sergiitk): catch tree build errors?
62+
CelAbstractSyntaxTree ast = CelProtoAbstractSyntaxTree.fromCheckedExpr(celExprChecked).getAst();
63+
64+
try {
65+
return new CelMatcher(ast, proto.getDescription());
66+
} catch (CelEvaluationException e) {
67+
throw ResourceInvalidException.ofResource(exprMatch,
68+
"Error Building CEL Program cel_expr_checked: " + e.getErrorCode() + " "
69+
+ e.getMessage());
70+
}
71+
}
72+
4573
public String description() {
4674
return description;
4775
}

0 commit comments

Comments
 (0)