Comments (6)
I need this functionality so I took a look at the code and I think I see how this could be implemented. I want to clarify the requirements though, if possible.
In cases when Object is expected (source or target) and a type converter is set with Object?, this converter should handle it.
Does this mean if we had a TypeConverter<Object?, Object?>
it can be used for all conversions?
- Object -> Object
- Object -> Object?
- Object? -> Object
- Object? -> Object?
I think anything with -> Object
could be a problem for this type converter, since the function that will handle the conversion could possibly return null. I think it's reasonable for it handle both Object ->
and Object? ->
though, since the conversion function would be built to handle a nullable input, which can also be used to handle a non-nullable input just fine.
In cases when Object? is expected (source or target) and a type converter is set with Object, this converter should not be used.
So in this case, if we had a type converter TypeConverter<Object, Object>
, it would not be usable to any conversions that have a nullable source or target? I think it could technically handle -> Object?
conversions as well, since the non-null value returned by the conversion function could still be assigned to a nullable variable, but I'm not sure if it should handle it or not.
So I think maybe there is a design choice to be made. TypeConverters with nullable generics can technically handle some non-nullable cases safely, and TypeConverters with non-nullable generics can technically handle some nullable cases safely, but I guess the decision is whether they should or not. We could make them really strict so that the type and the nullability of the type has to match exactly, and that might be easier to understand for someone using the API, but it would also mean that their code might have to be more verbose to cover all the scenarios they need.
What do you think?
from auto_mappr.
Thanks for your quick reviews and release! I'm using the feature now. 🎉
from auto_mappr.
Hi @ekerik220 thanks for your initiative! Hmm, you are probably right. Thinking about it more, should it work like this?:
Aka choosing the solution that works the same way as if you had some converter function and manually using it in code. And in the end, that's exactly what type converter is doing under the hood - you assign such a function to it.
TypeConverter<Object, Object>
... akaObject converter(Object) => ...
Object
->Object
Object
->Object?
TypeConverter<Object, Object?>
... akaObject? converter(Object) => ...
Object
->Object?
TypeConverter<Object?, Object>
... akaObject converter(Object?) => ...
Object
->Object
Object?
->Object
Object
->Object?
Object?
->Object?
TypeConverter<Object?, Object?>
... akaObject? converter(Object?) => ...
Object
->Object?
Object?
->Object?
So the most universal type converter that can handle every case will be TypeConverter<Object?, Object>
probably?
This enhancement should be well-tested for sure.
from auto_mappr.
The test should be similar to the lists I posted above, aka 4 groups with 9 tests total. That should test every case.
from auto_mappr.
That looks good to me! For sure, this enhancement has a lot of possible cases so all of the cases should be tested.
Do you have any suggestions on how this should be tested? I think generating and using a mapper like in the current integration tests gets us part of the way there, and maybe unit testing TypeConverter.canBeUsed
would be good too.
I made a quick implementation that I think might work. No tests yet, but let me know if you think it should be done differently or if I'm missing something. #130
from auto_mappr.
So released in auto_mappr 2.1.1 and auto_mappr_annotation 2.1.0 🎉
from auto_mappr.
Related Issues (20)
- Support reverse flag in build.yaml config HOT 2
- Global converter not found when used with reverse flag HOT 2
- Strict mode HOT 6
- Build Options as Config object HOT 1
- Switch to DCM OSS
- Custom MapType HOT 5
- Generation fails when required TARGET field doesn't exist in SOURCE even with whenNull HOT 2
- Combining multiple SOURCEs into one single TARGET HOT 2
- Custom Mapping Futures HOT 3
- Async support HOT 2
- Anyone using auto_mappr in production? HOT 2
- Support field name mapping in reverse mode
- Global Methods dont work with out type HOT 10
- Field ignores whenNull and ignoreNull HOT 3
- Generated code for classes without ctors ignores 'from' argument HOT 7
- Move where from extension to static function
- Extend tryConvert method for catching mapping exceptions
- Enum set to null for unknown values HOT 1
- Possibility to ignore elements in a list when mapping goes wrong for one element HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from auto_mappr.