Kaynağa Gözat

Fix up handing of fields with leading names that should be all caps.

Add a compile test to confirm things are working as expected.
Thomas Van Lenten 9 yıl önce
ebeveyn
işleme
1bf4b38f29

+ 1 - 0
Makefile.am

@@ -413,6 +413,7 @@ objectivec_EXTRA_DIST=                                                       \
   objectivec/GPBCodedInputStream_PackagePrivate.h                            \
   objectivec/GPBCodedInputStream_PackagePrivate.h                            \
   objectivec/GPBCodedOutputStream.h                                          \
   objectivec/GPBCodedOutputStream.h                                          \
   objectivec/GPBCodedOutputStream.m                                          \
   objectivec/GPBCodedOutputStream.m                                          \
+  objectivec/GPBCodedOutputStream_PackagePrivate.h                           \
   objectivec/GPBDescriptor.h                                                 \
   objectivec/GPBDescriptor.h                                                 \
   objectivec/GPBDescriptor.m                                                 \
   objectivec/GPBDescriptor.m                                                 \
   objectivec/GPBDescriptor_PackagePrivate.h                                  \
   objectivec/GPBDescriptor_PackagePrivate.h                                  \

+ 18 - 0
objectivec/Tests/GPBMessageTests.m

@@ -1820,6 +1820,24 @@
   XCTAssertEqualObjects(enumDescriptor, expectedDescriptor);
   XCTAssertEqualObjects(enumDescriptor, expectedDescriptor);
 }
 }
 
 
+- (void)testPropertyNaming {
+  // objectivec_helpers.cc has some special handing to get proper all caps
+  // for a few cases to meet objc developer expectations.
+  //
+  // This "test" confirms that the expected names are generated, otherwise the
+  // test itself will fail to compile.
+  ObjCPropertyNaming *msg = [ObjCPropertyNaming message];
+  // On their own, at the end, in the middle.
+  msg.URL = @"good";
+  msg.thumbnailURL = @"good";
+  msg.URLFoo = @"good";
+  msg.someURLBlah = @"good";
+  msg.HTTP = @"good";
+  msg.HTTPS = @"good";
+  // No caps since it was "urls".
+  [msg.urlsArray addObject:@"good"];
+}
+
 - (void)testEnumNaming {
 - (void)testEnumNaming {
   // objectivec_helpers.cc has some interesting cases to deal with in
   // objectivec_helpers.cc has some interesting cases to deal with in
   // EnumValueName/EnumValueShortName.  Confirm that things generated as
   // EnumValueName/EnumValueShortName.  Confirm that things generated as

+ 12 - 0
objectivec/Tests/unittest_objc.proto

@@ -112,6 +112,18 @@ enum retain {
   serializedSize = 6;
   serializedSize = 6;
 }
 }
 
 
+message ObjCPropertyNaming {
+  // Test that the properties properly get things all caps.
+  optional string url           = 1;
+  optional string thumbnail_url = 2;
+  optional string url_foo       = 3;
+  optional string some_url_blah = 4;
+  optional string http          = 5;
+  optional string https         = 6;
+  // This one doesn't.
+  repeated string urls          = 7;
+}
+
 // EnumValueShortName: The short names shouldn't get suffixes/prefixes.
 // EnumValueShortName: The short names shouldn't get suffixes/prefixes.
 enum Foo {
 enum Foo {
   SERIALIZED_SIZE = 1;
   SERIALIZED_SIZE = 1;

+ 9 - 6
src/google/protobuf/compiler/objectivec/objectivec_helpers.cc

@@ -124,9 +124,14 @@ string UnderscoresToCamelCase(const string& input, bool first_capitalized) {
   }
   }
   values.push_back(current);
   values.push_back(current);
 
 
+  string result;
+  bool first_segment_forces_upper = false;
   for (vector<string>::iterator i = values.begin(); i != values.end(); ++i) {
   for (vector<string>::iterator i = values.begin(); i != values.end(); ++i) {
     string value = *i;
     string value = *i;
     bool all_upper = (kUpperSegments.count(value) > 0);
     bool all_upper = (kUpperSegments.count(value) > 0);
+    if (all_upper && (result.length() == 0)) {
+      first_segment_forces_upper = true;
+    }
     for (int j = 0; j < value.length(); j++) {
     for (int j = 0; j < value.length(); j++) {
       if (j == 0 || all_upper) {
       if (j == 0 || all_upper) {
         value[j] = ascii_toupper(value[j]);
         value[j] = ascii_toupper(value[j]);
@@ -134,13 +139,11 @@ string UnderscoresToCamelCase(const string& input, bool first_capitalized) {
         // Nothing, already in lower.
         // Nothing, already in lower.
       }
       }
     }
     }
-    *i = value;
-  }
-  string result;
-  for (vector<string>::iterator i = values.begin(); i != values.end(); ++i) {
-    result += *i;
+    result += value;
   }
   }
-  if ((result.length() != 0) && !first_capitalized) {
+  if ((result.length() != 0) &&
+      !first_capitalized &&
+      !first_segment_forces_upper) {
     result[0] = ascii_tolower(result[0]);
     result[0] = ascii_tolower(result[0]);
   }
   }
   return result;
   return result;