From 058d9236b4f7adfe2fa23eccfb640d4d0a310834 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Tue, 15 May 2018 15:16:46 -0400 Subject: [PATCH] Add @required annotations in constructor --- angel_serialize_generator/lib/build_context.dart | 2 +- angel_serialize_generator/lib/context.dart | 4 +++- angel_serialize_generator/lib/model.dart | 4 ++++ angel_serialize_generator/test/models/author.dart | 8 ++++++++ angel_serialize_generator/test/models/author.g.dart | 4 ++-- 5 files changed, 18 insertions(+), 4 deletions(-) diff --git a/angel_serialize_generator/lib/build_context.dart b/angel_serialize_generator/lib/build_context.dart index 03b24132..01d25b25 100644 --- a/angel_serialize_generator/lib/build_context.dart +++ b/angel_serialize_generator/lib/build_context.dart @@ -34,7 +34,7 @@ Future buildContext( autoSnakeCaseNames = annotation.peek('autoSnakeCaseNames')?.boolValue ?? autoSnakeCaseNames; - var ctx = new BuildContext(annotation, + var ctx = new BuildContext(annotation, clazz, originalClassName: clazz.name, sourceFilename: p.basename(buildStep.inputId.path), autoIdAndDateFields: autoIdAndDateFields, diff --git a/angel_serialize_generator/lib/context.dart b/angel_serialize_generator/lib/context.dart index 7fd245a6..0debfd3c 100644 --- a/angel_serialize_generator/lib/context.dart +++ b/angel_serialize_generator/lib/context.dart @@ -32,10 +32,12 @@ class BuildContext { final ConstantReader annotation; + final ClassElement clazz; + /// The name of the field that identifies data of this model type. String primaryKeyName = 'id'; - BuildContext(this.annotation, + BuildContext(this.annotation, this.clazz, {this.originalClassName, this.sourceFilename, this.autoSnakeCaseNames, diff --git a/angel_serialize_generator/lib/model.dart b/angel_serialize_generator/lib/model.dart index 6bd676f9..d7446ab2 100644 --- a/angel_serialize_generator/lib/model.dart +++ b/angel_serialize_generator/lib/model.dart @@ -100,6 +100,10 @@ class JsonModelGenerator extends GeneratorForAnnotation { else { b.type = convertTypeReference(field.type); } + + if (ctx.requiredFields.containsKey(field.name)) { + b.annotations.add(new CodeExpression(new Code('required'))); + } })); } })); diff --git a/angel_serialize_generator/test/models/author.dart b/angel_serialize_generator/test/models/author.dart index 3e0cc868..ce4695a2 100644 --- a/angel_serialize_generator/test/models/author.dart +++ b/angel_serialize_generator/test/models/author.dart @@ -3,6 +3,7 @@ library angel_serialize.test.models.author; import 'package:angel_framework/common.dart'; import 'package:angel_serialize/angel_serialize.dart'; import 'package:collection/collection.dart'; +import 'package:meta/meta.dart'; import 'book.dart'; part 'author.g.dart'; @@ -11,9 +12,14 @@ part 'author.serializer.g.dart'; @serializable abstract class _Author extends Model { + @required String get name; + + @required int get age; + List get books; + Book get newestBook; @exclude @@ -34,7 +40,9 @@ abstract class _Bookmark extends Model { final Book book; List get history; + int get page; + String get comment; _Bookmark(this.book); diff --git a/angel_serialize_generator/test/models/author.g.dart b/angel_serialize_generator/test/models/author.g.dart index a14db13e..a40b4452 100644 --- a/angel_serialize_generator/test/models/author.g.dart +++ b/angel_serialize_generator/test/models/author.g.dart @@ -9,8 +9,8 @@ part of angel_serialize.test.models.author; class Author extends _Author { Author( {this.id, - this.name, - this.age, + @required this.name, + @required this.age, List books, this.newestBook, this.secret,