Skip to content

Add MappableBase<TSelf> interface to guarantee toMap, toJson, and copyWith across generated models#317

Open
okandemirofficial wants to merge 2 commits intoschultek:mainfrom
okandemirofficial:feat/mappable_base
Open

Add MappableBase<TSelf> interface to guarantee toMap, toJson, and copyWith across generated models#317
okandemirofficial wants to merge 2 commits intoschultek:mainfrom
okandemirofficial:feat/mappable_base

Conversation

@okandemirofficial
Copy link

@okandemirofficial okandemirofficial commented Oct 8, 2025

Summary

This PR introduces a small, self-referential generic interface MappableBase<TSelf extends MappableBase>.
All generated model mixins now implement MappableBase, so generic services can rely on toMap(), toJson(), and copyWith without extra flags.

Why

I often write generic helpers/services that should work with any mappable type:

class MyService<T extends MappableBase> {
  void convertToJson(T value) {
    final json = value.toMap();
  }
}

Having a shared capability interface lets the compiler enforce these features at call sites.

What changed

Added base mixin MappableBase

mixin MappableBase<T> {
  String toJson();
  Map<String, dynamic> toMap();

  @override
  String toString();

  @override
  bool operator ==(Object other);

  @override
  int get hashCode;

  ClassCopyWith<T, T, T> get copyWith;
}

Updated the generator to emit:

mixin MyModelMappable implements MappableBase {

If this approach doesn’t make sense or there’s a better way to achieve it, please feel free to let me know and disregard the PR.

I really appreciate your work — thanks in advance for taking the time to review this!

@okandemirofficial okandemirofficial changed the title Mappable Base Implementation Add MappableBase<TSelf> interface to guarantee toMap, toJson, and copyWith across generated models Oct 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments