Skip to content

Commit 0790ab4

Browse files
committed
[ObjC] Add api to add a field to another collection of unknown fields.
PiperOrigin-RevId: 663424215
1 parent b3b9888 commit 0790ab4

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

objectivec/GPBUnknownFields.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,23 @@ __attribute__((objc_subclassing_restricted))
109109
**/
110110
- (nonnull GPBUnknownFields *)addGroupWithFieldNumber:(int32_t)fieldNumber;
111111

112+
/**
113+
* Add the copy of the given unknown field.
114+
*
115+
* This can be useful from processing one `GPBUnknownFields` to create another.
116+
*
117+
* NOTE: If the field being copied is an Group, this instance added is new and thus
118+
* the `.group` of that result is also new, so if you intent is to modify the group
119+
* it *must* be fetched out of the result.
120+
*
121+
* It is a programming error to call this when the `type` is a legacy field.
122+
*
123+
* @param field The field to add.
124+
*
125+
* @return The autoreleased field that was added.
126+
**/
127+
- (GPBUnknownField *)addCopyOfField:(nonnull GPBUnknownField *)field;
128+
112129
/**
113130
* Removes the given field from the set.
114131
*

objectivec/GPBUnknownFields.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,16 @@ - (GPBUnknownFields *)addGroupWithFieldNumber:(int32_t)fieldNumber {
323323
return [group autorelease];
324324
}
325325

326+
- (GPBUnknownField *)addCopyOfField:(nonnull GPBUnknownField *)field {
327+
if (field->type_ == GPBUnknownFieldTypeLegacy) {
328+
[NSException raise:NSInternalInconsistencyException
329+
format:@"GPBUnknownField is the wrong type"];
330+
}
331+
GPBUnknownField *result = [field copy];
332+
[fields_ addObject:result];
333+
return [result autorelease];
334+
}
335+
326336
- (void)removeField:(nonnull GPBUnknownField *)field {
327337
NSUInteger count = fields_.count;
328338
[fields_ removeObjectIdenticalTo:field];

objectivec/Tests/GPBUnknownFieldsTest.m

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,37 @@ - (void)testFastEnumeration {
662662
XCTAssertEqual(loop, 10);
663663
}
664664

665+
- (void)testAddCopyOfField {
666+
GPBUnknownFields* ufs = [[[GPBUnknownFields alloc] init] autorelease];
667+
[ufs addFieldNumber:1 varint:10];
668+
[ufs addFieldNumber:2 fixed32:11];
669+
[ufs addFieldNumber:3 fixed64:12];
670+
[ufs addFieldNumber:4 lengthDelimited:DataFromCStr("foo")];
671+
GPBUnknownFields* group = [ufs addGroupWithFieldNumber:5];
672+
[group addFieldNumber:10 varint:100];
673+
GPBUnknownFields* subGroup = [group addGroupWithFieldNumber:100];
674+
[subGroup addFieldNumber:50 varint:50];
675+
676+
GPBUnknownFields* ufs2 = [[[GPBUnknownFields alloc] init] autorelease];
677+
for (GPBUnknownField* field in ufs) {
678+
GPBUnknownField* field2 = [ufs2 addCopyOfField:field];
679+
XCTAssertEqualObjects(field, field2);
680+
if (field.type == GPBUnknownFieldTypeGroup) {
681+
// Group does a copy because the `.group` value is mutable.
682+
XCTAssertTrue(field != field2); // Pointer comparison.
683+
XCTAssertTrue(group != field2.group); // Pointer comparison.
684+
XCTAssertEqualObjects(group, field2.group);
685+
GPBUnknownFields* subGroupAdded = [field2.group firstGroup:100];
686+
XCTAssertTrue(subGroupAdded != subGroup); // Pointer comparison.
687+
XCTAssertEqualObjects(subGroupAdded, subGroup);
688+
} else {
689+
// All other types are immutable, so they use the same object.
690+
XCTAssertTrue(field == field2); // Pointer comparision.
691+
}
692+
}
693+
XCTAssertEqualObjects(ufs, ufs2);
694+
}
695+
665696
- (void)testDescriptions {
666697
// Exercise description for completeness.
667698
GPBUnknownFields* ufs = [[[GPBUnknownFields alloc] init] autorelease];

0 commit comments

Comments
 (0)