From 165dd828139f02c4e72baee756d49f7d563ec78c Mon Sep 17 00:00:00 2001 From: Tobe O Date: Thu, 4 Jul 2019 14:03:20 -0400 Subject: [PATCH] Support mutation --- angel_serialize_generator/example/main.g.dart | 4 +-- angel_serialize_generator/lib/model.dart | 7 +++- angel_serialize_generator/test/book_test.dart | 7 ++++ .../test/models/book.g.dart | 36 +++++++++---------- .../test/models/game_pad_button.g.dart | 4 +-- 5 files changed, 35 insertions(+), 23 deletions(-) diff --git a/angel_serialize_generator/example/main.g.dart b/angel_serialize_generator/example/main.g.dart index 376c807c..03560cf6 100644 --- a/angel_serialize_generator/example/main.g.dart +++ b/angel_serialize_generator/example/main.g.dart @@ -11,10 +11,10 @@ class Todo extends _Todo { Todo({this.text, this.completed}); @override - final String text; + String text; @override - final bool completed; + bool completed; Todo copyWith({String text, bool completed}) { return Todo( diff --git a/angel_serialize_generator/lib/model.dart b/angel_serialize_generator/lib/model.dart index 6c08f2e0..07e3a093 100644 --- a/angel_serialize_generator/lib/model.dart +++ b/angel_serialize_generator/lib/model.dart @@ -45,10 +45,15 @@ class JsonModelGenerator extends GeneratorForAnnotation { clazz.fields.add(Field((b) { b ..name = field.name - ..modifier = FieldModifier.final$ + // ..modifier = FieldModifier.final$ ..annotations.add(CodeExpression(Code('override'))) ..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]) { if (el?.documentationComment != null) { b.docs.addAll(el.documentationComment.split('\n')); diff --git a/angel_serialize_generator/test/book_test.dart b/angel_serialize_generator/test/book_test.dart index bb23e659..7853bb05 100644 --- a/angel_serialize_generator/test/book_test.dart +++ b/angel_serialize_generator/test/book_test.dart @@ -41,6 +41,13 @@ main() { 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', () { expect(serializedDeathlyHallows['page_count'], deathlyHallows.pageCount); expect(serializedDeathlyHallows.keys, isNot(contains('pageCount'))); diff --git a/angel_serialize_generator/test/models/book.g.dart b/angel_serialize_generator/test/models/book.g.dart index eeb587a8..588658b3 100644 --- a/angel_serialize_generator/test/models/book.g.dart +++ b/angel_serialize_generator/test/models/book.g.dart @@ -23,32 +23,32 @@ class Book extends _Book { : this.notModels = List.unmodifiable(notModels ?? []); @override - final String id; + String id; @override - final String author; + String author; @override - final String title; + String title; @override - final String description; + String description; /// The number of pages the book has. @override - final int pageCount; + int pageCount; @override - final List notModels; + List notModels; @override - final String camelCaseString; + String camelCaseString; @override - final DateTime createdAt; + DateTime createdAt; @override - final DateTime updatedAt; + DateTime updatedAt; Book copyWith( {String id, @@ -126,7 +126,7 @@ class Author extends _Author { : this.books = List.unmodifiable(books ?? []); @override - final String id; + String id; @override final String name; @@ -148,10 +148,10 @@ class Author extends _Author { final String obscured; @override - final DateTime createdAt; + DateTime createdAt; @override - final DateTime updatedAt; + DateTime updatedAt; Author copyWith( {String id, @@ -221,16 +221,16 @@ class Library extends _Library { : this.collection = Map.unmodifiable(collection ?? {}); @override - final String id; + String id; @override final Map collection; @override - final DateTime createdAt; + DateTime createdAt; @override - final DateTime updatedAt; + DateTime updatedAt; Library copyWith( {String id, @@ -283,7 +283,7 @@ class Bookmark extends _Bookmark { super(book); @override - final String id; + String id; @override final List history; @@ -295,10 +295,10 @@ class Bookmark extends _Bookmark { final String comment; @override - final DateTime createdAt; + DateTime createdAt; @override - final DateTime updatedAt; + DateTime updatedAt; Bookmark copyWith(_Book book, {String id, diff --git a/angel_serialize_generator/test/models/game_pad_button.g.dart b/angel_serialize_generator/test/models/game_pad_button.g.dart index 55de9d22..c3af517a 100644 --- a/angel_serialize_generator/test/models/game_pad_button.g.dart +++ b/angel_serialize_generator/test/models/game_pad_button.g.dart @@ -49,10 +49,10 @@ class Gamepad extends _Gamepad { this.dynamicMap = Map.unmodifiable(dynamicMap ?? {}); @override - final List<_GamepadButton> buttons; + List<_GamepadButton> buttons; @override - final Map dynamicMap; + Map dynamicMap; Gamepad copyWith( {List<_GamepadButton> buttons, Map dynamicMap}) {