Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions runtime/src/main/java/dev/cel/runtime/planner/ProgramPlanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ private Optional<PlannedInterpretable> maybeInterceptOptionalCalls(

private PlannedInterpretable planCreateStruct(CelExpr celExpr, PlannerContext ctx) {
CelStruct struct = celExpr.struct();
CelType structType = resolveStructType(struct);
CelType structType = resolveStructType(celExpr, ctx);

ImmutableList<Entry> entries = struct.entries();
String[] keys = new String[entries.size()];
Expand Down Expand Up @@ -489,7 +489,17 @@ private ResolvedFunction resolveFunction(
return ResolvedFunction.newBuilder().setFunctionName(functionName).setTarget(target).build();
}

private CelType resolveStructType(CelStruct struct) {
private CelType resolveStructType(CelExpr expr, PlannerContext ctx) {
CelType checkedType = ctx.typeMap().get(expr.id());
if (checkedType != null) {
CelKind kind = checkedType.kind();
// Type-checked ASTs do not need a type-provider lookup as long as it's of expected kind.
if (isValidStructKind(kind)) {
return checkedType;
}
}

CelStruct struct = expr.struct();
String messageName = struct.messageName();
for (String typeName : container.resolveCandidateNames(messageName)) {
CelType structType = typeProvider.findType(typeName).orElse(null);
Expand All @@ -499,9 +509,7 @@ private CelType resolveStructType(CelStruct struct) {

CelKind kind = structType.kind();

if (!kind.equals(CelKind.STRUCT)
&& !kind.equals(CelKind.TIMESTAMP)
&& !kind.equals(CelKind.DURATION)) {
if (!isValidStructKind(kind)) {
throw new IllegalArgumentException(
String.format(
"Expected struct type for %s, got %s", structType.name(), structType.kind()));
Expand All @@ -513,6 +521,12 @@ private CelType resolveStructType(CelStruct struct) {
throw new IllegalArgumentException("Undefined type name: " + messageName);
}

private static boolean isValidStructKind(CelKind kind) {
return kind.equals(CelKind.STRUCT)
|| kind.equals(CelKind.TIMESTAMP)
|| kind.equals(CelKind.DURATION);
}

/** Converts a given expression into a qualified name, if possible. */
private Optional<String> toQualifiedName(CelExpr operand) {
switch (operand.getKind()) {
Expand Down
Loading