-
Notifications
You must be signed in to change notification settings - Fork 20
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
Fix/41 nullable list #58
Conversation
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.
Hi @petrnymsa
Thank you for the contribution.
Unfortunately I just merged a quite big change, and there are some conflicts which need to be resolved first.
@smotastic Hi, thanks for info. I've tested new commits and unfortunately there is still broken Static mapping. With this new change, it is not possible add helper methods for map Enums, List, Maps, Set or anything that is not a class as it generates invalid code
both will generate something like
for List it wil generate similar code. So for now within my fork I've completely reverted static mapping as it is not usable in my application. |
@petrnymsa Hi. You can try to move the static method '_mapEnumFoo' and '_mapNames' out of the mapper class. Just like this: enum DogType { happy, angry }
// TARGET
class Dog {
final String name;
final String breed;
final int age;
final DogType dogType;
Dog(this.name, this.breed, this.age, this.dogType);
}
// SOURCE
class DogModel {
final String dogName;
final String breed;
final int dogAge;
final String dogType;
DogModel(this.dogName, this.breed, this.dogAge, this.dogType);
}
/// Mapper showcasing explicit fieldmapping in case fields do not match their respective fieldnames
@Mapper()
abstract class DogMapper {
@Mapping(source: 'dogName', target: 'name')
@Mapping(source: 'dogAge', target: 'age')
@Mapping(source: _mapDogType, target: 'dogType')
Dog fromDogModel(DogModel model);
}
DogType _mapDogType(DogModel model) {
if (model.dogType == 'angry') return DogType.angry;
return DogType.happy;
} The generated code : class DogMapperImpl extends DogMapper {
DogMapperImpl() : super();
@override
Dog fromDogModel(DogModel model) {
final dog =
Dog(model.dogName, model.breed, model.dogAge, _mapDogType(model));
return dog;
}
} The feature you need is function mapping. The static method in the mapper class can be used as a "function mapping", but there is some tricky about it. If the return type of a static method in the mapper class is not a primitive type, it will be recognized as a "static mapping". For the "static mapping" the function named"$[methodName]" will be generated. This is why the function '$_mapEnumFoo' which has error code is generated. When you move the function out of the mapper class. It will not be recognized as a "static mapping", so the function ' _$_mapEnumFoo' which has error code will not be generated. But it is also a "function mapping" which you need. There is the example about 'static mapping': and the example about 'function mapping': |
The previous way of working with enums was quite sensible and clean. I hope we can get back to how it was done without reviewing the whole code. Perhaps more unit tests are required to avoid this sort of regression/breaking changes. |
c32d2c8
to
22e657b
Compare
@smotastic Hi, I've updated code to newest develop. Do you think, that you can take a look? I am not so familiar with analyer API so propably there is an easier way. however code which checks for nullability var sourceIsNullable = sourceListType.nullabilitySuffix == NullabilitySuffix.question;
var targetIsNullable = targetListType.nullabilitySuffix == NullabilitySuffix.question; for nullale lists parameter this will return false, so i've used directly sourceField.type.nullabilitySuffix |
Hi @petrnymsa |
Fixes #41