From f191ab0f35432c76fff0763509a4cc6439183d49 Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Thu, 15 Sep 2022 14:40:15 -0400 Subject: [PATCH] Provide a protocol for GPBExtensionRegistry's lookup support. This allows some to use an alternative registry if they have a different implementation. This is really just wiring though the change to use the GPBExtensionRegistry protocol vs the concrete GPBExtensionRegistry through the other apis. --- objectivec/GPBCodedInputStream.h | 4 +-- objectivec/GPBCodedInputStream.m | 6 ++-- .../GPBCodedInputStream_PackagePrivate.h | 4 +-- objectivec/GPBDictionary.m | 4 +-- objectivec/GPBDictionary_PackagePrivate.h | 4 +-- objectivec/GPBExtensionInternals.h | 4 +-- objectivec/GPBExtensionInternals.m | 6 ++-- objectivec/GPBExtensionRegistry.h | 32 +++++++++++-------- objectivec/GPBMessage.h | 14 ++++---- objectivec/GPBMessage.m | 24 +++++++------- objectivec/GPBMessage_PackagePrivate.h | 4 +-- 11 files changed, 56 insertions(+), 50 deletions(-) diff --git a/objectivec/GPBCodedInputStream.h b/objectivec/GPBCodedInputStream.h index 1886ccf61b6b..73b65581d798 100644 --- a/objectivec/GPBCodedInputStream.h +++ b/objectivec/GPBCodedInputStream.h @@ -31,7 +31,7 @@ #import @class GPBMessage; -@class GPBExtensionRegistry; +@protocol GPBExtensionRegistry; NS_ASSUME_NONNULL_BEGIN @@ -184,7 +184,7 @@ CF_EXTERN_C_END * extensions for message. **/ - (void)readMessage:(GPBMessage *)message - extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry; + extensionRegistry:(nullable id)extensionRegistry; /** * Reads and discards a single field, given its tag value. diff --git a/objectivec/GPBCodedInputStream.m b/objectivec/GPBCodedInputStream.m index c459391e08dc..25dde75320b6 100644 --- a/objectivec/GPBCodedInputStream.m +++ b/objectivec/GPBCodedInputStream.m @@ -434,7 +434,7 @@ - (NSString *)readString { - (void)readGroup:(int32_t)fieldNumber message:(GPBMessage *)message - extensionRegistry:(GPBExtensionRegistry *)extensionRegistry { + extensionRegistry:(id)extensionRegistry { CheckRecursionLimit(&state_); ++state_.recursionDepth; [message mergeFromCodedInputStream:self extensionRegistry:extensionRegistry]; @@ -454,7 +454,7 @@ - (void)readUnknownGroup:(int32_t)fieldNumber } - (void)readMessage:(GPBMessage *)message - extensionRegistry:(GPBExtensionRegistry *)extensionRegistry { + extensionRegistry:(id)extensionRegistry { CheckRecursionLimit(&state_); int32_t length = ReadRawVarint32(&state_); size_t oldLimit = GPBCodedInputStreamPushLimit(&state_, length); @@ -466,7 +466,7 @@ - (void)readMessage:(GPBMessage *)message } - (void)readMapEntry:(id)mapDictionary - extensionRegistry:(GPBExtensionRegistry *)extensionRegistry + extensionRegistry:(id)extensionRegistry field:(GPBFieldDescriptor *)field parentMessage:(GPBMessage *)parentMessage { CheckRecursionLimit(&state_); diff --git a/objectivec/GPBCodedInputStream_PackagePrivate.h b/objectivec/GPBCodedInputStream_PackagePrivate.h index 43ec6e79b2f3..cdfb0dcc3fed 100644 --- a/objectivec/GPBCodedInputStream_PackagePrivate.h +++ b/objectivec/GPBCodedInputStream_PackagePrivate.h @@ -61,7 +61,7 @@ typedef struct GPBCodedInputStreamState { // support for older data. - (void)readGroup:(int32_t)fieldNumber message:(GPBMessage *)message - extensionRegistry:(GPBExtensionRegistry *)extensionRegistry; + extensionRegistry:(id)extensionRegistry; // Reads a group field value from the stream and merges it into the given // UnknownFieldSet. @@ -70,7 +70,7 @@ typedef struct GPBCodedInputStreamState { // Reads a map entry. - (void)readMapEntry:(id)mapDictionary - extensionRegistry:(GPBExtensionRegistry *)extensionRegistry + extensionRegistry:(id)extensionRegistry field:(GPBFieldDescriptor *)field parentMessage:(GPBMessage *)parentMessage; @end diff --git a/objectivec/GPBDictionary.m b/objectivec/GPBDictionary.m index 187a97092fbb..77642c27f9be 100644 --- a/objectivec/GPBDictionary.m +++ b/objectivec/GPBDictionary.m @@ -386,7 +386,7 @@ BOOL GPBDictionaryIsInitializedInternalHelper(NSDictionary *dict, GPBFieldDescri static void ReadValue(GPBCodedInputStream *stream, GPBGenericValue *valueToFill, GPBDataType type, - GPBExtensionRegistry *registry, + idregistry, GPBFieldDescriptor *field) { switch (type) { case GPBDataTypeBool: @@ -454,7 +454,7 @@ static void ReadValue(GPBCodedInputStream *stream, void GPBDictionaryReadEntry(id mapDictionary, GPBCodedInputStream *stream, - GPBExtensionRegistry *registry, + idregistry, GPBFieldDescriptor *field, GPBMessage *parentMessage) { GPBDataType keyDataType = field.mapKeyDataType; diff --git a/objectivec/GPBDictionary_PackagePrivate.h b/objectivec/GPBDictionary_PackagePrivate.h index 15e428326415..d494b7ee8845 100644 --- a/objectivec/GPBDictionary_PackagePrivate.h +++ b/objectivec/GPBDictionary_PackagePrivate.h @@ -34,7 +34,7 @@ @class GPBCodedInputStream; @class GPBCodedOutputStream; -@class GPBExtensionRegistry; +@protocol GPBExtensionRegistry; @class GPBFieldDescriptor; @protocol GPBDictionaryInternalsProtocol @@ -493,7 +493,7 @@ BOOL GPBDictionaryIsInitializedInternalHelper(NSDictionary *dict, // Helper to read a map instead. void GPBDictionaryReadEntry(id mapDictionary, GPBCodedInputStream *stream, - GPBExtensionRegistry *registry, + idregistry, GPBFieldDescriptor *field, GPBMessage *parentMessage); diff --git a/objectivec/GPBExtensionInternals.h b/objectivec/GPBExtensionInternals.h index 2b980aefa4c4..854b57fe8949 100644 --- a/objectivec/GPBExtensionInternals.h +++ b/objectivec/GPBExtensionInternals.h @@ -34,12 +34,12 @@ @class GPBCodedInputStream; @class GPBCodedOutputStream; -@class GPBExtensionRegistry; +@protocol GPBExtensionRegistry; void GPBExtensionMergeFromInputStream(GPBExtensionDescriptor *extension, BOOL isPackedOnStream, GPBCodedInputStream *input, - GPBExtensionRegistry *extensionRegistry, + idextensionRegistry, GPBMessage *message); size_t GPBComputeExtensionSerializedSizeIncludingTag( diff --git a/objectivec/GPBExtensionInternals.m b/objectivec/GPBExtensionInternals.m index bacec5740a50..b74591e0a090 100644 --- a/objectivec/GPBExtensionInternals.m +++ b/objectivec/GPBExtensionInternals.m @@ -40,7 +40,7 @@ static id NewSingleValueFromInputStream(GPBExtensionDescriptor *extension, GPBCodedInputStream *input, - GPBExtensionRegistry *extensionRegistry, + idextensionRegistry, GPBMessage *existingValue) __attribute__((ns_returns_retained)); @@ -273,7 +273,7 @@ static void WriteArrayIncludingTagsToCodedOutputStream( void GPBExtensionMergeFromInputStream(GPBExtensionDescriptor *extension, BOOL isPackedOnStream, GPBCodedInputStream *input, - GPBExtensionRegistry *extensionRegistry, + idextensionRegistry, GPBMessage *message) { GPBExtensionDescription *description = extension->description_; GPBCodedInputStreamState *state = &input->state_; @@ -334,7 +334,7 @@ size_t GPBComputeExtensionSerializedSizeIncludingTag( // Note that this returns a retained value intentionally. static id NewSingleValueFromInputStream(GPBExtensionDescriptor *extension, GPBCodedInputStream *input, - GPBExtensionRegistry *extensionRegistry, + idextensionRegistry, GPBMessage *existingValue) { GPBExtensionDescription *description = extension->description_; GPBCodedInputStreamState *state = &input->state_; diff --git a/objectivec/GPBExtensionRegistry.h b/objectivec/GPBExtensionRegistry.h index d79632d28b07..b1850568d994 100644 --- a/objectivec/GPBExtensionRegistry.h +++ b/objectivec/GPBExtensionRegistry.h @@ -41,6 +41,24 @@ NS_ASSUME_NONNULL_BEGIN * GPBExtensionRegistry in which you have registered any extensions that you * want to be able to parse. Otherwise, those extensions will just be treated * like unknown fields. + **/ +@protocol GPBExtensionRegistry + +/** + * Looks for the extension registered for the given field number on a given + * GPBDescriptor. + * + * @param descriptor The descriptor to look for a registered extension on. + * @param fieldNumber The field number of the extension to look for. + * + * @return The registered GPBExtensionDescriptor or nil if none was found. + **/ +- (nullable GPBExtensionDescriptor *)extensionForDescriptor:(GPBDescriptor *)descriptor + fieldNumber:(NSInteger)fieldNumber; +@end + +/** + * A concrete implementation of `GPBExtensionRegistry`. * * The *Root classes provide `+extensionRegistry` for the extensions defined * in a given file *and* all files it imports. You can also create a @@ -54,7 +72,7 @@ NS_ASSUME_NONNULL_BEGIN * MyMessage *msg = [MyMessage parseData:data extensionRegistry:registry error:&parseError]; * ``` **/ -@interface GPBExtensionRegistry : NSObject +@interface GPBExtensionRegistry : NSObject /** * Adds the given GPBExtensionDescriptor to this registry. @@ -70,18 +88,6 @@ NS_ASSUME_NONNULL_BEGIN **/ - (void)addExtensions:(GPBExtensionRegistry *)registry; -/** - * Looks for the extension registered for the given field number on a given - * GPBDescriptor. - * - * @param descriptor The descriptor to look for a registered extension on. - * @param fieldNumber The field number of the extension to look for. - * - * @return The registered GPBExtensionDescriptor or nil if none was found. - **/ -- (nullable GPBExtensionDescriptor *)extensionForDescriptor:(GPBDescriptor *)descriptor - fieldNumber:(NSInteger)fieldNumber; - @end NS_ASSUME_NONNULL_END diff --git a/objectivec/GPBMessage.h b/objectivec/GPBMessage.h index 01253a4afc99..990a796faf8c 100644 --- a/objectivec/GPBMessage.h +++ b/objectivec/GPBMessage.h @@ -36,7 +36,7 @@ @class GPBCodedInputStream; @class GPBCodedOutputStream; @class GPBExtensionDescriptor; -@class GPBExtensionRegistry; +@protocol GPBExtensionRegistry; @class GPBFieldDescriptor; @class GPBUnknownFieldSet; @@ -147,7 +147,7 @@ CF_EXTERN_C_END * @return A new instance of the generated class. **/ + (nullable instancetype)parseFromData:(NSData *)data - extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry + extensionRegistry:(nullable id)extensionRegistry error:(NSError **)errorPtr; /** @@ -171,7 +171,7 @@ CF_EXTERN_C_END **/ + (nullable instancetype)parseFromCodedInputStream:(GPBCodedInputStream *)input extensionRegistry: - (nullable GPBExtensionRegistry *)extensionRegistry + (nullable id)extensionRegistry error:(NSError **)errorPtr; /** @@ -196,7 +196,7 @@ CF_EXTERN_C_END **/ + (nullable instancetype)parseDelimitedFromCodedInputStream:(GPBCodedInputStream *)input extensionRegistry: - (nullable GPBExtensionRegistry *)extensionRegistry + (nullable id)extensionRegistry error:(NSError **)errorPtr; /** @@ -239,7 +239,7 @@ CF_EXTERN_C_END * @return An initialized instance of the generated class. **/ - (nullable instancetype)initWithData:(NSData *)data - extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry + extensionRegistry:(nullable id)extensionRegistry error:(NSError **)errorPtr; /** @@ -264,7 +264,7 @@ CF_EXTERN_C_END **/ - (nullable instancetype)initWithCodedInputStream:(GPBCodedInputStream *)input extensionRegistry: - (nullable GPBExtensionRegistry *)extensionRegistry + (nullable id)extensionRegistry error:(NSError **)errorPtr; /** @@ -278,7 +278,7 @@ CF_EXTERN_C_END * unsuccessful. **/ - (void)mergeFromData:(NSData *)data - extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry; + extensionRegistry:(nullable id)extensionRegistry; /** * Merges the fields from another message (of the same type) into this diff --git a/objectivec/GPBMessage.m b/objectivec/GPBMessage.m index ee94dee8ed4d..b29360460802 100644 --- a/objectivec/GPBMessage.m +++ b/objectivec/GPBMessage.m @@ -881,7 +881,7 @@ - (instancetype)initWithData:(NSData *)data error:(NSError **)errorPtr { } - (instancetype)initWithData:(NSData *)data - extensionRegistry:(GPBExtensionRegistry *)extensionRegistry + extensionRegistry:(id)extensionRegistry error:(NSError **)errorPtr { if ((self = [self init])) { @try { @@ -912,7 +912,7 @@ - (instancetype)initWithData:(NSData *)data - (instancetype)initWithCodedInputStream:(GPBCodedInputStream *)input extensionRegistry: - (GPBExtensionRegistry *)extensionRegistry + (id)extensionRegistry error:(NSError **)errorPtr { if ((self = [self init])) { @try { @@ -1973,7 +1973,7 @@ - (void)clearExtension:(GPBExtensionDescriptor *)extension { #pragma mark - mergeFrom - (void)mergeFromData:(NSData *)data - extensionRegistry:(GPBExtensionRegistry *)extensionRegistry { + extensionRegistry:(id)extensionRegistry { GPBCodedInputStream *input = [[GPBCodedInputStream alloc] initWithData:data]; [self mergeFromCodedInputStream:input extensionRegistry:extensionRegistry]; [input checkLastTagWas:0]; @@ -1983,7 +1983,7 @@ - (void)mergeFromData:(NSData *)data #pragma mark - mergeDelimitedFrom - (void)mergeDelimitedFromCodedInputStream:(GPBCodedInputStream *)input - extensionRegistry:(GPBExtensionRegistry *)extensionRegistry { + extensionRegistry:(id)extensionRegistry { GPBCodedInputStreamState *state = &input->state_; if (GPBCodedInputStreamIsAtEnd(state)) { return; @@ -2003,7 +2003,7 @@ + (instancetype)parseFromData:(NSData *)data error:(NSError **)errorPtr { } + (instancetype)parseFromData:(NSData *)data - extensionRegistry:(GPBExtensionRegistry *)extensionRegistry + extensionRegistry:(id)extensionRegistry error:(NSError **)errorPtr { return [[[self alloc] initWithData:data extensionRegistry:extensionRegistry @@ -2011,7 +2011,7 @@ + (instancetype)parseFromData:(NSData *)data } + (instancetype)parseFromCodedInputStream:(GPBCodedInputStream *)input - extensionRegistry:(GPBExtensionRegistry *)extensionRegistry + extensionRegistry:(id)extensionRegistry error:(NSError **)errorPtr { return [[[self alloc] initWithCodedInputStream:input @@ -2023,7 +2023,7 @@ + (instancetype)parseFromCodedInputStream:(GPBCodedInputStream *)input + (instancetype)parseDelimitedFromCodedInputStream:(GPBCodedInputStream *)input extensionRegistry: - (GPBExtensionRegistry *)extensionRegistry + (id)extensionRegistry error:(NSError **)errorPtr { GPBMessage *message = [[[self alloc] init] autorelease]; @try { @@ -2065,7 +2065,7 @@ - (void)setUnknownFields:(GPBUnknownFieldSet *)unknownFields { } - (void)parseMessageSet:(GPBCodedInputStream *)input - extensionRegistry:(GPBExtensionRegistry *)extensionRegistry { + extensionRegistry:(id)extensionRegistry { uint32_t typeId = 0; NSData *rawBytes = nil; GPBExtensionDescriptor *extension = nil; @@ -2117,7 +2117,7 @@ - (void)parseMessageSet:(GPBCodedInputStream *)input } - (BOOL)parseUnknownField:(GPBCodedInputStream *)input - extensionRegistry:(GPBExtensionRegistry *)extensionRegistry + extensionRegistry:(id)extensionRegistry tag:(uint32_t)tag { GPBWireFormat wireType = GPBWireFormatGetTagWireType(tag); int32_t fieldNumber = GPBWireFormatGetTagFieldNumber(tag); @@ -2170,7 +2170,7 @@ - (void)addUnknownMapEntry:(int32_t)fieldNum value:(NSData *)data { static void MergeSingleFieldFromCodedInputStream( GPBMessage *self, GPBFieldDescriptor *field, GPBFileSyntax syntax, - GPBCodedInputStream *input, GPBExtensionRegistry *extensionRegistry) { + GPBCodedInputStream *input, idextensionRegistry) { GPBDataType fieldDataType = GPBGetFieldDataType(field); switch (fieldDataType) { #define CASE_SINGLE_POD(NAME, TYPE, FUNC_TYPE) \ @@ -2306,7 +2306,7 @@ static void MergeRepeatedPackedFieldFromCodedInputStream( static void MergeRepeatedNotPackedFieldFromCodedInputStream( GPBMessage *self, GPBFieldDescriptor *field, GPBFileSyntax syntax, - GPBCodedInputStream *input, GPBExtensionRegistry *extensionRegistry) { + GPBCodedInputStream *input, idextensionRegistry) { GPBCodedInputStreamState *state = &input->state_; id genericArray = GetOrCreateArrayIvarWithField(self, field); switch (GPBGetFieldDataType(field)) { @@ -2371,7 +2371,7 @@ static void MergeRepeatedNotPackedFieldFromCodedInputStream( } - (void)mergeFromCodedInputStream:(GPBCodedInputStream *)input - extensionRegistry:(GPBExtensionRegistry *)extensionRegistry { + extensionRegistry:(id)extensionRegistry { GPBDescriptor *descriptor = [self descriptor]; GPBFileSyntax syntax = descriptor.file.syntax; GPBCodedInputStreamState *state = &input->state_; diff --git a/objectivec/GPBMessage_PackagePrivate.h b/objectivec/GPBMessage_PackagePrivate.h index ca10983b3c2c..a8755a29ce07 100644 --- a/objectivec/GPBMessage_PackagePrivate.h +++ b/objectivec/GPBMessage_PackagePrivate.h @@ -78,13 +78,13 @@ typedef struct GPBMessage_Storage *GPBMessage_StoragePtr; // or zero for EOF. // NOTE: This will throw if there is an error while parsing. - (void)mergeFromCodedInputStream:(GPBCodedInputStream *)input - extensionRegistry:(GPBExtensionRegistry *)extensionRegistry; + extensionRegistry:(id)extensionRegistry; // Parses the next delimited message of this type from the input and merges it // with this message. - (void)mergeDelimitedFromCodedInputStream:(GPBCodedInputStream *)input extensionRegistry: - (GPBExtensionRegistry *)extensionRegistry; + (id)extensionRegistry; - (void)addUnknownMapEntry:(int32_t)fieldNum value:(NSData *)data;