Skip to content

Commit

Permalink
Merge pull request protocolbuffers#1714 from dnkoutso/master
Browse files Browse the repository at this point in the history
Get value from text format name in GPBEnumDescriptor
  • Loading branch information
thomasvl authored Jun 27, 2016
2 parents 0ab78e1 + 37ca94f commit 2bcd43a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions objectivec/GPBDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ typedef NS_ENUM(uint8_t, GPBFieldType) {
- (BOOL)getValue:(nullable int32_t *)outValue forEnumName:(NSString *)name;

- (nullable NSString *)textFormatNameForValue:(int32_t)number;
- (BOOL)getValue:(nullable int32_t *)outValue forEnumTextFormatName:(NSString *)textFormatName;

@end

Expand Down
17 changes: 17 additions & 0 deletions objectivec/GPBDescriptor.m
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,23 @@ - (BOOL)getValue:(int32_t *)outValue forEnumName:(NSString *)name {
return NO;
}

- (BOOL)getValue:(int32_t *)outValue forEnumTextFormatName:(NSString *)textFormatName;
{
if (nameOffsets_ == NULL) [self calcValueNameOffsets];

for (uint32_t i = 0; i < valueCount_; ++i) {
int32_t value = values_[i];
NSString *valueTextFormatName = [self textFormatNameForValue:value];
if ([valueTextFormatName isEqual:textFormatName]) {
if (outValue) {
*outValue = value;
}
return YES;
}
}
return NO;
}

- (NSString *)textFormatNameForValue:(int32_t)number {
if (nameOffsets_ == NULL) [self calcValueNameOffsets];

Expand Down
8 changes: 8 additions & 0 deletions objectivec/Tests/GPBDescriptorTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ - (void)testEnumDescriptor {
[descriptor getValue:&value forEnumName:@"TestAllTypes_NestedEnum_Baz"]);
XCTAssertEqual(value, TestAllTypes_NestedEnum_Baz);

// TextFormat
enumName = [descriptor textFormatNameForValue:1];
XCTAssertNotNil(enumName);
XCTAssertTrue([descriptor getValue:&value forEnumTextFormatName:@"FOO"]);
XCTAssertEqual(value, TestAllTypes_NestedEnum_Foo);

// Bad values
enumName = [descriptor enumNameForValue:0];
XCTAssertNil(enumName);
Expand All @@ -134,6 +140,8 @@ - (void)testEnumDescriptor {
forEnumName:@"TestAllTypes_NestedEnum_Unknown"]);
XCTAssertFalse([descriptor getValue:NULL
forEnumName:@"TestAllTypes_NestedEnum_Unknown"]);
XCTAssertFalse([descriptor getValue:NULL forEnumTextFormatName:@"Unknown"]);
XCTAssertFalse([descriptor getValue:&value forEnumTextFormatName:@"Unknown"]);
}

- (void)testEnumValueValidator {
Expand Down

0 comments on commit 2bcd43a

Please sign in to comment.