Skip to content

Commit

Permalink
[ObjC] Add api to add a field to another collection of unknown fields.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 663424215
  • Loading branch information
thomasvl committed Aug 16, 2024
1 parent b3b9888 commit 0790ab4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
17 changes: 17 additions & 0 deletions objectivec/GPBUnknownFields.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,23 @@ __attribute__((objc_subclassing_restricted))
**/
- (nonnull GPBUnknownFields *)addGroupWithFieldNumber:(int32_t)fieldNumber;

/**
* Add the copy of the given unknown field.
*
* This can be useful from processing one `GPBUnknownFields` to create another.
*
* NOTE: If the field being copied is an Group, this instance added is new and thus
* the `.group` of that result is also new, so if you intent is to modify the group
* it *must* be fetched out of the result.
*
* It is a programming error to call this when the `type` is a legacy field.
*
* @param field The field to add.
*
* @return The autoreleased field that was added.
**/
- (GPBUnknownField *)addCopyOfField:(nonnull GPBUnknownField *)field;

/**
* Removes the given field from the set.
*
Expand Down
10 changes: 10 additions & 0 deletions objectivec/GPBUnknownFields.m
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,16 @@ - (GPBUnknownFields *)addGroupWithFieldNumber:(int32_t)fieldNumber {
return [group autorelease];
}

- (GPBUnknownField *)addCopyOfField:(nonnull GPBUnknownField *)field {
if (field->type_ == GPBUnknownFieldTypeLegacy) {
[NSException raise:NSInternalInconsistencyException
format:@"GPBUnknownField is the wrong type"];
}
GPBUnknownField *result = [field copy];
[fields_ addObject:result];
return [result autorelease];
}

- (void)removeField:(nonnull GPBUnknownField *)field {
NSUInteger count = fields_.count;
[fields_ removeObjectIdenticalTo:field];
Expand Down
31 changes: 31 additions & 0 deletions objectivec/Tests/GPBUnknownFieldsTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,37 @@ - (void)testFastEnumeration {
XCTAssertEqual(loop, 10);
}

- (void)testAddCopyOfField {
GPBUnknownFields* ufs = [[[GPBUnknownFields alloc] init] autorelease];
[ufs addFieldNumber:1 varint:10];
[ufs addFieldNumber:2 fixed32:11];
[ufs addFieldNumber:3 fixed64:12];
[ufs addFieldNumber:4 lengthDelimited:DataFromCStr("foo")];
GPBUnknownFields* group = [ufs addGroupWithFieldNumber:5];
[group addFieldNumber:10 varint:100];
GPBUnknownFields* subGroup = [group addGroupWithFieldNumber:100];
[subGroup addFieldNumber:50 varint:50];

GPBUnknownFields* ufs2 = [[[GPBUnknownFields alloc] init] autorelease];
for (GPBUnknownField* field in ufs) {
GPBUnknownField* field2 = [ufs2 addCopyOfField:field];
XCTAssertEqualObjects(field, field2);
if (field.type == GPBUnknownFieldTypeGroup) {
// Group does a copy because the `.group` value is mutable.
XCTAssertTrue(field != field2); // Pointer comparison.
XCTAssertTrue(group != field2.group); // Pointer comparison.
XCTAssertEqualObjects(group, field2.group);
GPBUnknownFields* subGroupAdded = [field2.group firstGroup:100];
XCTAssertTrue(subGroupAdded != subGroup); // Pointer comparison.
XCTAssertEqualObjects(subGroupAdded, subGroup);
} else {
// All other types are immutable, so they use the same object.
XCTAssertTrue(field == field2); // Pointer comparision.
}
}
XCTAssertEqualObjects(ufs, ufs2);
}

- (void)testDescriptions {
// Exercise description for completeness.
GPBUnknownFields* ufs = [[[GPBUnknownFields alloc] init] autorelease];
Expand Down

0 comments on commit 0790ab4

Please sign in to comment.