Skip to content

Commit

Permalink
Remove ProtoRegistryProvider
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 572628158
  • Loading branch information
l46kok authored and copybara-github committed Oct 17, 2023
1 parent 4d7e58a commit 8220264
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 61 deletions.
1 change: 0 additions & 1 deletion common/src/main/java/dev/cel/common/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ java_library(
DYNAMIC_PROTO_SOURCES = [
"DynamicProto.java",
"ProtoAdapter.java",
"ProtoRegistryProvider.java",
]

java_library(
Expand Down
16 changes: 16 additions & 0 deletions common/src/main/java/dev/cel/common/internal/DynamicProto.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ public static DynamicProto create(ProtoMessageFactory protoMessageFactory) {
this.protoMessageFactory = checkNotNull(protoMessageFactory);
}

/** Attempts to unpack an Any message. */
public Optional<Message> maybeUnpackAny(Message msg) {
try {
Any any =
msg instanceof Any
? (Any) msg
: Any.parseFrom(
msg.toByteString(),
protoMessageFactory.getDescriptorPool().getExtensionRegistry());

return Optional.of(unpack(any));
} catch (InvalidProtocolBufferException e) {
return Optional.empty();
}
}

/**
* Unpack an {@code Any} value to a concrete {@code Message} value.
*
Expand Down
17 changes: 2 additions & 15 deletions common/src/main/java/dev/cel/common/internal/ProtoEquality.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.google.protobuf.ByteString;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Message;
import dev.cel.common.annotations.Internal;
import java.util.List;
Expand Down Expand Up @@ -69,8 +68,8 @@ public boolean equals(Message message1, Message message2) {
// Test whether the typeUrl values are the same, if not return false.
// Use the dynamicProto.unpack(message1), dynamicProto.unpack(message2)
// and assign the results to message1 and message2.
Optional<Message> unpackedAny1 = anyUnpack(message1);
Optional<Message> unpackedAny2 = anyUnpack(message2);
Optional<Message> unpackedAny1 = dynamicProto.maybeUnpackAny(message1);
Optional<Message> unpackedAny2 = dynamicProto.maybeUnpackAny(message2);
if (unpackedAny1.isPresent() && unpackedAny2.isPresent()) {
return equals(unpackedAny1.get(), unpackedAny2.get());
}
Expand Down Expand Up @@ -165,18 +164,6 @@ private ByteString anyValue(Message msg) {
return (ByteString) msg.getField(value);
}

private Optional<Message> anyUnpack(Message msg) {
try {
Any any =
msg instanceof Any
? (Any) msg
: Any.parseFrom(msg.toByteString(), ProtoRegistryProvider.getExtensionRegistry());
return Optional.of(dynamicProto.unpack(any));
} catch (InvalidProtocolBufferException e) {
return Optional.empty();
}
}

private static ProtoMap protoMap(List<?> entries) {
ImmutableMap.Builder<Object, Object> protoMap = ImmutableMap.builder();
FieldDescriptor keyField = null;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ public void maybeAdaptDynamicMessage() throws Exception {
Any any = Any.pack(struct);
DynamicMessage anyDyn =
DynamicMessage.parseFrom(
Struct.getDescriptor(), any.getValue(), ProtoRegistryProvider.getExtensionRegistry());
Struct.getDescriptor(),
any.getValue(),
DefaultDescriptorPool.INSTANCE.getExtensionRegistry());
Message adapted = dynamicProto.maybeAdaptDynamicMessage(anyDyn);
assertThat(adapted).isEqualTo(struct);
assertThat(adapted).isInstanceOf(Struct.class);
Expand All @@ -244,7 +246,9 @@ public void maybeAdaptDynamicMessage_cached() throws Exception {
Any any = Any.pack(struct);
DynamicMessage anyDyn =
DynamicMessage.parseFrom(
Struct.getDescriptor(), any.getValue(), ProtoRegistryProvider.getExtensionRegistry());
Struct.getDescriptor(),
any.getValue(),
DefaultDescriptorPool.INSTANCE.getExtensionRegistry());
Message adapted = dynamicProto.maybeAdaptDynamicMessage(anyDyn);
Message adapted2 = dynamicProto.maybeAdaptDynamicMessage(anyDyn);
assertThat(adapted).isEqualTo(adapted2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,12 @@ public void equalsMessageDynamicAnyFields() throws InvalidProtocolBufferExceptio
DynamicMessage.parseFrom(
Any.getDescriptor(),
doublePackedStruct.getValue(),
ProtoRegistryProvider.getExtensionRegistry());
DefaultDescriptorPool.INSTANCE.getExtensionRegistry());
DynamicMessage dynAny2 =
DynamicMessage.parseFrom(
Any.getDescriptor(),
doublePackedStruct.getValue(),
ProtoRegistryProvider.getExtensionRegistry());
DefaultDescriptorPool.INSTANCE.getExtensionRegistry());
assertThat(protoEquality.equals(dynAny, dynAny2)).isTrue();
}
}
2 changes: 1 addition & 1 deletion testing/src/main/java/dev/cel/testing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ java_library(
":eval",
"//:java_truth",
"//common",
"//common/internal:dynamic_proto",
"//common/internal:cel_descriptor_pools",
"//common/resources/testdata/proto3:standalone_global_enum_java_proto",
"//common/resources/testdata/proto3:test_all_types_java_proto",
"//common/types:cel_types",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import com.google.protobuf.util.Timestamps;
import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.CelProtoAbstractSyntaxTree;
import dev.cel.common.internal.ProtoRegistryProvider;
import dev.cel.common.internal.DefaultDescriptorPool;
import dev.cel.common.types.CelTypes;
import dev.cel.runtime.Activation;
import dev.cel.runtime.InterpreterException;
Expand Down Expand Up @@ -1916,7 +1916,7 @@ public void dynamicMessage() throws Exception {
DynamicMessage.parseFrom(
TestAllTypes.getDescriptor(),
wrapperBindings.toByteArray(),
ProtoRegistryProvider.getExtensionRegistry()));
DefaultDescriptorPool.INSTANCE.getExtensionRegistry()));

declareVariable("msg", CelTypes.createMessage(TestAllTypes.getDescriptor().getFullName()));

Expand Down

0 comments on commit 8220264

Please sign in to comment.