[ObjC] Add a `unpack` helper for GPBAny with extension support.

The message included could be proto2 syntax and thus have extensions.

PiperOrigin-RevId: 586991004
pull/14917/head
Thomas Van Lenten 2023-12-01 06:20:51 -08:00 committed by Copybara-Service
parent f0c495ef3e
commit 29fca8a64b
2 changed files with 25 additions and 4 deletions

View File

@ -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

View File

@ -195,6 +195,12 @@ static NSString *ParseTypeFromURL(NSString *typeURLString) {
}
- (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) {
@ -215,10 +221,7 @@ static NSString *ParseTypeFromURL(NSString *typeURLString) {
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