Support mutation

This commit is contained in:
Tobe O 2019-07-04 14:03:20 -04:00
parent 8cdf71bea5
commit 165dd82813
5 changed files with 35 additions and 23 deletions

View file

@ -11,10 +11,10 @@ class Todo extends _Todo {
Todo({this.text, this.completed}); Todo({this.text, this.completed});
@override @override
final String text; String text;
@override @override
final bool completed; bool completed;
Todo copyWith({String text, bool completed}) { Todo copyWith({String text, bool completed}) {
return Todo( return Todo(

View file

@ -45,10 +45,15 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
clazz.fields.add(Field((b) { clazz.fields.add(Field((b) {
b b
..name = field.name ..name = field.name
..modifier = FieldModifier.final$ // ..modifier = FieldModifier.final$
..annotations.add(CodeExpression(Code('override'))) ..annotations.add(CodeExpression(Code('override')))
..type = convertTypeReference(field.type); ..type = convertTypeReference(field.type);
// Fields should only be forced-final if the original field has no setter.
if (field.setter == null && field is! ShimFieldImpl) {
b.modifier = FieldModifier.final$;
}
for (var el in [field.getter, field]) { for (var el in [field.getter, field]) {
if (el?.documentationComment != null) { if (el?.documentationComment != null) {
b.docs.addAll(el.documentationComment.split('\n')); b.docs.addAll(el.documentationComment.split('\n'));

View file

@ -41,6 +41,13 @@ main() {
deathlyHallows.updatedAt.toIso8601String()); deathlyHallows.updatedAt.toIso8601String());
}); });
test('can be mutated', () {
var b = deathlyHallows.copyWith();
b.author = 'Hey';
expect(b.author, 'Hey');
expect(b.toJson()[BookFields.author], 'Hey');
});
test('heeds @Alias', () { test('heeds @Alias', () {
expect(serializedDeathlyHallows['page_count'], deathlyHallows.pageCount); expect(serializedDeathlyHallows['page_count'], deathlyHallows.pageCount);
expect(serializedDeathlyHallows.keys, isNot(contains('pageCount'))); expect(serializedDeathlyHallows.keys, isNot(contains('pageCount')));

View file

@ -23,32 +23,32 @@ class Book extends _Book {
: this.notModels = List.unmodifiable(notModels ?? []); : this.notModels = List.unmodifiable(notModels ?? []);
@override @override
final String id; String id;
@override @override
final String author; String author;
@override @override
final String title; String title;
@override @override
final String description; String description;
/// The number of pages the book has. /// The number of pages the book has.
@override @override
final int pageCount; int pageCount;
@override @override
final List<double> notModels; List<double> notModels;
@override @override
final String camelCaseString; String camelCaseString;
@override @override
final DateTime createdAt; DateTime createdAt;
@override @override
final DateTime updatedAt; DateTime updatedAt;
Book copyWith( Book copyWith(
{String id, {String id,
@ -126,7 +126,7 @@ class Author extends _Author {
: this.books = List.unmodifiable(books ?? []); : this.books = List.unmodifiable(books ?? []);
@override @override
final String id; String id;
@override @override
final String name; final String name;
@ -148,10 +148,10 @@ class Author extends _Author {
final String obscured; final String obscured;
@override @override
final DateTime createdAt; DateTime createdAt;
@override @override
final DateTime updatedAt; DateTime updatedAt;
Author copyWith( Author copyWith(
{String id, {String id,
@ -221,16 +221,16 @@ class Library extends _Library {
: this.collection = Map.unmodifiable(collection ?? {}); : this.collection = Map.unmodifiable(collection ?? {});
@override @override
final String id; String id;
@override @override
final Map<String, _Book> collection; final Map<String, _Book> collection;
@override @override
final DateTime createdAt; DateTime createdAt;
@override @override
final DateTime updatedAt; DateTime updatedAt;
Library copyWith( Library copyWith(
{String id, {String id,
@ -283,7 +283,7 @@ class Bookmark extends _Bookmark {
super(book); super(book);
@override @override
final String id; String id;
@override @override
final List<int> history; final List<int> history;
@ -295,10 +295,10 @@ class Bookmark extends _Bookmark {
final String comment; final String comment;
@override @override
final DateTime createdAt; DateTime createdAt;
@override @override
final DateTime updatedAt; DateTime updatedAt;
Bookmark copyWith(_Book book, Bookmark copyWith(_Book book,
{String id, {String id,

View file

@ -49,10 +49,10 @@ class Gamepad extends _Gamepad {
this.dynamicMap = Map.unmodifiable(dynamicMap ?? {}); this.dynamicMap = Map.unmodifiable(dynamicMap ?? {});
@override @override
final List<_GamepadButton> buttons; List<_GamepadButton> buttons;
@override @override
final Map<String, dynamic> dynamicMap; Map<String, dynamic> dynamicMap;
Gamepad copyWith( Gamepad copyWith(
{List<_GamepadButton> buttons, Map<String, dynamic> dynamicMap}) { {List<_GamepadButton> buttons, Map<String, dynamic> dynamicMap}) {