Named parameters in constructor

This commit is contained in:
Tobe O 2018-02-27 14:57:05 -05:00
parent 1fac0bfee0
commit f10b8ab21b
3 changed files with 44 additions and 4 deletions

View file

@ -52,9 +52,12 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
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<Serializable> {
..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;
}));
}
}));
}
}

View file

@ -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<String, Book> collection;

View file

@ -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;