Merge pull request #1714 from dnkoutso/master

Get value from text format name in GPBEnumDescriptor
pull/1719/head
Thomas Van Lenten 9 years ago committed by GitHub
commit 2bcd43afe4
  1. 1
      objectivec/GPBDescriptor.h
  2. 17
      objectivec/GPBDescriptor.m
  3. 8
      objectivec/Tests/GPBDescriptorTests.m

@ -123,6 +123,7 @@ typedef NS_ENUM(uint8_t, GPBFieldType) {
- (BOOL)getValue:(nullable int32_t *)outValue forEnumName:(NSString *)name; - (BOOL)getValue:(nullable int32_t *)outValue forEnumName:(NSString *)name;
- (nullable NSString *)textFormatNameForValue:(int32_t)number; - (nullable NSString *)textFormatNameForValue:(int32_t)number;
- (BOOL)getValue:(nullable int32_t *)outValue forEnumTextFormatName:(NSString *)textFormatName;
@end @end

@ -756,6 +756,23 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
return NO; 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 { - (NSString *)textFormatNameForValue:(int32_t)number {
if (nameOffsets_ == NULL) [self calcValueNameOffsets]; if (nameOffsets_ == NULL) [self calcValueNameOffsets];

@ -125,6 +125,12 @@
[descriptor getValue:&value forEnumName:@"TestAllTypes_NestedEnum_Baz"]); [descriptor getValue:&value forEnumName:@"TestAllTypes_NestedEnum_Baz"]);
XCTAssertEqual(value, 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 // Bad values
enumName = [descriptor enumNameForValue:0]; enumName = [descriptor enumNameForValue:0];
XCTAssertNil(enumName); XCTAssertNil(enumName);
@ -134,6 +140,8 @@
forEnumName:@"TestAllTypes_NestedEnum_Unknown"]); forEnumName:@"TestAllTypes_NestedEnum_Unknown"]);
XCTAssertFalse([descriptor getValue:NULL XCTAssertFalse([descriptor getValue:NULL
forEnumName:@"TestAllTypes_NestedEnum_Unknown"]); forEnumName:@"TestAllTypes_NestedEnum_Unknown"]);
XCTAssertFalse([descriptor getValue:NULL forEnumTextFormatName:@"Unknown"]);
XCTAssertFalse([descriptor getValue:&value forEnumTextFormatName:@"Unknown"]);
} }
- (void)testEnumValueValidator { - (void)testEnumValueValidator {

Loading…
Cancel
Save