smotastic / smartstruct

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

Getters are included in generated mapping #84

Open petrnymsa opened 1 year ago

petrnymsa commented 1 year ago

Often classes contains getters without explicit setter (computed getter,...). In such case, generated code tries to assign value to them.

Run smartstruct 1.4.0 against this mapper:

class GetterTarget {
  final String name;
  final int age;

  int foo = 0;

  GetterTarget({
    required this.name,
    required this.age,
  });

  List<Object?> get props => [name, age];

  bool get sample => false;
}

class GetterSource {
  final String name;
  final int age;

  int foo = 1;

  GetterSource({
    required this.name,
    required this.age,
  });

  List<Object?> get props => [name, age];

  bool get sample => true;
}

@Mapper()
abstract class GetterMapper {
  GetterTarget fromModel(GetterSource source);
}

Generated mapping


class GetterMapperImpl extends GetterMapper {
  GetterMapperImpl() : super();

  @override
  GetterTarget fromModel(GetterSource source) {
    final gettertarget = GetterTarget(
      name: source.name,
      age: source.age,
    );
    gettertarget.foo = source.foo;
     // NO SETTER !
    gettertarget.props = source.props.map((e) => e).toList();

   // NO SETTER !
    gettertarget.sample = source.sample;
    return gettertarget;
  }
}

This can happen alot if project uses Equatable, where props, stringify and hashCode is used.