Skip to content

Commit

Permalink
improve toEnumVarName
Browse files Browse the repository at this point in the history
  • Loading branch information
jpfinne committed Jul 10, 2024
1 parent 07de37e commit a856f5f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -864,8 +864,6 @@ public String toApiFilename(String name) {
return toApiName(name);
}


// TODO: document the purpose of this sanitize
private String sanitizeNameNonwordDashDollar(String name) {
// sanitize name
return sanitizeName(name, "\\W-[\\$]");
Expand All @@ -874,43 +872,57 @@ private String sanitizeNameNonwordDashDollar(String name) {
/**
* Avoid Overloading getClass Method.
*/
private static String sanetizeNamePropertyClass(String name) {
private static String sanetizeVarNamePropertyClass(String name) {
return name.toLowerCase(Locale.ROOT).matches("^_*class$")? "propertyClass": name;
}

/**
* replace any non-word character to underscore.
*/
private static String saniziteVarNameAllNonWordCharacters(String name) {
private static String sanitizeVarNameAllNonWordCharacters(String name) {
return name.replaceAll("\\W+", "_");
}

/**
* replace any characters not allowed in java identifiers to underscore.
*/
private static String saniziteVarNameNonJavaIdentifier(String name) {
return name.replaceAll("[^a-zA-Z0-9$]+", "_");
private static String sanitizeVarNameNonJavaIdentifier(String name) {
return name.replaceAll("[^a-zA-Z0-9$_]+", "_");
}

// numbers are not allowed at the beginning
private static String sanetizeNameStartingWithDigits(String name) {
return name.matches("^\\d.*")? "_" + name: name;
}

private static String sanetizeNameOnlyUnderscore(String name) {
return "_".equals(name)? "_u": name;
}

// camelize (lower first character) the variable name
// pet_id => petId
private String sanetizeNameCamelCaseDollarSign(String name) {
private String sanitizeNameCamelCaseDollarSign(String name) {
if (camelCaseDollarSign) {
return camelize(name, LOWERCASE_FIRST_CHAR);
} else {
return camelize(name, LOWERCASE_FIRST_LETTER);
}
}

private String sanitizeNameStartCamelCaseDollarSign(String name) {
if (name.matches("^\\$[a-zA-Z].*")) {
// var starts with a dollar sign and a letter
// we can only camelize the 2 first characters
// otherwise the underscores are removed
return sanitizeNameCamelCaseDollarSign(name.substring(0, 2)) + StringUtils.substring(name, 2);
}
return name;
}

private String sanitizeNameCamelCaseForNonUnderscore(String name) {
if (name.matches("^[^_]+$")) {
return sanitizeNameCamelCaseDollarSign(name);
}
return name;
}


private String sanetizeNameSpecialCharacters(String name) {
// If name contains special chars -> replace them.
if (name.chars().anyMatch(character -> specialCharReplacements.containsKey(String.valueOf(character)))) {
Expand All @@ -932,12 +944,18 @@ public String toVarName(final String name) {
return nameMapping.get(name);
}

if ("_".equals(name)) {
// return immediately to avoid additional conversion
return "u";
}

String var = name;

var = sanitizeNameNonwordDashDollar(var);
var = sanetizeNamePropertyClass(var);
var = sanetizeNameOnlyUnderscore(var);

var = sanetizeNameStartingWithDigits(var);
var = sanitizeNameNonwordDashDollar(var);
var = sanetizeVarNamePropertyClass(var);


// if it's all upper case, do nothing
if (var.matches("^[A-Z0-9_]*$")) {
Expand All @@ -949,7 +967,7 @@ public String toVarName(final String name) {
}

var = sanetizeNameSpecialCharacters(var);
var = sanetizeNameCamelCaseDollarSign(var);
var = sanitizeNameCamelCaseDollarSign(var);
var = sanetizeNameReservedWord(var);

return var;
Expand Down Expand Up @@ -2023,40 +2041,46 @@ public String toEnumVarName(String value, String datatype) {

switch (enumPropertyNaming) {
case original:
var = saniziteVarNameNonJavaIdentifier(var);
var = sanitizeVarNameNonJavaIdentifier(var);
// NOTE: This is provided as a last-case allowance, but will still result in reserved words being escaped.
break;
case camelCase:
var = saniziteVarNameNonJavaIdentifier(var);
var = sanetizeNameCamelCaseDollarSign(var);
var = sanitizeVarNameNonJavaIdentifier(var);
var = sanitizeNameCamelCaseForNonUnderscore(var);
var = sanitizeNameStartCamelCaseDollarSign(var);
break;
case PascalCase:
var = saniziteVarNameNonJavaIdentifier(var);
var = StringUtils.capitalize(sanetizeNameCamelCaseDollarSign(var));
var = sanitizeVarNameNonJavaIdentifier(var);
// if it's all upper case, do nothing
var = sanitizeNameCamelCaseForNonUnderscore(var);
var = StringUtils.capitalize(var);
break;
case snake_case:
var = saniziteVarNameNonJavaIdentifier(var);
// NOTE: hyphens are already replaced by underscores.
var = sanitizeVarNameNonJavaIdentifier(var);
var = sanitizeNameStartCamelCaseDollarSign(var);
break;
case UPPERCASE:
// This is the backward compatible behaviour

var = saniziteVarNameAllNonWordCharacters(var);
var = underscore(var).toUpperCase(Locale.ROOT);
// Note; don't know why we need this. Keep it for backward compatibility only in the default enumPropertyNaming
var = sanetizeNamePropertyClass(var);
var = sanitizeVarNameAllNonWordCharacters(var);
var = underscore(var);
var = var.toUpperCase(Locale.ROOT);
// Note; don't know why we need this. Keep it for backward compatibility only in the default enumPropertyNaming
//var = sanetizeVarNamePropertyClass(var);
if ("_".equals(var)) {
// backward compatibility. it would make sense to return "UNDERSCORE"
return "u";
}
break;
}

/*
fix uncompilable code:
- underscore
- name starting with a digit
- reserved words
*/

var = sanetizeNameStartingWithDigits(var);
var = sanetizeNameOnlyUnderscore(var);
var = sanetizeNameReservedWord(var);
return var;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ public void toEnumVarName_original() {
codegen.setEnumPropertyNaming(CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.original);
Assert.assertEquals(codegen.toEnumVarName("enum-value", "String"), "enum_value");
Assert.assertEquals(codegen.toEnumVarName("enumValue", "String"), "enumValue");
Assert.assertEquals(codegen.toEnumVarName("enum**Value", "String"), "enum_Value");
Assert.assertEquals(codegen.toEnumVarName("==", "String"), "__");
Assert.assertEquals(codegen.toEnumVarName("_,.", "String"), "__");
Assert.assertEquals(codegen.toEnumVarName("_", "String"), "UNDERSCORE");
Assert.assertEquals(codegen.toEnumVarName("___", "String"), "___");
Assert.assertEquals(codegen.toEnumVarName("1a", "String"), "_1a");
Assert.assertEquals(codegen.toEnumVarName("$name", "String"), "$name");
Assert.assertEquals(codegen.toEnumVarName("nam$$e", "String"), "nam$$e");
Assert.assertEquals(codegen.toEnumVarName("user-name", "String"), "user_name");
Assert.assertEquals(codegen.toEnumVarName("user_name", "String"), "user_name");
codegen.setCamelCaseDollarSign(false);
Assert.assertEquals(codegen.toEnumVarName("$name", "String"), "$name");
Assert.assertEquals(codegen.toEnumVarName("$NamE", "String"), "$NamE");
codegen.setCamelCaseDollarSign(true);
Assert.assertEquals(codegen.toEnumVarName("$name", "String"), "$name");
}

Expand All @@ -112,31 +126,59 @@ public void toEnumVarName_snake_case() {
codegen.setEnumPropertyNaming(CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.snake_case);
Assert.assertEquals(codegen.toEnumVarName("enum-value", "String"), "enum_value");
Assert.assertEquals(codegen.toEnumVarName("enumValue", "String"), "enumValue");
Assert.assertEquals(codegen.toEnumVarName("$name", "String"), "_name");
Assert.assertEquals(codegen.toEnumVarName("enum**Value", "String"), "enum_Value");
Assert.assertEquals(codegen.toEnumVarName("==", "String"), "__");
Assert.assertEquals(codegen.toEnumVarName("___", "String"), "___");
Assert.assertEquals(codegen.toEnumVarName("1a", "String"), "_1a");
codegen.setCamelCaseDollarSign(false);
Assert.assertEquals(codegen.toEnumVarName("$name", "String"), "$name");
Assert.assertEquals(codegen.toEnumVarName("$NamE", "String"), "$namE");
codegen.setCamelCaseDollarSign(true);
Assert.assertEquals(codegen.toEnumVarName("$name", "String"), "$Name");
}

@Test
public void toEnumVarName_PascalCase() {
codegen.setEnumPropertyNaming(CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.PascalCase);
Assert.assertEquals(codegen.toEnumVarName("enum-value", "String"), "EnumValue");
Assert.assertEquals(codegen.toEnumVarName("enumValue", "String"), "EnumValue");
Assert.assertEquals(codegen.toEnumVarName("1a", "String"), "_1a");
Assert.assertEquals(codegen.toEnumVarName("==", "String"), "__");
Assert.assertEquals(codegen.toEnumVarName("___", "String"), "___");
Assert.assertEquals(codegen.toEnumVarName("enum-value", "String"), "EnumValue");
codegen.setCamelCaseDollarSign(false);
Assert.assertEquals(codegen.toEnumVarName("$name", "String"), "$name");
Assert.assertEquals(codegen.toEnumVarName("$NamE", "String"), "$namE");
codegen.setCamelCaseDollarSign(true);
Assert.assertEquals(codegen.toEnumVarName("$name", "String"), "$Name");
Assert.assertEquals(codegen.toEnumVarName("$NamE", "String"), "$NamE");
}

@Test
public void toEnumVarName_camelCase() {
codegen.setEnumPropertyNaming(CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.camelCase);
Assert.assertEquals(codegen.toEnumVarName("enum-value", "String"), "enumValue");
Assert.assertEquals(codegen.toEnumVarName("enumValue", "String"), "enumValue");
Assert.assertEquals(codegen.toEnumVarName("ABcD", "String"), "aBcD");
Assert.assertEquals(codegen.toEnumVarName("==", "String"), "__");
Assert.assertEquals(codegen.toEnumVarName("___", "String"), "___");
Assert.assertEquals(codegen.toEnumVarName("1a", "String"), "_1a");
codegen.setCamelCaseDollarSign(false);
Assert.assertEquals(codegen.toEnumVarName("$name", "String"), "$name");
codegen.setCamelCaseDollarSign(true);
Assert.assertEquals(codegen.toEnumVarName("$name", "String"), "$Name");
}

@Test
public void toEnumVarName_UPPERCASE() {
codegen.setEnumPropertyNaming(CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.UPPERCASE);
Assert.assertEquals(codegen.toEnumVarName("enum-value", "String"), "ENUM_VALUE");
Assert.assertEquals(codegen.toEnumVarName("enumValue", "String"), "ENUM_VALUE");
Assert.assertEquals(codegen.toEnumVarName("___", "String"), "___");
Assert.assertEquals(codegen.toEnumVarName("1a", "String"), "_1A");
Assert.assertEquals(codegen.toEnumVarName("$name", "String"), "_NAME");
Assert.assertEquals(codegen.toEnumVarName("nam$$e", "String"), "NAM_E");
Assert.assertEquals(codegen.toEnumVarName("user-name", "String"), "USER_NAME");
Assert.assertEquals(codegen.toEnumVarName("user_name", "String"), "USER_NAME");
}

@Test
Expand Down Expand Up @@ -188,6 +230,7 @@ public void convertVarName() {
Assert.assertEquals(codegen.toVarName("1A"), "_1A");
Assert.assertEquals(codegen.toVarName("1AAAA"), "_1AAAA");
Assert.assertEquals(codegen.toVarName("1AAaa"), "_1aAaa");
Assert.assertEquals(codegen.toVarName("__"), "__");
}

@Test
Expand Down

0 comments on commit a856f5f

Please sign in to comment.