Skip to content

#3628 Add locale parameter for numberFormat#3693

Merged
filiphr merged 19 commits intomapstruct:mainfrom
Obolrom:3628_number_format_locale_param
Nov 17, 2024
Merged

#3628 Add locale parameter for numberFormat#3693
filiphr merged 19 commits intomapstruct:mainfrom
Obolrom:3628_number_format_locale_param

Conversation

@Obolrom
Copy link
Contributor

@Obolrom Obolrom commented Sep 3, 2024

Implementation of enhancement #3628

  • add Mapping#locale param
  • adjust decimal format creation logic
  • add test case for other default lang
  • support locale param for Mapping#dateFormat
  • integrate locale param for MapMapping & IterableMapping annotations
  • add documentation for locale params

e.g.

@Mapping( target = "bigDecimal1", numberFormat = "#0.#E0", locale = "en/US" )

Fixes #3628

@Obolrom Obolrom marked this pull request as ready for review September 3, 2024 12:55
Copy link
Member

@filiphr filiphr left a comment

Choose a reason for hiding this comment

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

Thanks for your work on this @Obolrom. It is on the right track. However, it still needs some work before it is in a mergeable state. I've left some inline comments, but there are some other more general remarks as well:

  • The use of new Locale public constructor. This constructor has been deprecated in Java 19 and there is Locale.forLanguageTag available since Java 7, let's use that everywhere instead.
  • I see that we are only supporting the numberFormat in the @Mapping for this. However, we also have dateFormat in @Mapping and we also have the following (if we are adding the locale we need to support it everywhere):
    • MapMapping#keyNumberFormat
    • MapMapping#keyDateFormat
    • MapMapping#valueNumberFormat
    • MapMapping#valueDateFormat
    • IterableMapping#numberFormat
    • IterableMapping#dateFormat
  • I don't see any tests with a custom locale. The NumberFormatConversionRuLocaleTest is identical to NumberFormatConversionTest, with the only difference being that a different default locale is used. What we need instead is in the NumberFormatConversionTest (which runs using the default en locale to have mappers using a custom locale.
  • An additional improvement could be that we store the passed Locale to the method in a field, but this is more complex and this can be an additional improvement.

@Obolrom Obolrom requested a review from filiphr September 4, 2024 12:02
@Obolrom
Copy link
Contributor Author

Obolrom commented Sep 4, 2024

@filiphr, thank you for the quick feedback.
I have integrated locale param for IterableMapping & MapMapping, added documentation, fixed tests, and made the code a little cleaner. So it is ready for review.

However, I didn't understand your idea about

  • An additional improvement could be that we store the passed Locale to the method in a field, but this is more complex and this can be an additional improvement.

@filiphr
Copy link
Member

filiphr commented Sep 13, 2024

However, I didn't understand your idea about

Ignore that for now. It isn't that important

Copy link
Member

@filiphr filiphr left a comment

Choose a reason for hiding this comment

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

I've missed some things in my previous review. Have a look at my comments. We strive to have a readable generated code. What is being done now is not really readable, let's try to not have full qualified names if possible.

Comment on lines 79 to 87
if ( conversionContext.getLocale() != null ) {
sb.append( ", java.util.Locale.forLanguageTag( \"" );
sb.append( conversionContext.getLocale() );
sb.append( "\" )" );
}
else {
sb.append( ", java.util.Locale.getDefault() " );
}
Copy link
Member

Choose a reason for hiding this comment

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

We strive to have as much readable code as possible. having a full qualified name here isn't readable 😉. Please have a look at how we are doing it in the getFromExpression for the BigDecimal import

Comment on lines 87 to 95
if ( conversionContext.getLocale() != null ) {
sb.append( ", Locale.forLanguageTag( \"" );
sb.append( conversionContext.getLocale() );
sb.append( "\" )" );
}
else {
sb.append( ", Locale.getDefault() " );
}
Copy link
Member

Choose a reason for hiding this comment

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

Same comment as in BigDecimalToStringConversion

@Obolrom
Copy link
Contributor Author

Obolrom commented Sep 14, 2024

@filiphr
I have fixed the issues you mentioned in the last comment.

However, right now we have some code duplications
WrapperToStringConversion::appendDecimalFormatter is identical to PrimitiveToStringConversion::appendDecimalFormatter
and
BigDecimalToStringConversion::appendDecimalFormatter is identical to BigIntegerToStringConversion::appendDecimalFormatter.

We can make appendDecimalFormatter method abstract and move it to AbstractNumberToStringConversion.
However, in this case, the code will still be duplicated.
What do you think of that?

@Obolrom Obolrom requested a review from filiphr September 14, 2024 09:49
@Obolrom Obolrom force-pushed the 3628_number_format_locale_param branch from 1580228 to 2d461c8 Compare September 15, 2024 18:31
@Obolrom Obolrom force-pushed the 3628_number_format_locale_param branch from 2d461c8 to 7388038 Compare September 24, 2024 10:29
@Obolrom
Copy link
Contributor Author

Obolrom commented Nov 3, 2024

@filiphr
Kind reminder to review PR after changes 😄

@filiphr
Copy link
Member

filiphr commented Nov 3, 2024

Looks better @Obolrom. However, I see that there is a change in behaviour and now we always pass Locale.getDefault to certain places. I would say that we should only pass some locale if there is something set by the user. Otherwise, we keep doing what we were doing before.

i.e. there should be no need to change anything in existing tests.

@filiphr
Copy link
Member

filiphr commented Nov 3, 2024

If you are busy, I can apply these changes while merging

@Obolrom
Copy link
Contributor Author

Obolrom commented Nov 3, 2024

@filiphr
It would be great if we close this PR today.
Feel free to apply these changes

@filiphr
Copy link
Member

filiphr commented Nov 3, 2024

@Obolrom it will most definitely not be closed today. I'll apply the changes when I start the merge

@filiphr filiphr merged commit 737af6b into mapstruct:main Nov 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add locale parameter for numberFormat

2 participants