leancodepl / contractsgenerator-dart

Dart contracts client generator for a CQRS API
3 stars 1 forks source link

Automatically generate `copyWith` #43

Open bartekpacia opened 2 years ago

bartekpacia commented 2 years ago

It'd be useful if DTOs (or all classes, so commands and queries as well? I really don't know) already came with copyWith() generated.

There could be some flag in contracts_generator.yaml to enable copyWith() generation (to reduce size of the generated code and time of generation when the project doesn't need this feature).

shilangyu commented 2 years ago

Maybe the copy withs should be generated in a separate file as extension methods (which then will be re-exported by the contracts)? It will be annoying that contracts will have to be imported to be able to use copy with, but in return it will keep the original contracts file clean.

What do you think?

bartekpacia commented 2 years ago

IMHO it's okay :)

shilangyu commented 2 years ago

I would like to implement a more modular approach where anyone could provide custom annotations to generated classes. Then, one could annotated DTOs with @CopyWith() if they please. However this has a few issues:

  1. Usually annotating is not enough and some extra modifications have to be applied to the class (for example @JsonSerializable() needs a fromJson constructor and a toJson method)
  2. It needs a more granular approach than "annotate all"/"annotate none" (for example @CopyWith() will crash if annotated on an incorrect class). The means we would need some user-provided resolving unit, ie more than a config file

I hope that point 1 will get solved once promised metaprogramming lands in dart

bartekpacia commented 2 years ago

Thanks for explaining 👍