Skip to content

Commit

Permalink
Provide a protocol for GPBExtensionRegistry's lookup support.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
thomasvl committed Sep 16, 2022
1 parent 3c01924 commit 9b8beb2
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 50 deletions.
4 changes: 2 additions & 2 deletions objectivec/GPBCodedInputStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#import <Foundation/Foundation.h>

@class GPBMessage;
@class GPBExtensionRegistry;
@protocol GPBExtensionRegistry;

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -184,7 +184,7 @@ CF_EXTERN_C_END
* extensions for message.
**/
- (void)readMessage:(GPBMessage *)message
extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry;
extensionRegistry:(nullable NSObject<GPBExtensionRegistry> *)extensionRegistry;

/**
* Reads and discards a single field, given its tag value.
Expand Down
6 changes: 3 additions & 3 deletions objectivec/GPBCodedInputStream.m
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ - (NSString *)readString {

- (void)readGroup:(int32_t)fieldNumber
message:(GPBMessage *)message
extensionRegistry:(GPBExtensionRegistry *)extensionRegistry {
extensionRegistry:(NSObject<GPBExtensionRegistry> *)extensionRegistry {
CheckRecursionLimit(&state_);
++state_.recursionDepth;
[message mergeFromCodedInputStream:self extensionRegistry:extensionRegistry];
Expand All @@ -454,7 +454,7 @@ - (void)readUnknownGroup:(int32_t)fieldNumber
}

- (void)readMessage:(GPBMessage *)message
extensionRegistry:(GPBExtensionRegistry *)extensionRegistry {
extensionRegistry:(NSObject<GPBExtensionRegistry> *)extensionRegistry {
CheckRecursionLimit(&state_);
int32_t length = ReadRawVarint32(&state_);
size_t oldLimit = GPBCodedInputStreamPushLimit(&state_, length);
Expand All @@ -466,7 +466,7 @@ - (void)readMessage:(GPBMessage *)message
}

- (void)readMapEntry:(id)mapDictionary
extensionRegistry:(GPBExtensionRegistry *)extensionRegistry
extensionRegistry:(NSObject<GPBExtensionRegistry> *)extensionRegistry
field:(GPBFieldDescriptor *)field
parentMessage:(GPBMessage *)parentMessage {
CheckRecursionLimit(&state_);
Expand Down
4 changes: 2 additions & 2 deletions objectivec/GPBCodedInputStream_PackagePrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ typedef struct GPBCodedInputStreamState {
// support for older data.
- (void)readGroup:(int32_t)fieldNumber
message:(GPBMessage *)message
extensionRegistry:(GPBExtensionRegistry *)extensionRegistry;
extensionRegistry:(NSObject<GPBExtensionRegistry> *)extensionRegistry;

// Reads a group field value from the stream and merges it into the given
// UnknownFieldSet.
Expand All @@ -70,7 +70,7 @@ typedef struct GPBCodedInputStreamState {

// Reads a map entry.
- (void)readMapEntry:(id)mapDictionary
extensionRegistry:(GPBExtensionRegistry *)extensionRegistry
extensionRegistry:(NSObject<GPBExtensionRegistry> *)extensionRegistry
field:(GPBFieldDescriptor *)field
parentMessage:(GPBMessage *)parentMessage;
@end
Expand Down
4 changes: 2 additions & 2 deletions objectivec/GPBDictionary.m
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ BOOL GPBDictionaryIsInitializedInternalHelper(NSDictionary *dict, GPBFieldDescri
static void ReadValue(GPBCodedInputStream *stream,
GPBGenericValue *valueToFill,
GPBDataType type,
GPBExtensionRegistry *registry,
NSObject<GPBExtensionRegistry> *registry,
GPBFieldDescriptor *field) {
switch (type) {
case GPBDataTypeBool:
Expand Down Expand Up @@ -454,7 +454,7 @@ static void ReadValue(GPBCodedInputStream *stream,

void GPBDictionaryReadEntry(id mapDictionary,
GPBCodedInputStream *stream,
GPBExtensionRegistry *registry,
NSObject<GPBExtensionRegistry> *registry,
GPBFieldDescriptor *field,
GPBMessage *parentMessage) {
GPBDataType keyDataType = field.mapKeyDataType;
Expand Down
4 changes: 2 additions & 2 deletions objectivec/GPBDictionary_PackagePrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

@class GPBCodedInputStream;
@class GPBCodedOutputStream;
@class GPBExtensionRegistry;
@protocol GPBExtensionRegistry;
@class GPBFieldDescriptor;

@protocol GPBDictionaryInternalsProtocol
Expand Down Expand Up @@ -493,7 +493,7 @@ BOOL GPBDictionaryIsInitializedInternalHelper(NSDictionary *dict,

// Helper to read a map instead.
void GPBDictionaryReadEntry(id mapDictionary, GPBCodedInputStream *stream,
GPBExtensionRegistry *registry,
NSObject<GPBExtensionRegistry> *registry,
GPBFieldDescriptor *field,
GPBMessage *parentMessage);

Expand Down
4 changes: 2 additions & 2 deletions objectivec/GPBExtensionInternals.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@

@class GPBCodedInputStream;
@class GPBCodedOutputStream;
@class GPBExtensionRegistry;
@protocol GPBExtensionRegistry;

void GPBExtensionMergeFromInputStream(GPBExtensionDescriptor *extension,
BOOL isPackedOnStream,
GPBCodedInputStream *input,
GPBExtensionRegistry *extensionRegistry,
NSObject<GPBExtensionRegistry> *extensionRegistry,
GPBMessage *message);

size_t GPBComputeExtensionSerializedSizeIncludingTag(
Expand Down
6 changes: 3 additions & 3 deletions objectivec/GPBExtensionInternals.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

static id NewSingleValueFromInputStream(GPBExtensionDescriptor *extension,
GPBCodedInputStream *input,
GPBExtensionRegistry *extensionRegistry,
NSObject<GPBExtensionRegistry> *extensionRegistry,
GPBMessage *existingValue)
__attribute__((ns_returns_retained));

Expand Down Expand Up @@ -273,7 +273,7 @@ static void WriteArrayIncludingTagsToCodedOutputStream(
void GPBExtensionMergeFromInputStream(GPBExtensionDescriptor *extension,
BOOL isPackedOnStream,
GPBCodedInputStream *input,
GPBExtensionRegistry *extensionRegistry,
NSObject<GPBExtensionRegistry> *extensionRegistry,
GPBMessage *message) {
GPBExtensionDescription *description = extension->description_;
GPBCodedInputStreamState *state = &input->state_;
Expand Down Expand Up @@ -334,7 +334,7 @@ size_t GPBComputeExtensionSerializedSizeIncludingTag(
// Note that this returns a retained value intentionally.
static id NewSingleValueFromInputStream(GPBExtensionDescriptor *extension,
GPBCodedInputStream *input,
GPBExtensionRegistry *extensionRegistry,
NSObject<GPBExtensionRegistry> *extensionRegistry,
GPBMessage *existingValue) {
GPBExtensionDescription *description = extension->description_;
GPBCodedInputStreamState *state = &input->state_;
Expand Down
32 changes: 19 additions & 13 deletions objectivec/GPBExtensionRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <NSObject>

/**
* 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
Expand All @@ -54,7 +72,7 @@ NS_ASSUME_NONNULL_BEGIN
* MyMessage *msg = [MyMessage parseData:data extensionRegistry:registry error:&parseError];
* ```
**/
@interface GPBExtensionRegistry : NSObject<NSCopying>
@interface GPBExtensionRegistry : NSObject<NSCopying, GPBExtensionRegistry>

