-
Notifications
You must be signed in to change notification settings - Fork 424
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
Use toString() to match enum constants #830
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11987,19 +11987,17 @@ private ITypeConverter<?> getTypeConverter(final Class<?> type, ArgSpec argSpec, | |
return new ITypeConverter<Object>() { | ||
@SuppressWarnings("unchecked") | ||
public Object convert(String value) throws Exception { | ||
String sensitivity = "case-sensitive"; | ||
if (commandSpec.parser().caseInsensitiveEnumValuesAllowed()) { | ||
String upper = value.toUpperCase(); | ||
try { return Enum.valueOf((Class<Enum>) type, value); } | ||
catch (IllegalArgumentException ex) { | ||
boolean insensitive = commandSpec.parser().caseInsensitiveEnumValuesAllowed(); | ||
for (Object enumConstant : type.getEnumConstants()) { | ||
if (upper.equals(String.valueOf(enumConstant).toUpperCase())) { return enumConstant; } | ||
String thisName = enumConstant.toString(); | ||
if( value.equals(thisName) || insensitive && value.equalsIgnoreCase(thisName) ) { return enumConstant; } | ||
} | ||
sensitivity = "case-insensitive"; | ||
} | ||
try { return Enum.valueOf((Class<Enum>) type, value); } | ||
catch (Exception ex) { | ||
String sensitivity = insensitive ? "case-insensitive" : "case-sensitive"; | ||
Enum<?>[] constants = ((Class<Enum<?>>) type).getEnumConstants(); | ||
String[] names = new String[constants.length]; | ||
for (int i = 0; i < names.length; i++) { names[i] = constants[i].name(); } | ||
for (int i = 0; i < names.length; i++) { names[i] = constants[i].toString(); } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When no match is found, we want to report the enum constant names, not their toString() values: see #592 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, using |
||
throw new TypeConversionException( | ||
String.format("expected one of %s (%s) but was '%s'", Arrays.asList(names), sensitivity, value)); } | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a bugfix to also compare
enumConstant.name()
case-insensitively:Also, please keep the whitespace conventions the same as the original. :-) So,
if (xxx)
instead ofif( xxx )
.