From 3c3aa797792e3b98ec371d28591624b45aea2013 Mon Sep 17 00:00:00 2001 From: Sokwhan Huh Date: Tue, 17 Oct 2023 11:31:26 -0700 Subject: [PATCH] Remove ProtoRegistryProvider PiperOrigin-RevId: 574214515 --- .../java/dev/cel/common/internal/BUILD.bazel | 1 - .../dev/cel/common/internal/DynamicProto.java | 16 ++++++++ .../cel/common/internal/ProtoEquality.java | 17 +-------- .../internal/ProtoRegistryProvider.java | 38 ------------------- .../cel/common/internal/DynamicProtoTest.java | 8 +++- .../common/internal/ProtoEqualityTest.java | 4 +- .../src/main/java/dev/cel/testing/BUILD.bazel | 2 +- .../dev/cel/testing/BaseInterpreterTest.java | 4 +- 8 files changed, 29 insertions(+), 61 deletions(-) delete mode 100644 common/src/main/java/dev/cel/common/internal/ProtoRegistryProvider.java diff --git a/common/src/main/java/dev/cel/common/internal/BUILD.bazel b/common/src/main/java/dev/cel/common/internal/BUILD.bazel index 715185e3..8219aa73 100644 --- a/common/src/main/java/dev/cel/common/internal/BUILD.bazel +++ b/common/src/main/java/dev/cel/common/internal/BUILD.bazel @@ -94,7 +94,6 @@ java_library( DYNAMIC_PROTO_SOURCES = [ "DynamicProto.java", "ProtoAdapter.java", - "ProtoRegistryProvider.java", ] java_library( diff --git a/common/src/main/java/dev/cel/common/internal/DynamicProto.java b/common/src/main/java/dev/cel/common/internal/DynamicProto.java index 98e7081c..0cb9eb32 100644 --- a/common/src/main/java/dev/cel/common/internal/DynamicProto.java +++ b/common/src/main/java/dev/cel/common/internal/DynamicProto.java @@ -46,6 +46,22 @@ public static DynamicProto create(ProtoMessageFactory protoMessageFactory) { this.protoMessageFactory = checkNotNull(protoMessageFactory); } + /** Attempts to unpack an Any message. */ + public Optional 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. * diff --git a/common/src/main/java/dev/cel/common/internal/ProtoEquality.java b/common/src/main/java/dev/cel/common/internal/ProtoEquality.java index 0337894d..57130b7b 100644 --- a/common/src/main/java/dev/cel/common/internal/ProtoEquality.java +++ b/common/src/main/java/dev/cel/common/internal/ProtoEquality.java @@ -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; @@ -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 unpackedAny1 = anyUnpack(message1); - Optional unpackedAny2 = anyUnpack(message2); + Optional unpackedAny1 = dynamicProto.maybeUnpackAny(message1); + Optional unpackedAny2 = dynamicProto.maybeUnpackAny(message2); if (unpackedAny1.isPresent() && unpackedAny2.isPresent()) { return equals(unpackedAny1.get(), unpackedAny2.get()); } @@ -165,18 +164,6 @@ private ByteString anyValue(Message msg) { return (ByteString) msg.getField(value); } - private Optional 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 protoMap = ImmutableMap.builder(); FieldDescriptor keyField = null; diff --git a/common/src/main/java/dev/cel/common/internal/ProtoRegistryProvider.java b/common/src/main/java/dev/cel/common/internal/ProtoRegistryProvider.java deleted file mode 100644 index 72756f59..00000000 --- a/common/src/main/java/dev/cel/common/internal/ProtoRegistryProvider.java +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2022 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package dev.cel.common.internal; - -import com.google.protobuf.ExtensionRegistry; -import com.google.protobuf.TypeRegistry; -import dev.cel.common.annotations.Internal; - -/** - * ProtoRegistryProvider provides Extension and Type registries for handling Protobuf.Any messages. - * - *

CEL Library Internals. Do Not Use. - */ -@Internal -public final class ProtoRegistryProvider { - - public static ExtensionRegistry getExtensionRegistry() { - return ExtensionRegistry.getEmptyRegistry(); - } - - static TypeRegistry getTypeRegistry() { - return TypeRegistry.getEmptyTypeRegistry(); - } - - private ProtoRegistryProvider() {} -} diff --git a/common/src/test/java/dev/cel/common/internal/DynamicProtoTest.java b/common/src/test/java/dev/cel/common/internal/DynamicProtoTest.java index 39afd25a..7198fa04 100644 --- a/common/src/test/java/dev/cel/common/internal/DynamicProtoTest.java +++ b/common/src/test/java/dev/cel/common/internal/DynamicProtoTest.java @@ -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); @@ -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); diff --git a/common/src/test/java/dev/cel/common/internal/ProtoEqualityTest.java b/common/src/test/java/dev/cel/common/internal/ProtoEqualityTest.java index 4a65bb03..6a4b9110 100644 --- a/common/src/test/java/dev/cel/common/internal/ProtoEqualityTest.java +++ b/common/src/test/java/dev/cel/common/internal/ProtoEqualityTest.java @@ -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(); } } diff --git a/testing/src/main/java/dev/cel/testing/BUILD.bazel b/testing/src/main/java/dev/cel/testing/BUILD.bazel index 2aaa4198..1ffb77b4 100644 --- a/testing/src/main/java/dev/cel/testing/BUILD.bazel +++ b/testing/src/main/java/dev/cel/testing/BUILD.bazel @@ -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", diff --git a/testing/src/main/java/dev/cel/testing/BaseInterpreterTest.java b/testing/src/main/java/dev/cel/testing/BaseInterpreterTest.java index 34ae763b..4f7ba5b3 100644 --- a/testing/src/main/java/dev/cel/testing/BaseInterpreterTest.java +++ b/testing/src/main/java/dev/cel/testing/BaseInterpreterTest.java @@ -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; @@ -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()));