Support mutation
This commit is contained in:
parent
8cdf71bea5
commit
165dd82813
5 changed files with 35 additions and 23 deletions
|
@ -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(
|
||||
|
|
|
@ -45,10 +45,15 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
|
|||
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'));
|
||||
|
|
|
@ -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')));
|
||||
|
|
|
@ -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<double> notModels;
|
||||
List<double> 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<String, _Book> 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<int> 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,
|
||||
|
|
|
@ -49,10 +49,10 @@ class Gamepad extends _Gamepad {
|
|||
this.dynamicMap = Map.unmodifiable(dynamicMap ?? {});
|
||||
|
||||
@override
|
||||
final List<_GamepadButton> buttons;
|
||||
List<_GamepadButton> buttons;
|
||||
|
||||
@override
|
||||
final Map<String, dynamic> dynamicMap;
|
||||
Map<String, dynamic> dynamicMap;
|
||||
|
||||
Gamepad copyWith(
|
||||
{List<_GamepadButton> buttons, Map<String, dynamic> dynamicMap}) {
|
||||
|
|
Loading…
Reference in a new issue