schultek / dart_mappable

Improved json serialization and data classes with full support for generics, inheritance, customization and more.
https://pub.dev/packages/dart_mappable
MIT License
148 stars 22 forks source link

Enabling deep copyWith on custom collection/iterables #28

Open point-source opened 1 year ago

point-source commented 1 year ago

Is there a way to enable dart_mappable to support deep copy on custom iterables and collections (such as those from fast_immutable_collections)? There is currently a way to support serialization on these custom types via custom mappers and it would be great to extend this type of support to the deep copy feature as well.

Specifically to enable syntax such as:

final myinstance = MyClass(myCustomIterable: <String>[]);
final MyClass myInstanceCopy = myinstance.copyWith.myCustomIterable.add('string');
schultek commented 1 year ago

I can look into it. I think the problem with fic is that they don't implement the normal list and map mixins, so any implementation needs to be specific to fic.

point-source commented 1 year ago

Yeah that's why I was thinking about having a class similar to the way BaseMapper works. Then the user could implement each method necessary.

schultek commented 1 year ago

I've added an example on the feat/support-fic branch. It uses a new SerializableMapper that works with json_serializable-style classes. It also sets up a custom copyWith extension.

Right now there is no new code-generation, but all manual setup. But i can see how the wiring for the copywith extension could be generated for the ImmutableListCopyWith class with some extra annotation.

AlexanderFarkas commented 1 year ago

Wouldn't this work for FIC?

final MyClass myInstanceCopy = myinstance.copyWith.myCustomIterable.apply((iterable) => iterable.add('string'));