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 =
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,

View file

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

View file

@ -100,6 +100,10 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
else {
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_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<Book> get books;
Book get newestBook;
@exclude
@ -34,7 +40,9 @@ abstract class _Bookmark extends Model {
final Book book;
List<int> get history;
int get page;
String get comment;
_Bookmark(this.book);

View file

@ -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<Book> books,
this.newestBook,
this.secret,