smallrye-config
smallrye-config copied to clipboard
Add 3 bundled converters for DateTimeFormatter, DateFormat, CharSequence
.withConverter(DateFormat.class, 100, SimpleDateFormat::new)
.withConverter(DateTimeFormatter.class, 100, DateTimeFormatter::ofPattern)
.withConverter(CharSequence.class, 100, Object::toString)
And probably Locale… Current Locale conversion is quite bad.
How about:
.withConverter(Locale.class, 101, s->{
if (s == null || s.isBlank()){
return null;
} else if (found(s.indexOf('-'))){
return Locale.forLanguageTag(trim(s));// de-DE
} else {// uses Guava, because it is a copy-paste of real code. Can be replaced with String.split
List<String> elements = Splitter.on('_').limit(3).trimResults().splitToList(s);
return elements.size() == 1 ? new Locale(elements.get(0)) // de
: elements.size() == 2 ? new Locale(elements.get(0), elements.get(1)) // de_DE
: new Locale(elements.get(0), elements.get(1), elements.get(2));
}
})
We don't provide a specific Locale converter. In cases where there isn't one, an implicit Converter is determined by a lookup of static methods of, valueOf, parse, and String constructor (which matches how a Locale is converted).
This may be inconvenient in some cases, but it does cover a big spectrum of combinations. Feel free to provide a PR to improve it. Thank you!
Example application. Shows
- Spring / Spring Boot integration (2/3 steps: SmallRyeConfig bean, integration into Environment/
@Value; ×@ConfigProperty) - useful one line converters: DateFormat, DateTimeFormatter, CharSequence, Charset, Locale
https://github.com/magicprinc/SmallRyeConfig-SpringBoot