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
21 changes: 13 additions & 8 deletions common/src/main/java/dev/cel/common/values/ProtoMessageValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,29 @@ public abstract class ProtoMessageValue extends StructValue<String, Message>
@Override
public abstract Message value();

@Override
public abstract CelType celType();

abstract CelDescriptorPool celDescriptorPool();

abstract ProtoCelValueConverter protoCelValueConverter();

abstract boolean enableJsonFieldNames();

@Override
public CelType celType() {
// Built on demand, since one of these values is created on every hop but rarely asked its type.
return StructTypeReference.create(value().getDescriptorForType().getFullName());
}

@Override
public boolean isZeroValue() {
return value().getDefaultInstanceForType().equals(value());
}

@Override
public final String toString() {
// AutoValue's would print the message's contents, which may be sensitive.
return "ProtoMessageValue{protoTypeName=" + value().getDescriptorForType().getFullName() + "}";
}

@Override
public Object select(String field) {
FieldDescriptor fieldDescriptor =
Expand Down Expand Up @@ -95,11 +104,7 @@ public static ProtoMessageValue create(
checkNotNull(celDescriptorPool);
checkNotNull(protoCelValueConverter);
return new AutoValue_ProtoMessageValue(
value,
StructTypeReference.create(value.getDescriptorForType().getFullName()),
celDescriptorPool,
protoCelValueConverter,
enableJsonFieldNames);
value, celDescriptorPool, protoCelValueConverter, enableJsonFieldNames);
}

private Optional<Object> findFieldValue(FieldDescriptor fieldDescriptor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,19 @@ public void celTypeTest() {
.isEqualTo(StructTypeReference.create(TestAllTypes.getDescriptor().getFullName()));
}

@Test
public void toString_returnsTypeNameOnly() {
ProtoMessageValue protoMessageValue =
ProtoMessageValue.create(
TestAllTypes.newBuilder().setSingleString("secret").build(),
DefaultDescriptorPool.INSTANCE,
PROTO_CEL_VALUE_CONVERTER,
/* enableJsonFieldNames= */ false);

assertThat(protoMessageValue.toString())
.isEqualTo("ProtoMessageValue{protoTypeName=cel.expr.conformance.proto2.TestAllTypes}");
}

@Test
public void findField_jsonName_success() {
TestAllTypes testAllTypes = TestAllTypes.newBuilder().setSingleInt32(42).build();
Expand Down
Loading