Skip to content

Commit

Permalink
[ObjC] Internal helper for getting the unknown field data from a message
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 655202619
  • Loading branch information
thomasvl committed Jul 24, 2024
1 parent 0832b52 commit bf2d4e7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
19 changes: 19 additions & 0 deletions objectivec/GPBMessage.m
Original file line number Diff line number Diff line change
Expand Up @@ -3684,4 +3684,23 @@ id GPBGetObjectIvarWithField(GPBMessage *self, GPBFieldDescriptor *field) {
return expected;
}

NSData *GPBMessageUnknownFieldsData(GPBMessage *self) {
NSData *result = nil;
GPBUnknownFieldSet *unknownFields = self->unknownFields_;
if (unknownFields) {
if (self.descriptor.isWireFormat) {
NSMutableData *mutableData =
[NSMutableData dataWithLength:unknownFields.serializedSizeAsMessageSet];
GPBCodedOutputStream *output = [[GPBCodedOutputStream alloc] initWithData:mutableData];
[unknownFields writeAsMessageSetTo:output];
[output flush];
[output release];
result = mutableData;
} else {
result = [unknownFields data];
}
}
return result;
}

#pragma clang diagnostic pop
3 changes: 3 additions & 0 deletions objectivec/GPBMessage_PackagePrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,7 @@ void GPBAutocreatedDictionaryModified(GPBMessage *self, id dictionary);
// autocreated reference to this message.
void GPBClearMessageAutocreator(GPBMessage *self);

// The data (or null) from the unknown fields of a message;
NSData *GPBMessageUnknownFieldsData(GPBMessage *self);

CF_EXTERN_C_END
19 changes: 3 additions & 16 deletions objectivec/GPBUnknownFields.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#import "GPBCodedOutputStream_PackagePrivate.h"
#import "GPBDescriptor.h"
#import "GPBMessage.h"
#import "GPBMessage_PackagePrivate.h"
#import "GPBUnknownField.h"
#import "GPBUnknownFieldSet_PackagePrivate.h"
#import "GPBUnknownField_PackagePrivate.h"
Expand Down Expand Up @@ -196,22 +197,8 @@ - (instancetype)initFromMessage:(nonnull GPBMessage *)message {
self = [super init];
if (self) {
fields_ = [[NSMutableArray alloc] init];
// TODO: b/349146447 - Move off the legacy class and directly to the data once Message is
// updated.
GPBUnknownFieldSet *legacyUnknownFields = [message unknownFields];
if (legacyUnknownFields) {
NSData *data;
if (message.descriptor.isWireFormat) {
NSMutableData *mutableData =
[NSMutableData dataWithLength:legacyUnknownFields.serializedSizeAsMessageSet];
GPBCodedOutputStream *output = [[GPBCodedOutputStream alloc] initWithData:mutableData];
[legacyUnknownFields writeAsMessageSetTo:output];
[output flush];
[output release];
data = mutableData;
} else {
data = [legacyUnknownFields data];
}
NSData *data = GPBMessageUnknownFieldsData(message);
if (data) {
GPBCodedInputStream *input = [[GPBCodedInputStream alloc] initWithData:data];
// Parse until the end of the data (tag will be zero).
if (!MergeFromInputStream(self, input, 0)) {
Expand Down

0 comments on commit bf2d4e7

Please sign in to comment.