Skip to content

[#7446] Handle special characters followed by digits in http-client-java's CodeNamer.getEnumMemberName #7448

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

Open
wants to merge 1 commit 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 @@ -94,6 +94,15 @@ public static String getEnumMemberName(String name) {
return name;
}

// Special handling for inputs that start with special characters followed by numbers
if (name.length() > 1 && !Character.isLetterOrDigit(name.charAt(0)) && Character.isDigit(name.charAt(1))) {
Copy link
Author

Choose a reason for hiding this comment

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

I could add name.length() >= 2 if that is more readable/less of an index out of bounds mind phrack

// Convert the first character to its word form
String firstCharReplacement = getBasicLatinCharacter(name.charAt(0));
if (firstCharReplacement != null) {
name = firstCharReplacement + "_" + name.substring(1);
}
}

// trim leading and trailing '_'
if ((name.startsWith("_") || name.endsWith("_")) && !name.chars().allMatch(c -> c == '_')) {
StringBuilder sb = new StringBuilder(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public void testEnumMemberName() {

Assertions.assertEquals("ONE_ZEROMINUTELY", CodeNamer.getEnumMemberName("_10minutely"));

Assertions.assertEquals("DOLLAR_SIGN_100_GIFT_CARD", CodeNamer.getEnumMemberName("$100 Gift Card"));
Copy link
Contributor

Choose a reason for hiding this comment

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

I see, without the fix the output would be 100_GIFT_CARD, which is not a valid var name in Java.

There is 2 concern from me, on the output.

  1. _100 Gift Card would output ONE_ZERO_ZERO_GIFT_CARD, thus from existing logic, it may be more reasonable to output same for $100 Gift Card (I know this ONE_ZERO_ZERO_GIFT_CARD looks bad...)
  2. #$100 Gift Card would still generate 100_GIFT_CARD

I am of pretty weak opinion on item (1), but I would like to see if we had something consistent for item (2).

BTW, if one uses TypeSpec, one would have the option to explicitly write the name of the enum, thus avoid this logic to infer the var name.
https://typespec.io/docs/language-basics/enums/#assigning-values-to-enums


Assertions.assertEquals("PERCENT_SIGN_50_OFF", CodeNamer.getEnumMemberName("%50 Off"));

Assertions.assertEquals("NUMBER_SIGN_10_TRENDING", CodeNamer.getEnumMemberName("#10 Trending"));

Assertions.assertEquals("_", CodeNamer.getEnumMemberName("_"));
}
}
Loading