/**
* Adds the given GPBExtensionDescriptor to this registry.
Expand All @@ -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
14 changes: 7 additions & 7 deletions objectivec/GPBMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
@class GPBCodedInputStream;
@class GPBCodedOutputStream;
@class GPBExtensionDescriptor;
@class GPBExtensionRegistry;
@protocol GPBExtensionRegistry;
@class GPBFieldDescriptor;
@class GPBUnknownFieldSet;

Expand Down Expand Up @@ -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 NSObject<GPBExtensionRegistry> *)extensionRegistry
error:(NSError **)errorPtr;

/**
Expand All @@ -171,7 +171,7 @@ CF_EXTERN_C_END
**/
+ (nullable instancetype)parseFromCodedInputStream:(GPBCodedInputStream *)input
extensionRegistry:
(nullable GPBExtensionRegistry *)extensionRegistry
(nullable NSObject<GPBExtensionRegistry> *)extensionRegistry
error:(NSError **)errorPtr;

/**
Expand All @@ -196,7 +196,7 @@ CF_EXTERN_C_END
**/
+ (nullable instancetype)parseDelimitedFromCodedInputStream:(GPBCodedInputStream *)input
extensionRegistry:
(nullable GPBExtensionRegistry *)extensionRegistry
(nullable NSObject<GPBExtensionRegistry> *)extensionRegistry
error:(NSError **)errorPtr;

