From f10b8ab21b09a37e1af821e1433b09e8a02987b2 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Tue, 27 Feb 2018 14:57:05 -0500 Subject: [PATCH] Named parameters in constructor --- .../lib/angel_serialize_generator.dart | 22 ++++++++++++++++++- .../test/models/author.g.dart | 15 +++++++++++-- .../test/models/book.g.dart | 11 +++++++++- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/angel_serialize_generator/lib/angel_serialize_generator.dart b/angel_serialize_generator/lib/angel_serialize_generator.dart index e00a0fe2..be7a2e39 100644 --- a/angel_serialize_generator/lib/angel_serialize_generator.dart +++ b/angel_serialize_generator/lib/angel_serialize_generator.dart @@ -52,9 +52,12 @@ class JsonModelGenerator extends GeneratorForAnnotation { return buf.toString(); } + /// Generate an extended model class. void generateClass(BuildContext ctx, FileBuilder file) { file.body.add(new Class((clazz) { - clazz.name = ctx.modelClassNameRecase.pascalCase; + clazz + ..name = ctx.modelClassNameRecase.pascalCase + ..extend = new Reference(ctx.originalClassName); for (var field in ctx.fields) { clazz.fields.add(new Field((b) { @@ -64,6 +67,23 @@ class JsonModelGenerator extends GeneratorForAnnotation { ..type = convertTypeReference(field.type); })); } + + generateConstructor(ctx, clazz, file); + })); + } + + /// Generate a constructor with named parameters. + void generateConstructor( + BuildContext ctx, ClassBuilder clazz, FileBuilder file) { + clazz.constructors.add(new Constructor((constructor) { + for (var field in ctx.fields) { + constructor.optionalParameters.add(new Parameter((b) { + b + ..name = field.name + ..named = true + ..toThis = true; + })); + } })); } } diff --git a/angel_serialize_generator/test/models/author.g.dart b/angel_serialize_generator/test/models/author.g.dart index c13ec86f..ed7c2792 100644 --- a/angel_serialize_generator/test/models/author.g.dart +++ b/angel_serialize_generator/test/models/author.g.dart @@ -6,7 +6,16 @@ part of angel_serialize.test.models.author; // Generator: JsonModelGenerator // ************************************************************************** -class Author { +class Author extends _Author { + Author( + {this.id, + this.name, + this.age, + this.books, + this.newestBook, + this.createdAt, + this.updatedAt}); + final String id; final String name; @@ -22,7 +31,9 @@ class Author { final DateTime updatedAt; } -class Library { +class Library extends _Library { + Library({this.id, this.collection, this.createdAt, this.updatedAt}); + final String id; final Map collection; diff --git a/angel_serialize_generator/test/models/book.g.dart b/angel_serialize_generator/test/models/book.g.dart index eaafb316..100d634d 100644 --- a/angel_serialize_generator/test/models/book.g.dart +++ b/angel_serialize_generator/test/models/book.g.dart @@ -6,7 +6,16 @@ part of angel_serialize.test.models.book; // Generator: JsonModelGenerator // ************************************************************************** -class Book { +class Book extends _Book { + Book( + {this.id, + this.author, + this.title, + this.description, + this.pageCount, + this.createdAt, + this.updatedAt}); + final String id; final String author;