Skip to content
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

Merged
merged 3 commits into from
Oct 22, 2022
Merged

Conversation

petrnymsa
Copy link

@petrnymsa petrnymsa commented Aug 9, 2022

Fixes #41

Copy link
Owner

@smotastic smotastic left a 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.

@petrnymsa
Copy link
Author

@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

static EnumFoo _mapEnumFoo(Source model) => model.foo ... 
static List<String> _mapNames(Source model) => model.friends.map((f) => f.name).toList()

both will generate something like

EnumFoo _$_mapEnumFoo(Source model) {
   final enumFoo = EnumFoo._();
   return enumFoo;
}

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.

@skykaka
Copy link
Contributor

skykaka commented Aug 22, 2022

@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':
https://github.com/smotastic/smartstruct/blob/develop/example/lib/static_mapping/static_mapping.dart

and the example about 'function mapping':
https://github.com/smotastic/smartstruct/blob/develop/example/lib/function_mapping/function_mapping.dart

@luissalgadofreire
Copy link

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.

@petrnymsa
Copy link
Author

@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

@smotastic smotastic merged commit 92d44ac into smotastic:develop Oct 22, 2022
@smotastic
Copy link
Owner

Hi @petrnymsa
Really sorry for the late reply.
I am also not that familiar with the API, but I also don't think it's complicated or anything, so I like it.
Thank you for the contribution :)

@petrnymsa petrnymsa deleted the fix/41-nullable-list branch November 6, 2022 10:21
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.

Nullable lists are not being mapped properly.
4 participants