Skip to content

Commit 29fca8a

Browse files
thomasvlcopybara-github
authored andcommitted
[ObjC] Add a unpack helper for GPBAny with extension support.
The message included could be proto2 syntax and thus have extensions. PiperOrigin-RevId: 586991004
1 parent f0c495e commit 29fca8a

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

objectivec/GPBWellKnownTypes.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,24 @@ typedef NS_ENUM(NSInteger, GPBWellKnownTypesErrorCode) {
201201
*/
202202
- (nullable GPBMessage *)unpackMessageClass:(Class)messageClass error:(NSError **)errorPtr;
203203

204+
/**
205+
* Unpacks the serialized message as if it was an instance of the given class.
206+
*
207+
* @note When checking type_url, the base URL is not checked, only the fully
208+
* qualified name.
209+
*
210+
* @param messageClass The class to use to deserialize the contained message.
211+
* @param extensionRegistry The extension registry to use to look up extensions.
212+
* @param errorPtr Pointer to an error that will be populated if something
213+
* goes wrong.
214+
*
215+
* @return An instance of the given class populated with the contained data, or
216+
* nil on failure.
217+
*/
218+
- (nullable GPBMessage *)unpackMessageClass:(Class)messageClass
219+
extensionRegistry:(nullable id<GPBExtensionRegistry>)extensionRegistry
220+
error:(NSError **)errorPtr;
221+
204222
@end
205223

206224
NS_ASSUME_NONNULL_END

objectivec/GPBWellKnownTypes.m

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ - (BOOL)packWithMessage:(GPBMessage *)message
195195
}
196196

197197
- (GPBMessage *)unpackMessageClass:(Class)messageClass error:(NSError **)errorPtr {
198+
return [self unpackMessageClass:messageClass extensionRegistry:nil error:errorPtr];
199+
}
200+
201+
- (nullable GPBMessage *)unpackMessageClass:(Class)messageClass
202+
extensionRegistry:(nullable id<GPBExtensionRegistry>)extensionRegistry
203+
error:(NSError **)errorPtr {
198204
NSString *fullName = [messageClass descriptor].fullName;
199205
if (fullName.length == 0) {
200206
if (errorPtr) {
@@ -215,10 +221,7 @@ - (GPBMessage *)unpackMessageClass:(Class)messageClass error:(NSError **)errorPt
215221
return nil;
216222
}
217223

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

224227
@end

0 commit comments

Comments
 (0)