Skip to content

Commit fc288c6

Browse files
l46kokcopybara-github
authored andcommitted
Enable lite runtime to evaluate select optimized messages without descriptors
PiperOrigin-RevId: 974909937
1 parent a2353b3 commit fc288c6

18 files changed

Lines changed: 2524 additions & 10 deletions

File tree

common/src/main/java/dev/cel/common/values/BUILD.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ java_library(
317317
srcs = [
318318
"ProtoLiteCelValueConverter.java",
319319
"ProtoMessageLiteValue.java",
320+
"RawProtoMessageLiteValue.java",
320321
],
321322
tags = [
322323
],
@@ -325,6 +326,7 @@ java_library(
325326
":values",
326327
"//:auto_value",
327328
"//common/annotations",
329+
"//common/exceptions:attribute_not_found",
328330
"//common/internal:cel_lite_descriptor_pool",
329331
"//common/internal:well_known_proto",
330332
"//common/types",
@@ -342,6 +344,7 @@ cel_android_library(
342344
srcs = [
343345
"ProtoLiteCelValueConverter.java",
344346
"ProtoMessageLiteValue.java",
347+
"RawProtoMessageLiteValue.java",
345348
],
346349
tags = [
347350
],
@@ -350,6 +353,7 @@ cel_android_library(
350353
":values_android",
351354
"//:auto_value",
352355
"//common/annotations",
356+
"//common/exceptions:attribute_not_found",
353357
"//common/internal:cel_lite_descriptor_pool_android",
354358
"//common/internal:well_known_proto_android",
355359
"//common/types:type_providers_android",

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

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.common.annotations.VisibleForTesting;
2121
import com.google.common.base.Defaults;
2222
import com.google.common.collect.ImmutableList;
23+
import com.google.common.collect.ImmutableListMultimap;
2324
import com.google.common.collect.ImmutableMap;
2425
import com.google.common.collect.Multimap;
2526
import com.google.common.collect.Multimaps;
@@ -45,6 +46,7 @@
4546
import java.util.List;
4647
import java.util.Map;
4748
import java.util.NoSuchElementException;
49+
import java.util.Optional;
4850
import java.util.TreeMap;
4951

5052
/**
@@ -160,6 +162,19 @@ Object getDefaultCelValue(String protoTypeName, String fieldName) {
160162
return toRuntimeValue(defaultValue);
161163
}
162164

165+
@Internal
166+
public Optional<FieldLiteDescriptor> findFieldDescriptor(String protoTypeName, String fieldName) {
167+
return descriptorPool
168+
.findDescriptor(protoTypeName)
169+
.flatMap(desc -> desc.findByFieldName(fieldName));
170+
}
171+
172+
@Internal
173+
public Optional<Object> findDefaultCelValue(String protoTypeName, String fieldName) {
174+
return findFieldDescriptor(protoTypeName, fieldName)
175+
.map(fieldDescriptor -> toRuntimeValue(getDefaultValue(fieldDescriptor)));
176+
}
177+
163178
@Override
164179
@SuppressWarnings("LiteProtoToString") // No alternative identifier to use. Debug only info is OK.
165180
public Object toRuntimeValue(Object value) {
@@ -367,13 +382,16 @@ MessageFields readAllFields(byte[] bytes, String protoTypeName) throws IOExcepti
367382
return MessageFields.create(fieldValues.buildKeepingLast(), unknownFields);
368383
}
369384

385+
MessageFields readMessageFields(MessageLite msg, String protoTypeName) throws IOException {
386+
return readAllFields(msg.toByteArray(), protoTypeName);
387+
}
388+
370389
ImmutableMap<String, Object> readAllFields(MessageLite msg, String protoTypeName)
371390
throws IOException {
372-
return readAllFields(msg.toByteArray(), protoTypeName).values();
391+
return readMessageFields(msg, protoTypeName).values();
373392
}
374393

375-
private static Object readUnknownField(int tagWireType, CodedInputStream inputStream)
376-
throws IOException {
394+
static Object readUnknownField(int tagWireType, CodedInputStream inputStream) throws IOException {
377395
switch (tagWireType) {
378396
case WireFormat.WIRETYPE_VARINT:
379397
return inputStream.readInt64();
@@ -393,16 +411,19 @@ private static Object readUnknownField(int tagWireType, CodedInputStream inputSt
393411
}
394412

395413
@AutoValue
396-
@SuppressWarnings("AutoValueImmutableFields") // Unknowns are inaccessible to users.
414+
@AutoValue.CopyAnnotations
415+
@Immutable
416+
@SuppressWarnings("Immutable") // Safe immutable fields
397417
abstract static class MessageFields {
398418

399419
abstract ImmutableMap<String, Object> values();
400420

401-
abstract Multimap<Integer, Object> unknowns();
421+
abstract ImmutableListMultimap<Integer, Object> unknowns();
402422

403423
static MessageFields create(
404424
ImmutableMap<String, Object> fieldValues, Multimap<Integer, Object> unknownFields) {
405-
return new AutoValue_ProtoLiteCelValueConverter_MessageFields(fieldValues, unknownFields);
425+
return new AutoValue_ProtoLiteCelValueConverter_MessageFields(
426+
fieldValues, ImmutableListMultimap.copyOf(unknownFields));
406427
}
407428
}
408429

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717
import com.google.auto.value.AutoValue;
1818
import com.google.auto.value.extension.memoized.Memoized;
1919
import com.google.common.base.Preconditions;
20+
import com.google.common.collect.ImmutableListMultimap;
2021
import com.google.common.collect.ImmutableMap;
2122
import com.google.errorprone.annotations.Immutable;
2223
import com.google.protobuf.MessageLite;
24+
import dev.cel.common.annotations.Internal;
2325
import dev.cel.common.types.CelType;
2426
import dev.cel.common.types.StructTypeReference;
27+
import dev.cel.common.values.ProtoLiteCelValueConverter.MessageFields;
2528
import java.io.IOException;
2629
import java.util.Optional;
2730

@@ -43,17 +46,27 @@ public abstract class ProtoMessageLiteValue extends StructValue<String, MessageL
4346
@Override
4447
public abstract CelType celType();
4548

46-
abstract ProtoLiteCelValueConverter protoLiteCelValueConverter();
49+
@Internal
50+
public abstract ProtoLiteCelValueConverter protoLiteCelValueConverter();
4751

4852
@Memoized
49-
ImmutableMap<String, Object> fieldValues() {
53+
MessageFields messageFields() {
5054
try {
51-
return protoLiteCelValueConverter().readAllFields(value(), celType().name());
55+
return protoLiteCelValueConverter().readMessageFields(value(), celType().name());
5256
} catch (IOException e) {
5357
throw new IllegalStateException("Unable to read message fields for " + celType().name(), e);
5458
}
5559
}
5660

61+
@Internal
62+
public ImmutableMap<String, Object> fieldValues() {
63+
return messageFields().values();
64+
}
65+
66+
public ImmutableListMultimap<Integer, Object> unknownFields() {
67+
return messageFields().unknowns();
68+
}
69+
5770
@Override
5871
public boolean isZeroValue() {
5972
return value().getDefaultInstanceForType().equals(value());

0 commit comments

Comments
 (0)