smartstruct icon indicating copy to clipboard operation
smartstruct copied to clipboard

feat: generate extension methods

Open mrverdant13 opened this issue 4 years ago • 2 comments

Description

Given the following classes:

class Dog {
  final String breed;
  final int age;
  final String name;
  Dog(this.breed, this.age, this.name);
}

class DogModel {
  final String breed;
  final int age;
  final String name;
  DogModel(this.breed, this.age, this.name);
}

And given the following mapper interface:

// dogmapper.dart

part 'dogmapper.mapper.g.dart';

@Mapper()
abstract class DogMapper {
  Dog asEntity(DogModel model);
}

Instead of generating a mapper class, it would be great to generate mapper extension methods as follows:

// dogmapper.mapper.g.dart

extension ConvertibleDogModel on DogModel {
  Dog get asEntity => Dog(breed, age, name);
}

mrverdant13 avatar Jun 28 '21 07:06 mrverdant13

Hey @mrverdant13 I like the idea, I'm thinking of maybe making this configurable. So that you would be able to write something like

@Mapper(extension: true)
...

And instead of implementing the mapper class, it would generate extension methods like you suggested.

I don't know how much time I'll have in the weeks, but I'll see what I can do. Or if you feel like it, feel free to open a PR

smotastic avatar Jun 28 '21 16:06 smotastic

Definitely gonna check this out and try to post a PR.

BTW, thanks for the package! I was about to create one with just similar capabilities! 👀

mrverdant13 avatar Jun 28 '21 16:06 mrverdant13