Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AVRO-2385: generate camelCase method names for UPPER_SNAKE fields #735

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.apache.avro.data.TimeConversions;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericData.StringType;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.avro.specific.SpecificData;
import org.apache.commons.lang3.StringUtils;
import org.apache.velocity.Template;
Expand Down Expand Up @@ -1268,16 +1269,10 @@ private static String generateMethodName(Schema schema, Field field, String pref
String fieldName = mangle(field.name(), schema.isError() ? ERROR_RESERVED_WORDS : ACCESSOR_MUTATOR_RESERVED_WORDS,
true);

boolean nextCharToUpper = true;
for (int ii = 0; ii < fieldName.length(); ii++) {
if (fieldName.charAt(ii) == '_') {
nextCharToUpper = true;
} else if (nextCharToUpper) {
methodBuilder.append(Character.toUpperCase(fieldName.charAt(ii)));
nextCharToUpper = false;
} else {
methodBuilder.append(fieldName.charAt(ii));
}
if (prefix.isEmpty()) {
methodBuilder.append(camelize(fieldName, false));
} else {
methodBuilder.append(camelize(fieldName, true));
}
methodBuilder.append(postfix);

Expand All @@ -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);
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And what about MIXED_CamelCasedTYPED ? :)
Shouldn't it be better to split at start, then ignore part that contains only '_'

StringUtils.splitByCharacterTypeCamelCase("MIXED_CamelCasedTYPED") =>
["MIXED3, "_", "Camel", "Cased", "TYPED"]

I suggest

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
  ...

// probably camelcase, split by camel to reassemble
StringBuilder builder = new StringBuilder();
String[] parts = StringUtils.splitByCharacterTypeCamelCase(s);

// subsections should retain their casing if all upper
// unless they're in the first section, then their casing should be modified to
// match the starting case
if (StringUtils.isAllUpperCase(parts[0]) && upper) {
builder.append(parts[0]);
} else if (upper) {
builder.append(StringUtils.capitalize(StringUtils.lowerCase(parts[0])));
} else {
builder.append(StringUtils.lowerCase(parts[0]));
}

for (String part : ArrayUtils.subarray(parts, 1, parts.length)) {
if (StringUtils.isAllUpperCase(part)) {
builder.append(part);
} else {
builder.append(StringUtils.capitalize(StringUtils.lowerCase(part)));
}
}
return builder.toString();
}

/** Tests whether an unboxed Java type can be set to null */
public static boolean isUnboxedJavaTypeNullable(Schema schema) {
switch (schema.getType()) {
// Primitives can't be null; assume anything else can
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,32 @@ public void testAdditionalToolsAreInjectedIntoTemplate() throws Exception {
}

@Test
public void testCamelize() {
assertEquals("snakeCase", SpecificCompiler.camelize("snake_case", false));
assertEquals("constantCase", SpecificCompiler.camelize("CONSTANT_CASE", false));
assertEquals("mixedSnake", SpecificCompiler.camelize("Mixed_Snake", false));
assertEquals("snakeUuid", SpecificCompiler.camelize("snake_UUID", false));
assertEquals("lower", SpecificCompiler.camelize("lower", false));
assertEquals("capitalized", SpecificCompiler.camelize("Capitalized", false));
assertEquals("caps", SpecificCompiler.camelize("CAPS", false));
assertEquals("camelCased", SpecificCompiler.camelize("camelCased", false));
assertEquals("camelCased", SpecificCompiler.camelize("CamelCased", false));
martin-g marked this conversation as resolved.
Show resolved Hide resolved
assertEquals("someUUID", SpecificCompiler.camelize("someUUID", false));
assertEquals("uuidFirst", SpecificCompiler.camelize("UUIDFirst", false));

assertEquals("SnakeCase", SpecificCompiler.camelize("snake_case", true));
assertEquals("ConstantCase", SpecificCompiler.camelize("CONSTANT_CASE", true));
assertEquals("MixedSnake", SpecificCompiler.camelize("Mixed_Snake", true));
assertEquals("SnakeUuid", SpecificCompiler.camelize("snake_UUID", true));
assertEquals("Lower", SpecificCompiler.camelize("lower", true));
assertEquals("Capitalized", SpecificCompiler.camelize("Capitalized", true));
assertEquals("CAPS", SpecificCompiler.camelize("CAPS", true));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intentional ?
At https://github.com/apache/avro/pull/735/files#diff-bdda25ef6cc04f1ca556df625d9d4ecdd65951cfcd8b4e38da5916765bff75dbR946 UUID has been transformed to Uuid but here CAPS remains as CAPS. I'd expect Caps instead.

assertEquals("CamelCased", SpecificCompiler.camelize("camelCased", true));
assertEquals("CamelCased", SpecificCompiler.camelize("CamelCased", true));
martin-g marked this conversation as resolved.
Show resolved Hide resolved
assertEquals("SomeUUID", SpecificCompiler.camelize("someUUID", true));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as form CAPS above.

assertEquals("UUIDFirst", SpecificCompiler.camelize("UUIDFirst", true));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

}

public void testPojoWithUUID() throws IOException {
SpecificCompiler compiler = createCompiler();
compiler.setOptionalGettersForNullableFieldsOnly(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,13 @@ public void generateGetMethod() {
Schema$ = new Field("Schema", Schema.create(Type.STRING), null, null);
assertEquals("getSchema$1",
SpecificCompiler.generateGetMethod(createRecord("test", false, schema, Schema$), Schema$));

Field ALL_UPPER_CASE = new Field("ALL_UPPER_CASE", Schema.create(Type.STRING), null, null);
assertEquals("getAllUpperCase",
SpecificCompiler.generateGetMethod(createRecord("test", false, ALL_UPPER_CASE), ALL_UPPER_CASE));

Field someUUID = new Field("someUUID", Schema.create(Type.STRING), null, null);
assertEquals("getSomeUUID", SpecificCompiler.generateGetMethod(createRecord("test", false, someUUID), someUUID));
}

@Test
Expand Down Expand Up @@ -415,6 +422,13 @@ public void generateSetMethod() {
Schema$ = new Field("Schema", Schema.create(Type.STRING), null, null);
assertEquals("setSchema$1",
SpecificCompiler.generateSetMethod(createRecord("test", false, schema, Schema$), Schema$));

Field ALL_UPPER_CASE = new Field("ALL_UPPER_CASE", Schema.create(Type.STRING), null, null);
assertEquals("setAllUpperCase",
SpecificCompiler.generateSetMethod(createRecord("test", false, ALL_UPPER_CASE), ALL_UPPER_CASE));

Field someUUID = new Field("someUUID", Schema.create(Type.STRING), null, null);
assertEquals("setSomeUUID", SpecificCompiler.generateSetMethod(createRecord("test", false, someUUID), someUUID));
}

@Test
Expand Down