Skip to content
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

Add 3 bundled converters for DateTimeFormatter, DateFormat, CharSequence #1093

Open
magicprinc opened this issue Jan 15, 2024 · 3 comments
Open
Labels
enhancement New feature or request

Comments

@magicprinc
Copy link

.withConverter(DateFormat.class, 100, SimpleDateFormat::new)
.withConverter(DateTimeFormatter.class, 100, DateTimeFormatter::ofPattern)
.withConverter(CharSequence.class, 100, Object::toString)
@radcortez radcortez added the enhancement New feature or request label Jan 16, 2024
@magicprinc
Copy link
Author

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));
  }
})

@radcortez
Copy link
Member

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!

@magicprinc
Copy link
Author

magicprinc commented Mar 23, 2024

Example application. Shows

  1. Spring / Spring Boot integration (2/3 steps: SmallRyeConfig bean, integration into Environment/@Value; × @ConfigProperty)
  2. useful one line converters: DateFormat, DateTimeFormatter, CharSequence, Charset, Locale

https://github.com/magicprinc/SmallRyeConfig-SpringBoot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants