-
-
Notifications
You must be signed in to change notification settings - Fork 155
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
Specify Format and Culture when converting to strings #887
Comments
Thank you for opening this issue. These would be useful additions. I think we should split it up into two issues, one for the string format and one for the culture. I like the string format API. The culture info seems a bit complex and I'm not sure if we want to add this complexity to the API surface. I like the property approach, what do you think about a simple property/field based culture API like this:
ExampleAn example mapper which uses de-CH for all mappings except for [Mapper]
partial class CarMapper
{
[MapperCulture]
private readonly CultureInfo _defaultCulture = CultureInfo.GetCultureInfo("de-CH");
[MapperCulture]
private readonly CultureInfo _secondaryCulture = CultureInfo.GetCultureInfo("de-DE");
[MapProperty("A", "B", Culture = nameof(_secondaryCulture))]
public Dto MapToDto(Entity entity);
} |
Thank you for the feedback! I realize now that maybe it's better to accept the Only issue I see with your approach is a cardinality mismatch - there's only one default, but you can have more than one instance annotated with If I specify a culture on (I don't know enough about source generators, do you need to annotate all the members you expect to use for codegen?) An attribute on the Mapper can be limited to exactly one, and you're still adding a property to I admit the enum seems a bit superfluous. I'd be happy with just the Default logic, and only one Culture (or maybe |
Thank you for your input! We should definitely go for
ExampleAn example mapper which uses record Entity(DateTime Value0, int Value1);
record Dto(string Value0, string Value1);
[Mapper]
partial class CarMapper
{
[DefaultFormatProvider]
private readonly CultureInfo _defaultCulture = CultureInfo.GetCultureInfo("de-CH");
private readonly IFormatProvider _secondaryCulture = CultureInfo.GetCultureInfo("de-DE");
[MapProperty("Value1", "Value1", FormatProvider = nameof(_secondaryCulture), StringFormat = "dd.mm.yyyy"]
public Dto MapToDto(Entity entity);
}
// simplified generated code of the MapToDto implementation:
target.Value0 = entity.Value0.ToString(_defaultCulture);
target.Value1 = entity.Value1.ToString("dd.mm.yyyy", _secondaryCulture); |
Just wondering why not like this: record Entity(DateTime Value0, int Value1);
record Dto(string Value0, string Value1);
[Mapper]
[DefaultFormatProvider(nameof(_defaultCulture))]
partial class CarMapper
{
private readonly CultureInfo _defaultCulture = CultureInfo.GetCultureInfo("de-CH");
private readonly CultureInfo _secondaryCulture = CultureInfo.GetCultureInfo("de-DE");
[MapProperty("Value1", "Value1", FormatProvider = nameof(_secondaryCulture), StringFormat = "dd.mm.yyyy"]
public Dto MapToDto(Entity entity);
} Since you can just set But it does accomplish the same task, so overall I'm happy. |
During the implementation I thought again on the design and switched back to my first proposal with an additional
A first draft of the PR is available in #929 |
🎉 This issue has been resolved in version 3.3.0-next.4 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
🎉 This issue has been resolved in version 3.3.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Is your feature request related to a problem? Please describe.
Related to the more generic request in #886, the most common use case would be specific string formatting per property.
Describe the solution you'd like
Being able to specify the string format and culture on a per-property basis would go a long way. This could be achieved by augmenting the
[MapProperty]
attribute:When considering string formatting, being able to control the culture is crucial. It's also useful to be able to specify it at a more global level. As such, I propose adding two optional properties
Culture
andCultureName
to[MapProperty]
,[Mapper]
and[MapperDefaults]
.Culture
is a:With the following values and behavior on
MapProperty
:CultureName
is specified:CultureName
. If found, use it.ToString(CultureInfo.GetCultureInfo(CultureName))
.CultureName
is not specified:[Mapper]
if a non-Default Culture or CultureName is set, and apply same logic as above.CultureInfo
, use it.MapperDefaults
and apply the same logic.MapperDefaults.Culture
is also set toDefault
, useCultureInfo.CurrentCulture
.ToString(CultureInfo.CurrentCulture)
.ToString(CultureInfo.InvariantCulture)
This would allow for pretty flexible scenarios:
In instance-based scenarios, this would also allow the culture to be set by the user.
Describe alternatives you've considered
Setting the culture on a global per-thread basis. Not always helpful, because in certain scenarios I need to map to formats expected by heavily localized APIs, or for generating report data in specific formats.
The text was updated successfully, but these errors were encountered: