Skip to content

Commit

Permalink
Merge pull request protocolbuffers#1720 from thomasvl/issue_1716
Browse files Browse the repository at this point in the history
Fix GPBGetMessage{Repeated,Map}Field()
  • Loading branch information
thomasvl authored Jun 28, 2016
2 parents dc0aeaa + fc4c617 commit e0016c5
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 22 deletions.
30 changes: 30 additions & 0 deletions objectivec/GPBMessage.m
Original file line number Diff line number Diff line change
Expand Up @@ -3199,4 +3199,34 @@ + (BOOL)accessInstanceVariablesDirectly {

@end

#pragma mark - Messages from GPBUtilities.h but defined here for access to helpers.

// Only exists for public api, no core code should use this.
id GPBGetMessageRepeatedField(GPBMessage *self, GPBFieldDescriptor *field) {
#if defined(DEBUG) && DEBUG
if (field.fieldType != GPBFieldTypeRepeated) {
[NSException raise:NSInvalidArgumentException
format:@"%@.%@ is not a repeated field.",
[self class], field.name];
}
#endif
GPBDescriptor *descriptor = [[self class] descriptor];
GPBFileSyntax syntax = descriptor.file.syntax;
return GetOrCreateArrayIvarWithField(self, field, syntax);
}

// Only exists for public api, no core code should use this.
id GPBGetMessageMapField(GPBMessage *self, GPBFieldDescriptor *field) {
#if defined(DEBUG) && DEBUG
if (field.fieldType != GPBFieldTypeMap) {
[NSException raise:NSInvalidArgumentException
format:@"%@.%@ is not a map<> field.",
[self class], field.name];
}
#endif
GPBDescriptor *descriptor = [[self class] descriptor];
GPBFileSyntax syntax = descriptor.file.syntax;
return GetOrCreateMapIvarWithField(self, field, syntax);
}

#pragma clang diagnostic pop
24 changes: 2 additions & 22 deletions objectivec/GPBUtilities.m
Original file line number Diff line number Diff line change
Expand Up @@ -895,17 +895,7 @@ void GPBSetMessageGroupField(GPBMessage *self,

//%PDDM-EXPAND-END (4 expansions)

// Only exists for public api, no core code should use this.
id GPBGetMessageRepeatedField(GPBMessage *self, GPBFieldDescriptor *field) {
#if defined(DEBUG) && DEBUG
if (field.fieldType != GPBFieldTypeRepeated) {
[NSException raise:NSInvalidArgumentException
format:@"%@.%@ is not a repeated field.",
[self class], field.name];
}
#endif
return GPBGetObjectIvarWithField(self, field);
}
// GPBGetMessageRepeatedField is defined in GPBMessage.m

// Only exists for public api, no core code should use this.
void GPBSetMessageRepeatedField(GPBMessage *self, GPBFieldDescriptor *field, id array) {
Expand Down Expand Up @@ -997,17 +987,7 @@ void GPBSetMessageRepeatedField(GPBMessage *self, GPBFieldDescriptor *field, id
}
#endif

// Only exists for public api, no core code should use this.
id GPBGetMessageMapField(GPBMessage *self, GPBFieldDescriptor *field) {
#if defined(DEBUG) && DEBUG
if (field.fieldType != GPBFieldTypeMap) {
[NSException raise:NSInvalidArgumentException
format:@"%@.%@ is not a map<> field.",
[self class], field.name];
}
#endif
return GPBGetObjectIvarWithField(self, field);
}
// GPBGetMessageMapField is defined in GPBMessage.m

// Only exists for public api, no core code should use this.
void GPBSetMessageMapField(GPBMessage *self, GPBFieldDescriptor *field,
Expand Down
40 changes: 40 additions & 0 deletions objectivec/Tests/GPBMessageTests+Runtime.m
Original file line number Diff line number Diff line change
Expand Up @@ -2066,6 +2066,46 @@ - (void)testCopyingMapsMakesUniqueObjects {
}];
}

- (void)test_GPBGetMessageRepeatedField {
TestAllTypes *message = [TestAllTypes message];
GPBFieldDescriptor *fieldDescriptor = [[message descriptor] fieldWithName:@"repeatedStringArray"];
XCTAssertNotNil(fieldDescriptor);
NSMutableArray *fieldArray = GPBGetMessageRepeatedField(message, fieldDescriptor);
XCTAssertNotNil(fieldArray); // Should have autocreated.
XCTAssertTrue(fieldArray == message.repeatedStringArray); // Same pointer
}

- (void)test_GPBSetMessageRepeatedField {
TestAllTypes *message = [TestAllTypes message];
GPBFieldDescriptor *fieldDescriptor = [[message descriptor] fieldWithName:@"repeatedStringArray"];
XCTAssertNotNil(fieldDescriptor);

NSMutableArray *fieldArray = [NSMutableArray arrayWithObject:@"foo"];
GPBSetMessageRepeatedField(message, fieldDescriptor, fieldArray);
XCTAssertTrue(fieldArray == message.repeatedStringArray); // Same pointer
XCTAssertEqualObjects(@"foo", message.repeatedStringArray.firstObject);
}

- (void)test_GPBGetMessageMapField {
TestMap *message = [TestMap message];
GPBFieldDescriptor *fieldDescriptor = [[message descriptor] fieldWithName:@"mapStringString"];
XCTAssertNotNil(fieldDescriptor);
NSMutableDictionary *fieldMap = GPBGetMessageMapField(message, fieldDescriptor);
XCTAssertNotNil(fieldMap); // Should have autocreated.
XCTAssertTrue(fieldMap == message.mapStringString); // Same pointer
}

- (void)test_GPBSetMessageMapField {
TestMap *message = [TestMap message];
GPBFieldDescriptor *fieldDescriptor = [[message descriptor] fieldWithName:@"mapStringString"];
XCTAssertNotNil(fieldDescriptor);

NSMutableDictionary *fieldMap = [NSMutableDictionary dictionaryWithObject:@"bar" forKey:@"foo"];
GPBSetMessageMapField(message, fieldDescriptor, fieldMap);
XCTAssertTrue(fieldMap == message.mapStringString); // Same pointer
XCTAssertEqualObjects(@"bar", message.mapStringString[@"foo"]);
}

#pragma mark - Subset from from map_tests.cc

// TEST(GeneratedMapFieldTest, IsInitialized)
Expand Down

0 comments on commit e0016c5

Please sign in to comment.