Skip to content

Commit

Permalink
[ObjC] Add a unpack helper for GPBAny with extension support.
Browse files Browse the repository at this point in the history
The message included could be proto2 syntax and thus have extensions.

PiperOrigin-RevId: 586991004
  • Loading branch information
thomasvl authored and copybara-github committed Dec 1, 2023
1 parent f0c495e commit 29fca8a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
18 changes: 18 additions & 0 deletions objectivec/GPBWellKnownTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,24 @@ typedef NS_ENUM(NSInteger, GPBWellKnownTypesErrorCode) {
*/
- (nullable GPBMessage *)unpackMessageClass:(Class)messageClass error:(NSError **)errorPtr;

/**
* Unpacks the serialized message as if it was an instance of the given class.
*
* @note When checking type_url, the base URL is not checked, only the fully
* qualified name.
*
* @param messageClass The class to use to deserialize the contained message.
* @param extensionRegistry The extension registry to use to look up extensions.
* @param errorPtr Pointer to an error that will be populated if something
* goes wrong.
*
* @return An instance of the given class populated with the contained data, or
* nil on failure.
*/
- (nullable GPBMessage *)unpackMessageClass:(Class)messageClass
extensionRegistry:(nullable id<GPBExtensionRegistry>)extensionRegistry
error:(NSError **)errorPtr;

@end

NS_ASSUME_NONNULL_END
11 changes: 7 additions & 4 deletions objectivec/GPBWellKnownTypes.m
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ - (BOOL)packWithMessage:(GPBMessage *)message
}

- (GPBMessage *)unpackMessageClass:(Class)messageClass error:(NSError **)errorPtr {
return [self unpackMessageClass:messageClass extensionRegistry:nil error:errorPtr];
}

- (nullable GPBMessage *)unpackMessageClass:(Class)messageClass
extensionRegistry:(nullable id<GPBExtensionRegistry>)extensionRegistry
error:(NSError **)errorPtr {
NSString *fullName = [messageClass descriptor].fullName;
if (fullName.length == 0) {
if (errorPtr) {
Expand All @@ -215,10 +221,7 @@ - (GPBMessage *)unpackMessageClass:(Class)messageClass error:(NSError **)errorPt
return nil;
}

// Any is proto3, which means no extensions, so this assumes anything put
// within an any also won't need extensions. A second helper could be added
// if needed.
return [messageClass parseFromData:self.value error:errorPtr];
return [messageClass parseFromData:self.value extensionRegistry:extensionRegistry error:errorPtr];
}

@end

0 comments on commit 29fca8a

Please sign in to comment.