[ObjC] Add a `unpack` helper for GPBAny with extension support.
The message included could be proto2 syntax and thus have extensions. PiperOrigin-RevId: 586991004pull/14917/head
parent
f0c495ef3e
commit
29fca8a64b
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue