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 @@ -133,19 +133,23 @@ public final String toString() {

@Override
public Object select(String field) {
FieldLiteDescriptor fd = descriptor().findByFieldName(field);
if (fd == null) {
throw CelAttributeNotFoundException.of(
String.format("field '%s' is not declared in message '%s'", field, protoTypeName()));
}
FieldLiteDescriptor fd = findFieldDescriptor(field);
Object fieldValue = readField(fd);
return fieldValue != null ? fieldValue : protoLiteCelValueConverter().getDefaultCelValue(fd);
}

@Override
public Optional<Object> find(String field) {
// Per SelectableValue#find, a field that doesn't exist is reported as absent.
return Optional.ofNullable(descriptor().findByFieldName(field)).map(this::readField);
return Optional.ofNullable(readField(findFieldDescriptor(field)));
}

private FieldLiteDescriptor findFieldDescriptor(String field) {
FieldLiteDescriptor fd = descriptor().findByFieldName(field);
if (fd == null) {
throw CelAttributeNotFoundException.of(
String.format("field '%s' is not declared in message '%s'", field, protoTypeName()));
}
return fd;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,32 +368,35 @@ public void select_undeclaredField_throwsCelAttributeNotFoundException() {
+ " 'cel.expr.conformance.proto3.TestAllTypes'");
}

@SuppressWarnings("ImmutableEnumChecker") // Test only
private enum FindFieldTestCase {
PRESENT("single_string", Optional.of("foo")),
ABSENT("single_int64", Optional.empty()),
UNDECLARED("undeclared_field", Optional.empty());
@Test
public void find_undeclaredField_throwsCelAttributeNotFoundException() {
ProtoMessageLiteValue messageLiteValue =
ProtoMessageLiteValue.create(
TestAllTypes.getDefaultInstance(),
TEST_ALL_TYPES_DESCRIPTOR,
PROTO_LITE_CEL_VALUE_CONVERTER);

private final String fieldName;
private final Optional<Object> expected;
CelAttributeNotFoundException thrown =
assertThrows(
CelAttributeNotFoundException.class, () -> messageLiteValue.find("undeclared_field"));

FindFieldTestCase(String fieldName, Optional<Object> expected) {
this.fieldName = fieldName;
this.expected = expected;
}
assertThat(thrown)
.hasMessageThat()
.isEqualTo(
"field 'undeclared_field' is not declared in message"
+ " 'cel.expr.conformance.proto3.TestAllTypes'");
}

@Test
public void find_returnsValueOnlyIfFieldIsPresent(@TestParameter FindFieldTestCase testCase) {
public void find_returnsValueOnlyIfFieldIsPresent() {
ProtoMessageLiteValue messageLiteValue =
ProtoMessageLiteValue.create(
TestAllTypes.newBuilder().setSingleString("foo").build(),
TEST_ALL_TYPES_DESCRIPTOR,
PROTO_LITE_CEL_VALUE_CONVERTER);

Optional<Object> result = messageLiteValue.find(testCase.fieldName);

assertThat(result).isEqualTo(testCase.expected);
assertThat(messageLiteValue.find("single_string")).hasValue("foo");
assertThat(messageLiteValue.find("single_int64")).isEmpty();
}

@SuppressWarnings("ImmutableEnumChecker") // Test only
Expand Down
Loading