/**
Expand Down Expand Up @@ -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 NSObject<GPBExtensionRegistry> *)extensionRegistry
error:(NSError **)errorPtr;

/**
Expand All @@ -264,7 +264,7 @@ CF_EXTERN_C_END
**/
- (nullable instancetype)initWithCodedInputStream:(GPBCodedInputStream *)input
extensionRegistry:
(nullable GPBExtensionRegistry *)extensionRegistry
(nullable NSObject<GPBExtensionRegistry> *)extensionRegistry
error:(NSError **)errorPtr;

/**
Expand All @@ -278,7 +278,7 @@ CF_EXTERN_C_END
* unsuccessful.
**/
- (void)mergeFromData:(NSData *)data
extensionRegistry:(nullable GPBExtensionRegistry *)extensionRegistry;
extensionRegistry:(nullable NSObject<GPBExtensionRegistry> *)extensionRegistry;

/**
* Merges the fields from another message (of the same type) into this
Expand Down
24 changes: 12 additions & 12 deletions objectivec/GPBMessage.m
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ - (instancetype)initWithData:(NSData *)data error:(NSError **)errorPtr {
}

- (instancetype)initWithData:(NSData *)data
extensionRegistry:(GPBExtensionRegistry *)extensionRegistry
extensionRegistry:(NSObject<GPBExtensionRegistry> *)extensionRegistry
error:(NSError **)errorPtr {
if ((self = [self init])) {
@try {
Expand Down Expand Up @@ -912,7 +912,7 @@ - (instancetype)initWithData:(NSData *)data

- (instancetype)initWithCodedInputStream:(GPBCodedInputStream *)input
extensionRegistry:
(GPBExtensionRegistry *)extensionRegistry
(NSObject<GPBExtensionRegistry> *)extensionRegistry
error:(NSError **)errorPtr {
if ((self = [self init])) {
@try {
Expand Down Expand Up @@ -1973,7 +1973,7 @@ - (void)clearExtension:(GPBExtensionDescriptor *)extension {
#pragma mark - mergeFrom

- (void)mergeFromData:(NSData *)data
extensionRegistry:(GPBExtensionRegistry *)extensionRegistry {
extensionRegistry:(NSObject<GPBExtensionRegistry> *)extensionRegistry {
GPBCodedInputStream *input = [[GPBCodedInputStream alloc] initWithData:data];
[self mergeFromCodedInputStream:input extensionRegistry:extensionRegistry];
[input checkLastTagWas:0];
Expand All @@ -1983,7 +1983,7 @@ - (void)mergeFromData:(NSData *)data
#pragma mark - mergeDelimitedFrom

- (void)mergeDelimitedFromCodedInputStream:(GPBCodedInputStream *)input
extensionRegistry:(GPBExtensionRegistry *)extensionRegistry {
extensionRegistry:(NSObject<GPBExtensionRegistry> *)extensionRegistry {
GPBCodedInputStreamState *state = &input->state_;
if (GPBCodedInputStreamIsAtEnd(state)) {
return;
Expand All @@ -2003,15 +2003,15 @@ + (instancetype)parseFromData:(NSData *)data error:(NSError **)errorPtr {
}

+ (instancetype)parseFromData:(NSData *)data
extensionRegistry:(GPBExtensionRegistry *)extensionRegistry
extensionRegistry:(NSObject<GPBExtensionRegistry> *)extensionRegistry
error:(NSError **)errorPtr {
return [[[self alloc] initWithData:data
extensionRegistry:extensionRegistry
error:errorPtr] autorelease];
}

+ (instancetype)parseFromCodedInputStream:(GPBCodedInputStream *)input
extensionRegistry:(GPBExtensionRegistry *)extensionRegistry
extensionRegistry:(NSObject<GPBExtensionRegistry> *)extensionRegistry
error:(NSError **)errorPtr {
return
[[[self alloc] initWithCodedInputStream:input
Expand All @@ -2023,7 +2023,7 @@ + (instancetype)parseFromCodedInputStream:(GPBCodedInputStream *)input

+ (instancetype)parseDelimitedFromCodedInputStream:(GPBCodedInputStream *)input
extensionRegistry:
(GPBExtensionRegistry *)extensionRegistry
(NSObject<GPBExtensionRegistry> *)extensionRegistry
error:(NSError **)errorPtr {
GPBMessage *message = [[[self alloc] init] autorelease];
@try {
Expand Down Expand Up @@ -2065,7 +2065,7 @@ - (void)setUnknownFields:(GPBUnknownFieldSet *)unknownFields {
}

- (void)parseMessageSet:(GPBCodedInputStream *)input
extensionRegistry:(GPBExtensionRegistry *)extensionRegistry {
extensionRegistry:(NSObject<GPBExtensionRegistry> *)extensionRegistry {
uint32_t typeId = 0;
NSData *rawBytes = nil;
GPBExtensionDescriptor *extension = nil;
Expand Down Expand Up @@ -2117,7 +2117,7 @@ - (void)parseMessageSet:(GPBCodedInputStream *)input
}

- (BOOL)parseUnknownField:(GPBCodedInputStream *)input
extensionRegistry:(GPBExtensionRegistry *)extensionRegistry
extensionRegistry:(NSObject<GPBExtensionRegistry> *)extensionRegistry
tag:(uint32_t)tag {
GPBWireFormat wireType = GPBWireFormatGetTagWireType(tag);
int32_t fieldNumber = GPBWireFormatGetTagFieldNumber(tag);
Expand Down Expand Up @@ -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, NSObject<GPBExtensionRegistry> *extensionRegistry) {
GPBDataType fieldDataType = GPBGetFieldDataType(field);
switch (fieldDataType) {
#define CASE_SINGLE_POD(NAME, TYPE, FUNC_TYPE) \
Expand Down Expand Up @@ -2306,7 +2306,7 @@ static void MergeRepeatedPackedFieldFromCodedInputStream(

static void MergeRepeatedNotPackedFieldFromCodedInputStream(
GPBMessage *self, GPBFieldDescriptor *field, GPBFileSyntax syntax,
GPBCodedInputStream *input, GPBExtensionRegistry *extensionRegistry) {
GPBCodedInputStream *input, NSObject<GPBExtensionRegistry> *extensionRegistry) {
GPBCodedInputStreamState *state = &input->state_;
id genericArray = GetOrCreateArrayIvarWithField(self, field);
switch (GPBGetFieldDataType(field)) {
Expand Down Expand Up @@ -2371,7 +2371,7 @@ static void MergeRepeatedNotPackedFieldFromCodedInputStream(
}

- (void)mergeFromCodedInputStream:(GPBCodedInputStream *)input
extensionRegistry:(GPBExtensionRegistry *)extensionRegistry {
extensionRegistry:(NSObject<GPBExtensionRegistry> *)extensionRegistry {
GPBDescriptor *descriptor = [self descriptor];
GPBFileSyntax syntax = descriptor.file.syntax;
GPBCodedInputStreamState *state = &input->state_;
Expand Down
4 changes: 2 additions & 2 deletions objectivec/GPBMessage_PackagePrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:(NSObject<GPBExtensionRegistry> *)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;
(NSObject<GPBExtensionRegistry> *)extensionRegistry;

- (void)addUnknownMapEntry:(int32_t)fieldNum value:(NSData *)data;

Expand Down

0 comments on commit 9b8beb2

Please sign in to comment.