smotastic / smartstruct

Dart Code Generator for generating mapper classes
35 stars 20 forks source link

feat: generate extension methods #21

Open mrverdant13 opened 3 years ago

mrverdant13 commented 3 years ago

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);
}
smotastic commented 3 years ago

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

mrverdant13 commented 3 years ago

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! 👀