[ObjC] Add -[GPBMessage mergeFromData:extensionRegistry:error:]

This adds a merge method that doesn't throw, making it generally more usable and allowing usage directly from Swift.

PiperOrigin-RevId: 510490798
pull/11993/head
Protobuf Team Bot 2 years ago committed by Copybara-Service
parent df455a33fd
commit b4370b2e76
  1. 16
      objectivec/GPBMessage.h
  2. 36
      objectivec/GPBMessage.m

@ -278,6 +278,22 @@ CF_EXTERN_C_END
- (void)mergeFromData:(NSData *)data
extensionRegistry:(nullable id<GPBExtensionRegistry>)extensionRegistry;
/**
* Parses the given data as this message's class, and merges those values into
* this message.
*
* @param data The binary representation of the message to merge.
* @param extensionRegistry The extension registry to use to look up extensions.
* @param errorPtr An optional error pointer to fill in with a failure
* reason if the data can not be parsed. Will only be
* filled in if the data failed to be parsed.
*
* @return Boolean indicating success. errorPtr will only be fill in on failure.
**/
- (BOOL)mergeFromData:(NSData *)data
extensionRegistry:(nullable id<GPBExtensionRegistry>)extensionRegistry
error:(NSError **)errorPtr;
/**
* Merges the fields from another message (of the same type) into this
* message.

@ -965,27 +965,18 @@ static GPBUnknownFieldSet *GetOrMakeUnknownFields(GPBMessage *self) {
extensionRegistry:(id<GPBExtensionRegistry>)extensionRegistry
error:(NSError **)errorPtr {
if ((self = [self init])) {
@try {
[self mergeFromData:data extensionRegistry:extensionRegistry];
if (errorPtr) {
*errorPtr = nil;
}
} @catch (NSException *exception) {
if (![self mergeFromData:data extensionRegistry:extensionRegistry error:errorPtr]) {
[self release];
self = nil;
if (errorPtr) {
*errorPtr = ErrorFromException(exception);
}
}
#ifdef DEBUG
if (self && !self.initialized) {
} else if (!self.initialized) {
[self release];
self = nil;
if (errorPtr) {
*errorPtr = MessageError(GPBMessageErrorCodeMissingRequiredField, nil);
}
}
#endif
}
}
return self;
}
@ -2004,6 +1995,27 @@ static GPBUnknownFieldSet *GetOrMakeUnknownFields(GPBMessage *self) {
}
}
- (BOOL)mergeFromData:(NSData *)data
extensionRegistry:(nullable id<GPBExtensionRegistry>)extensionRegistry
error:(NSError **)errorPtr {
GPBCodedInputStream *input = [[GPBCodedInputStream alloc] initWithData:data];
@try {
[self mergeFromCodedInputStream:input extensionRegistry:extensionRegistry];
[input checkLastTagWas:0];
if (errorPtr) {
*errorPtr = nil;
}
} @catch (NSException *exception) {
[input release];
if (errorPtr) {
*errorPtr = ErrorFromException(exception);
}
return NO;
}
[input release];
return YES;
}
#pragma mark - Parse From Data Support
+ (instancetype)parseFromData:(NSData *)data error:(NSError **)errorPtr {

Loading…
Cancel
Save