smartstruct icon indicating copy to clipboard operation
smartstruct copied to clipboard

Named constructor support

Open smotastic opened this issue 3 years ago • 0 comments

Right now the first found constructor is used for populating data initially.

ConstructorElement _chooseConstructor(
    ClassElement outputClass, ClassElement _) {
  return outputClass.constructors.where((element) => !element.isFactory).first;
}

It would be nice, if in the Mapping Annotation you can define the constructor to use.

class Bar {
  final String text;
  Bar() : this.text = 'should not be used by mapper';
  Bar.useThis(this.text);
}

@Mapper()
class Mapper {
  @Mapping(constructor='useThis')
  Foo fromBar(Bar source);
}

Generated Mapper should then use the "useThis" constructor.

In the future we might use constructor tear off, right now it's not supported by dart and only in the beta. https://github.com/dart-lang/language/blob/master/accepted/future-releases/constructor-tearoffs/feature-specification.md

smotastic avatar Jun 03 '21 16:06 smotastic