Add @required annotations in constructor

This commit is contained in:
Tobe O 2018-05-15 15:16:46 -04:00
parent 756a154551
commit 058d9236b4
5 changed files with 18 additions and 4 deletions

View file

@ -34,7 +34,7 @@ Future<BuildContext> buildContext(
autoSnakeCaseNames = autoSnakeCaseNames =
annotation.peek('autoSnakeCaseNames')?.boolValue ?? autoSnakeCaseNames; annotation.peek('autoSnakeCaseNames')?.boolValue ?? autoSnakeCaseNames;
var ctx = new BuildContext(annotation, var ctx = new BuildContext(annotation, clazz,
originalClassName: clazz.name, originalClassName: clazz.name,
sourceFilename: p.basename(buildStep.inputId.path), sourceFilename: p.basename(buildStep.inputId.path),
autoIdAndDateFields: autoIdAndDateFields, autoIdAndDateFields: autoIdAndDateFields,

View file

@ -32,10 +32,12 @@ class BuildContext {
final ConstantReader annotation; final ConstantReader annotation;
final ClassElement clazz;
/// The name of the field that identifies data of this model type. /// The name of the field that identifies data of this model type.
String primaryKeyName = 'id'; String primaryKeyName = 'id';
BuildContext(this.annotation, BuildContext(this.annotation, this.clazz,
{this.originalClassName, {this.originalClassName,
this.sourceFilename, this.sourceFilename,
this.autoSnakeCaseNames, this.autoSnakeCaseNames,

View file

@ -100,6 +100,10 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
else { else {
b.type = convertTypeReference(field.type); b.type = convertTypeReference(field.type);
} }
if (ctx.requiredFields.containsKey(field.name)) {
b.annotations.add(new CodeExpression(new Code('required')));
}
})); }));
} }
})); }));

View file

@ -3,6 +3,7 @@ library angel_serialize.test.models.author;
import 'package:angel_framework/common.dart'; import 'package:angel_framework/common.dart';
import 'package:angel_serialize/angel_serialize.dart'; import 'package:angel_serialize/angel_serialize.dart';
import 'package:collection/collection.dart'; import 'package:collection/collection.dart';
import 'package:meta/meta.dart';
import 'book.dart'; import 'book.dart';
part 'author.g.dart'; part 'author.g.dart';
@ -11,9 +12,14 @@ part 'author.serializer.g.dart';
@serializable @serializable
abstract class _Author extends Model { abstract class _Author extends Model {
@required
String get name; String get name;
@required
int get age; int get age;
List<Book> get books; List<Book> get books;
Book get newestBook; Book get newestBook;
@exclude @exclude
@ -34,7 +40,9 @@ abstract class _Bookmark extends Model {
final Book book; final Book book;
List<int> get history; List<int> get history;
int get page; int get page;
String get comment; String get comment;
_Bookmark(this.book); _Bookmark(this.book);

View file

@ -9,8 +9,8 @@ part of angel_serialize.test.models.author;
class Author extends _Author { class Author extends _Author {
Author( Author(
{this.id, {this.id,
this.name, @required this.name,
this.age, @required this.age,
List<Book> books, List<Book> books,
this.newestBook, this.newestBook,
this.secret, this.secret,