Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public static CelAttributeNotFoundException forFieldResolution(Collection<String
return new CelAttributeNotFoundException(formatErrorMessage(fields));
}

public static CelAttributeNotFoundException forMissingAttributes(Collection<String> attributes) {
return new CelAttributeNotFoundException(
"No such attribute(s): " + String.join(", ", attributes));
}

private static String formatErrorMessage(Collection<String> fields) {
String maybePlural = "";
if (fields.size() > 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,44 @@
final class MissingAttribute implements Attribute {

private final ImmutableSet<String> missingAttributes;
private final Kind kind;

@Override
public Object resolve(GlobalResolver ctx, ExecutionFrame frame) {
throw CelAttributeNotFoundException.forFieldResolution(missingAttributes);
switch (kind) {
case ATTRIBUTE_NOT_FOUND:
throw CelAttributeNotFoundException.forMissingAttributes(missingAttributes);
case FIELD_NOT_FOUND:
throw CelAttributeNotFoundException.forFieldResolution(missingAttributes);
}

throw new IllegalArgumentException("Unexpected kind: " + kind);
}

@Override
public Attribute addQualifier(Qualifier qualifier) {
throw new UnsupportedOperationException("Unsupported operation");
}

static MissingAttribute newMissingAttribute(String... attributeNames) {
return newMissingAttribute(ImmutableSet.copyOf(attributeNames));
static MissingAttribute newMissingAttribute(ImmutableSet<String> attributeNames) {
return new MissingAttribute(attributeNames, Kind.ATTRIBUTE_NOT_FOUND);
}

static MissingAttribute newMissingAttribute(ImmutableSet<String> attributeNames) {
return new MissingAttribute(attributeNames);
static MissingAttribute newMissingField(String... attributeNames) {
return newMissingField(ImmutableSet.copyOf(attributeNames));
}

private MissingAttribute(ImmutableSet<String> missingAttributes) {
static MissingAttribute newMissingField(ImmutableSet<String> attributeNames) {
return new MissingAttribute(attributeNames, Kind.FIELD_NOT_FOUND);
}

private MissingAttribute(ImmutableSet<String> missingAttributes, Kind kind) {
this.missingAttributes = missingAttributes;
this.kind = kind;
}

private enum Kind {
ATTRIBUTE_NOT_FOUND,
FIELD_NOT_FOUND
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package dev.cel.runtime.planner;

import static dev.cel.runtime.planner.MissingAttribute.newMissingAttribute;
import static dev.cel.runtime.planner.MissingAttribute.newMissingField;

import dev.cel.common.values.SelectableValue;
import java.util.Map;
Expand All @@ -40,7 +40,7 @@ public Object qualify(Object obj) {
return map.containsKey(value);
}

return newMissingAttribute(value.toString());
return newMissingField(value.toString());
}

static PresenceTestQualifier create(Object value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,15 @@ public void planIdent_typeLiteral(@TestParameter TypeLiteralTestCase testCase) t
assertThat(result).isEqualTo(testCase.type);
}

@Test
public void plan_ident_missingAttribute_throws() throws Exception {
CelAbstractSyntaxTree ast = compile("int_var");
Program program = PLANNER.plan(ast);

CelEvaluationException e = assertThrows(CelEvaluationException.class, program::eval);
assertThat(e).hasMessageThat().contains("evaluation error at <input>:0: No such attribute(s)");
}

@Test
public void plan_ident_withContainer() throws Exception {
CelAbstractSyntaxTree ast = compile("abbr.ident");
Expand Down Expand Up @@ -713,14 +722,13 @@ public void plan_select_onMapVariable() throws Exception {
public void plan_select_mapVarInputMissing_throws() throws Exception {
CelAbstractSyntaxTree ast = compile("map_var.foo");
Program program = PLANNER.plan(ast);
String errorMessage = "evaluation error at <input>:7: Error resolving ";
String errorMessage = "evaluation error at <input>:7: No such attribute(s): ";
if (isParseOnly) {
errorMessage +=
"fields 'cel.expr.conformance.proto3.map_var, cel.expr.conformance.map_var,"
+ " cel.expr.map_var, cel.map_var, map_var'";
} else {
errorMessage += "field 'map_var'";
"cel.expr.conformance.proto3.map_var, cel.expr.conformance.map_var, cel.expr.map_var,"
+ " cel.map_var, ";
}
errorMessage += "map_var";

CelEvaluationException e =
assertThrows(CelEvaluationException.class, () -> program.eval(ImmutableMap.of()));
Expand Down
Loading