Skip to content

Commit 503ef65

Browse files
l46kokcopybara-github
authored andcommitted
Remove extraneous protoTypeName argument from RuntimeTypeProvider interface
PiperOrigin-RevId: 764810879
1 parent 818b259 commit 503ef65

11 files changed

Lines changed: 44 additions & 91 deletions

common/src/main/java/dev/cel/common/values/BaseProtoCelValueConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
@Internal
5252
public abstract class BaseProtoCelValueConverter extends CelValueConverter {
5353

54-
public abstract CelValue fromProtoMessageToCelValue(String protoTypeName, MessageLite msg);
54+
public abstract CelValue fromProtoMessageToCelValue(MessageLite msg);
5555

5656
/**
5757
* Adapts a {@link CelValue} to a native Java object. The CelValue is adapted into protobuf object

common/src/main/java/dev/cel/common/values/ProtoCelValueConverter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ public static ProtoCelValueConverter newInstance(
5757
}
5858

5959
@Override
60-
public CelValue fromProtoMessageToCelValue(String unusedProtoTypeName, MessageLite msg) {
61-
return fromProtoMessageToCelValue((MessageOrBuilder) msg);
60+
public CelValue fromProtoMessageToCelValue(MessageLite msg) {
61+
return fromDescriptorMessageToCelValue((MessageOrBuilder) msg);
6262
}
6363

6464
/** Adapts a Protobuf message into a {@link CelValue}. */
65-
public CelValue fromProtoMessageToCelValue(MessageOrBuilder message) {
65+
public CelValue fromDescriptorMessageToCelValue(MessageOrBuilder message) {
6666
Preconditions.checkNotNull(message);
6767

6868
// Attempt to convert the proto from a dynamic message into a concrete message if possible.
@@ -151,7 +151,7 @@ public CelValue fromProtoMessageFieldToCelValue(
151151
return fromJavaObjectToCelValue(map);
152152
}
153153

154-
return fromProtoMessageToCelValue((MessageOrBuilder) result);
154+
return fromDescriptorMessageToCelValue((MessageOrBuilder) result);
155155
case UINT32:
156156
return UintValue.create((int) result);
157157
case UINT64:

common/src/main/java/dev/cel/common/values/ProtoLiteCelValueConverter.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ CelValue getDefaultCelValue(String protoTypeName, String fieldName) {
157157

158158
Object defaultValue = getDefaultValue(fieldDescriptor);
159159
if (defaultValue instanceof MessageLite) {
160-
return fromProtoMessageToCelValue(
161-
fieldDescriptor.getFieldProtoTypeName(), (MessageLite) defaultValue);
160+
return fromProtoMessageToCelValue((MessageLite) defaultValue);
162161
}
163162

164163
return fromJavaObjectToCelValue(defaultValue);
@@ -357,17 +356,15 @@ private static Object readUnknownField(int tagWireType, CodedInputStream inputSt
357356
}
358357

359358
@Override
360-
public CelValue fromProtoMessageToCelValue(String protoTypeName, MessageLite msg) {
359+
@SuppressWarnings("LiteProtoToString") // No alternative identifier to use. Debug only info is OK.
360+
public CelValue fromProtoMessageToCelValue(MessageLite msg) {
361361
checkNotNull(msg);
362-
checkNotNull(protoTypeName);
363362

364363
MessageLiteDescriptor descriptor =
365364
descriptorPool
366365
.findDescriptor(msg)
367366
.orElseThrow(
368-
() ->
369-
new NoSuchElementException(
370-
"Could not find a descriptor for: " + protoTypeName));
367+
() -> new NoSuchElementException("Could not find a descriptor for: " + msg));
371368
WellKnownProto wellKnownProto =
372369
WellKnownProto.getByTypeName(descriptor.getProtoTypeName()).orElse(null);
373370

common/src/main/java/dev/cel/common/values/ProtoMessageLiteValueProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public Optional<CelValue> newValue(String structType, Map<String, Object> fields
5353
}
5454

5555
MessageLite message = descriptor.newMessageBuilder().build();
56-
return Optional.of(protoLiteCelValueConverter.fromProtoMessageToCelValue(structType, message));
56+
return Optional.of(protoLiteCelValueConverter.fromProtoMessageToCelValue(message));
5757
}
5858

5959
public static ProtoMessageLiteValueProvider newInstance(CelLiteDescriptor... descriptors) {

common/src/test/java/dev/cel/common/values/ProtoLiteCelValueConverterTest.java

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
3737
import dev.cel.common.internal.CelLiteDescriptorPool;
3838
import dev.cel.common.internal.DefaultLiteDescriptorPool;
39-
import dev.cel.common.internal.WellKnownProto;
4039
import dev.cel.common.values.ProtoLiteCelValueConverter.MessageFields;
4140
import dev.cel.expr.conformance.proto3.TestAllTypes;
4241
import dev.cel.expr.conformance.proto3.TestAllTypesCelDescriptor;
@@ -59,47 +58,36 @@ public void fromProtoMessageToCelValue_withTestMessage_convertsToProtoMessageLit
5958
ProtoMessageLiteValue protoMessageLiteValue =
6059
(ProtoMessageLiteValue)
6160
PROTO_LITE_CEL_VALUE_CONVERTER.fromProtoMessageToCelValue(
62-
"cel.expr.conformance.proto3.TestAllTypes", TestAllTypes.getDefaultInstance());
61+
TestAllTypes.getDefaultInstance());
6362

6463
assertThat(protoMessageLiteValue.value()).isEqualTo(TestAllTypes.getDefaultInstance());
6564
}
6665

6766
private enum WellKnownProtoTestCase {
68-
BOOL(WellKnownProto.BOOL_VALUE, com.google.protobuf.BoolValue.of(true), BoolValue.create(true)),
67+
BOOL(com.google.protobuf.BoolValue.of(true), BoolValue.create(true)),
6968
BYTES(
70-
WellKnownProto.BYTES_VALUE,
7169
com.google.protobuf.BytesValue.of(ByteString.copyFromUtf8("test")),
7270
BytesValue.create(CelByteString.of("test".getBytes(UTF_8)))),
73-
FLOAT(WellKnownProto.FLOAT_VALUE, FloatValue.of(1.0f), DoubleValue.create(1.0f)),
74-
DOUBLE(
75-
WellKnownProto.DOUBLE_VALUE,
76-
com.google.protobuf.DoubleValue.of(1.0),
77-
DoubleValue.create(1.0)),
78-
INT32(WellKnownProto.INT32_VALUE, Int32Value.of(1), IntValue.create(1)),
79-
INT64(WellKnownProto.INT64_VALUE, Int64Value.of(1L), IntValue.create(1L)),
80-
STRING(
81-
WellKnownProto.STRING_VALUE,
82-
com.google.protobuf.StringValue.of("test"),
83-
StringValue.create("test")),
71+
FLOAT(FloatValue.of(1.0f), DoubleValue.create(1.0f)),
72+
DOUBLE(com.google.protobuf.DoubleValue.of(1.0), DoubleValue.create(1.0)),
73+
INT32(Int32Value.of(1), IntValue.create(1)),
74+
INT64(Int64Value.of(1L), IntValue.create(1L)),
75+
STRING(com.google.protobuf.StringValue.of("test"), StringValue.create("test")),
8476

8577
DURATION(
86-
WellKnownProto.DURATION,
8778
Duration.newBuilder().setSeconds(10).setNanos(50).build(),
8879
DurationValue.create(java.time.Duration.ofSeconds(10, 50))),
8980
TIMESTAMP(
90-
WellKnownProto.TIMESTAMP,
9181
Timestamp.newBuilder().setSeconds(1678886400L).setNanos(123000000).build(),
9282
TimestampValue.create(Instant.ofEpochSecond(1678886400L, 123000000))),
93-
UINT32(WellKnownProto.UINT32_VALUE, UInt32Value.of(1), UintValue.create(1)),
94-
UINT64(WellKnownProto.UINT64_VALUE, UInt64Value.of(1L), UintValue.create(1L)),
83+
UINT32(UInt32Value.of(1), UintValue.create(1)),
84+
UINT64(UInt64Value.of(1L), UintValue.create(1L)),
9585
;
9686

97-
private final WellKnownProto wellKnownProto;
9887
private final MessageLite msg;
9988
private final CelValue celValue;
10089

101-
WellKnownProtoTestCase(WellKnownProto wellKnownProto, MessageLite msg, CelValue celValue) {
102-
this.wellKnownProto = wellKnownProto;
90+
WellKnownProtoTestCase(MessageLite msg, CelValue celValue) {
10391
this.msg = msg;
10492
this.celValue = celValue;
10593
}
@@ -109,8 +97,7 @@ private enum WellKnownProtoTestCase {
10997
public void fromProtoMessageToCelValue_withWellKnownProto_convertsToEquivalentCelValue(
11098
@TestParameter WellKnownProtoTestCase testCase) {
11199
CelValue convertedCelValue =
112-
PROTO_LITE_CEL_VALUE_CONVERTER.fromProtoMessageToCelValue(
113-
testCase.wellKnownProto.typeName(), testCase.msg);
100+
PROTO_LITE_CEL_VALUE_CONVERTER.fromProtoMessageToCelValue(testCase.msg);
114101

115102
assertThat(convertedCelValue).isEqualTo(testCase.celValue);
116103
}

runtime/src/main/java/dev/cel/runtime/CelValueRuntimeTypeProvider.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ final class CelValueRuntimeTypeProvider implements RuntimeTypeProvider {
4141
private static final BaseProtoCelValueConverter DEFAULT_CEL_VALUE_CONVERTER =
4242
new BaseProtoCelValueConverter() {
4343
@Override
44-
public CelValue fromProtoMessageToCelValue(String protoTypeName, MessageLite msg) {
44+
public CelValue fromProtoMessageToCelValue(MessageLite msg) {
4545
throw new UnsupportedOperationException(
4646
"A value provider must be provided in the runtime to handle protobuf messages");
4747
}
@@ -82,28 +82,24 @@ public Object createMessage(String messageName, Map<String, Object> values) {
8282
}
8383

8484
@Override
85-
public Object selectField(String typeName, Object message, String fieldName) {
86-
SelectableValue<CelValue> selectableValue =
87-
getSelectableValueOrThrow(typeName, message, fieldName);
85+
public Object selectField(Object message, String fieldName) {
86+
SelectableValue<CelValue> selectableValue = getSelectableValueOrThrow(message, fieldName);
8887

8988
return unwrapCelValue(selectableValue.select(StringValue.create(fieldName)));
9089
}
9190

9291
@Override
93-
public Object hasField(String messageName, Object message, String fieldName) {
94-
SelectableValue<CelValue> selectableValue =
95-
getSelectableValueOrThrow(messageName, message, fieldName);
92+
public Object hasField(Object message, String fieldName) {
93+
SelectableValue<CelValue> selectableValue = getSelectableValueOrThrow(message, fieldName);
9694

9795
return selectableValue.find(StringValue.create(fieldName)).isPresent();
9896
}
9997

10098
@SuppressWarnings("unchecked")
101-
private SelectableValue<CelValue> getSelectableValueOrThrow(
102-
String typeName, Object obj, String fieldName) {
99+
private SelectableValue<CelValue> getSelectableValueOrThrow(Object obj, String fieldName) {
103100
CelValue convertedCelValue;
104101
if ((obj instanceof MessageLite)) {
105-
convertedCelValue =
106-
protoCelValueConverter.fromProtoMessageToCelValue(typeName, (MessageLite) obj);
102+
convertedCelValue = protoCelValueConverter.fromProtoMessageToCelValue((MessageLite) obj);
107103
} else {
108104
convertedCelValue = protoCelValueConverter.fromJavaObjectToCelValue(obj);
109105
}
@@ -128,7 +124,7 @@ public Object adapt(String messageName, Object message) {
128124

129125
if (message instanceof MessageLite) {
130126
return unwrapCelValue(
131-
protoCelValueConverter.fromProtoMessageToCelValue(messageName, (MessageLite) message));
127+
protoCelValueConverter.fromProtoMessageToCelValue((MessageLite) message));
132128
} else {
133129
return unwrapCelValue(protoCelValueConverter.fromJavaObjectToCelValue(message));
134130
}

runtime/src/main/java/dev/cel/runtime/DefaultInterpreter.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,7 @@ public Object eval(GlobalResolver resolver, CelEvaluationListener listener)
150150
@Override
151151
public Object eval(GlobalResolver resolver, FunctionResolver lateBoundFunctionResolver)
152152
throws CelEvaluationException {
153-
return eval(resolver,
154-
lateBoundFunctionResolver,
155-
CelEvaluationListener.noOpListener());
153+
return eval(resolver, lateBoundFunctionResolver, CelEvaluationListener.noOpListener());
156154
}
157155

158156
@Override
@@ -365,12 +363,10 @@ private IntermediateResult evalFieldSelect(
365363
return IntermediateResult.create(attribute, operand);
366364
}
367365

368-
CelType operandCheckedType = getCheckedTypeOrThrow(operandExpr);
369366
if (isTestOnly) {
370-
return IntermediateResult.create(
371-
attribute, typeProvider.hasField(operandCheckedType.name(), operand, field));
367+
return IntermediateResult.create(attribute, typeProvider.hasField(operand, field));
372368
}
373-
Object fieldValue = typeProvider.selectField(operandCheckedType.name(), operand, field);
369+
Object fieldValue = typeProvider.selectField(operand, field);
374370

375371
return IntermediateResult.create(
376372
attribute, InterpreterUtil.valueOrUnknown(fieldValue, expr.id()));
@@ -736,9 +732,7 @@ private Optional<IntermediateResult> maybeEvalOptionalSelectField(
736732
}
737733

738734
String field = callExpr.args().get(1).constant().stringValue();
739-
CelType checkedType = getCheckedTypeOrThrow(expr);
740-
boolean hasField =
741-
(boolean) typeProvider.hasField(checkedType.name(), lhsResult.value(), field);
735+
boolean hasField = (boolean) typeProvider.hasField(lhsResult.value(), field);
742736
if (!hasField) {
743737
// Protobuf sets default (zero) values to uninitialized fields.
744738
// In case of CEL's optional values, we want to explicitly return Optional.none()

runtime/src/main/java/dev/cel/runtime/DescriptorMessageProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public DescriptorMessageProvider(ProtoMessageFactory protoMessageFactory, CelOpt
9898

9999
@Override
100100
@SuppressWarnings("unchecked")
101-
public @Nullable Object selectField(String unusedTypeName, Object message, String fieldName) {
101+
public @Nullable Object selectField(Object message, String fieldName) {
102102
boolean isOptionalMessage = false;
103103
if (message instanceof Optional) {
104104
isOptionalMessage = true;
@@ -148,7 +148,7 @@ public Object adapt(String messageName, Object message) {
148148
}
149149

150150
@Override
151-
public Object hasField(String messageName, Object message, String fieldName) {
151+
public Object hasField(Object message, String fieldName) {
152152
if (message instanceof Optional<?>) {
153153
Optional<?> optionalMessage = (Optional<?>) message;
154154
if (!optionalMessage.isPresent()) {

runtime/src/main/java/dev/cel/runtime/MessageProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ public interface MessageProvider {
2929
Object createMessage(String messageName, Map<String, Object> values);
3030

3131
/** Select field from message. */
32-
Object selectField(String messageName, Object message, String fieldName);
32+
Object selectField(Object message, String fieldName);
3333

3434
/** Check whether a field is set on message. */
35-
Object hasField(String messageName, Object message, String fieldName);
35+
Object hasField(Object message, String fieldName);
3636

3737
/** Adapt object to its message value with source location metadata on failure. */
3838
Object adapt(String messageName, Object message);

runtime/src/test/java/dev/cel/runtime/DefaultInterpreterTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ public Object createMessage(String messageName, Map<String, Object> values) {
5858
}
5959

6060
@Override
61-
public Object selectField(String typeName, Object message, String fieldName) {
61+
public Object selectField(Object message, String fieldName) {
6262
return null;
6363
}
6464

6565
@Override
66-
public Object hasField(String messageName, Object message, String fieldName) {
66+
public Object hasField(Object message, String fieldName) {
6767
return null;
6868
}
6969

0 commit comments

Comments
 (0)