You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@avro.apache.org by GitBox <gi...@apache.org> on 2022/06/28 12:58:21 UTC

[GitHub] [avro] clesaec commented on a diff in pull request #735: AVRO-2385: generate camelCase method names for UPPER_SNAKE fields

clesaec commented on code in PR #735:
URL: https://github.com/apache/avro/pull/735#discussion_r908448432


##########
lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java:
##########
@@ -1293,8 +1288,54 @@ private static String generateMethodName(Schema schema, Field field, String pref
   }
 
   /**
-   * Tests whether an unboxed Java type can be set to null
+   * CamelCase the incoming string, using underscores as the word hints
+   *
+   * @param s     string to camel-case. Must contain only [a-zA-Z0-9_]
+   * @param upper should the first letter be capitalized
    */
+  static String camelize(String s, boolean upper) {
+    if (StringUtils.contains(s, '_')) {
+      // use underscores as hints where the capitals go
+      StringBuilder builder = new StringBuilder();
+      for (String part : StringUtils.split(StringUtils.lowerCase(s), '_')) {
+        builder.append(StringUtils.capitalize(part));
+      }
+      s = builder.toString();
+
+      // fixup first character
+      if (upper) {
+        return StringUtils.capitalize(s);
+      } else {
+        return StringUtils.uncapitalize(s);
+      }
+    }
+

Review Comment:
   And what about MIXED_CamelCasedTYPED ? :) 
   Shouldn't it be better to split at start, then ignore part that contains only '_'
   ```java
   StringUtils.splitByCharacterTypeCamelCase("MIXED_CamelCasedTYPED") =>
   ["MIXED3, "_", "Camel", "Cased", "TYPED"]
   ```
   I suggest
   ```java
   static String camelize(String s, boolean upper) {
      String[] parts = StringUtils.splitByCharacterTypeCamelCase(s);
      int index = 0;
      while (index < parts.length && "_".equals(parts[index])) {
          index++;
      }
      // subsections should retain their casing if all upper
     ...
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org