-
Notifications
You must be signed in to change notification settings - Fork 9
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
Implement Type Converters #65
Conversation
@AutoMappr( | ||
[ | ||
MapType<DateDto, DateDomain>( | ||
fields: [ | ||
Field( | ||
'dateTimeA', | ||
type: TypeConverter<String, DateTime>(Mappr.tryParseFirstOfDecemberInYear), | ||
), | ||
], | ||
types: [ | ||
TypeConverter<String, DateTime>(Mappr.parseSecondOfDecemberInYear), | ||
], | ||
), | ||
MapType<DateDto2, DateDomain>(), | ||
MapType<UserDto, User>( | ||
fields: [ | ||
Field('id', type: TypeConverter<String, UserId>(UserId.new)), | ||
Field('accountId', type: TypeConverter<String, AccountId>(AccountId.new)), | ||
], | ||
), | ||
], | ||
types: [ | ||
TypeConverter<String, DateTime>(Mappr.parseISO8601), | ||
], | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any benefit of doing TypeConverter<String, AccountId>(SomeFunction.xxx)
instead of just passing SomeFunction.xxx
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope probably not the generic could probably be moved to the function level. I'll try that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually you need the explicit definition because inference doesn't seem to work without it. The wrapper Object is also there to extend it with further configuration options.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure right now, but isn't it similar to custom
where it also works for functions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, it would probably work without the wrapper object. But then we'd lose the option to configure the TypeConverter further with e.g. field name constraints.
For brevity we could support both passing just the function or the whole Object I guess. What do you think?
About constraining The Also I see two options for constraining a single TypeConverter to multiple field names. Either we support Regex patterns or we allow specifying a list of allowed field names. |
You mean #1, right? Aka flattening |
If we can have type convertors for fields, maptype, automapper, do we need further filtering by field? |
Yes I do think that would still a good or even required addition. Additionally lets say across all models you have a |
What do you think, @petrnymsa? |
dabc193
to
dbc285d
Compare
I am kind of against to have every possible feature in the package, but overall I like idea of TypeConverters. Taking your example with timestamps - I dont think that this is something, that you should solve with AutoMappr, as it is more "workaround of bad app design" than solution. If some arbirtrary API returns different Date formats, you should transfer it into proper format and in the domain level of your application you should have, one defined format for all date times etc. ... So, I am definitely for this feature if it is just
On the Field level - I dont think it is needed - you can acomplish any transformation with "custom" But there is one more issue to discuss Suppose we have mapping from A -> B, and A has nested class C, B has nested D, .e.g class A {
final C foo;
}
class B {
final D foo;
}
class C {}
class D {} Right now, AutoMappr will try to automatically map from C->D if there is defined With TypeConverters we can potentially have another way to solve nested mapping
This can be maybe little bit confusing, so it should be clarified that
Moreover, if is it second case, we already have issue #6, ideally we should not need to defined these nested mappings if theirs purpose is only, and only, for nested mapping... |
That's a good point that we could probably allow doing So the question is, how are |
I do think this is something my mapping code should take care of. Most of the time you don't have the luxury of a great API with sensible formats for values. In our case we mostly generate Dart API Clients from openapi specs and then provide an intermediary that maps these generated APIs to domain level defined ones. Mapping these generated DTOs to our domain objects is exactly the use case I'd expect a mapping library to cover. That includes having the option to map badly designed types.
I agree, there was no use case as of yet where that was necessary.
I'm still in the process of replacing my existing mapping code base with auto_mappr and that exact question also appeared and I've actually "abused" the |
Any updates on type converters? This is a real pain point right now. |
Will be implemented in a different PR, since this PR depends on way too old code. Note that there is a proposal for this feature in last comments of #60. |
Close #60
This is just a rough first implementation of the feature with very few tests etc. etc.
I'm open for any input about how things should work.
Another addition I want to propose is specifying field names (or even patterns) for Type Converters. This would allow globally specifying a
UserId
TypeConverter
that converts fromString
toUserId
if the field has the nameuserId
.