Skip to content

Commit

Permalink
make config enums ignore case (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyfts authored Oct 3, 2024
1 parent ea50d8f commit 96ce85f
Showing 1 changed file with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,20 @@ public void load(@Nullable Object instance, @Nullable String defValueString, Fie
langKey);

try {
boolean saveOnExit = false;
if (!Arrays.asList(possibleValues).contains(value)) {
throw new NoSuchFieldException();
value = Optional.ofNullable(getEnumIgnoredCase(value, possibleValues))
.orElseThrow(NoSuchFieldException::new);
saveOnExit = true;
}
Field enumField = fieldClass.getDeclaredField(value);
if (!enumField.isEnumConstant()) {
throw new NoSuchFieldException();
}
field.set(instance, enumField.get(instance));
if (saveOnExit) {
save(instance, field, config, category, name);
}
} catch (NoSuchFieldException e) {
ConfigurationManager.LOGGER.warn(
"Invalid value {} for enum configuration field {} of type {} in config class {}! Using default value of {}!",
Expand All @@ -370,6 +376,7 @@ public void load(@Nullable Object instance, @Nullable String defValueString, Fie
field.getDeclaringClass().getName(),
defaultValue);
field.set(instance, defaultValue);
save(instance, field, config, category, name);
}
}

Expand Down Expand Up @@ -399,6 +406,15 @@ private Enum<?> fromStringOrDefault(@Nullable Object instance, @Nullable String

return value;
}

private @Nullable String getEnumIgnoredCase(String value, String[] validValues) {
for (String valid : validValues) {
if (valid.equalsIgnoreCase(value)) {
return valid;
}
}
return null;
}
}

private static class StringArrayParser implements Parser {
Expand Down

0 comments on commit 96ce85f

Please sign in to comment.