Complete @required support

This commit is contained in:
Tobe O 2018-05-15 15:33:57 -04:00
parent 058d9236b4
commit 76129721f2
10 changed files with 67 additions and 7 deletions

View file

@ -1,5 +1,8 @@
# 2.0.7
* Create unmodifiable Lists and Maps.
* Support `@required` on fields.
* Affix an `@immutable` annotation to classes, if
`package:meta` is imported.
# 2.0.6
* Support for using `abstract` to create immutable model classes.

View file

@ -80,7 +80,7 @@ Future<BuildContext> buildContext(
if (required != null) {
var cr = new ConstantReader(required);
var reason = cr.peek('reason')?.stringValue ??
"Missing field '${ctx.resolveFieldName(field.name)}' on ${ctx
"Missing required field '${ctx.resolveFieldName(field.name)}' on ${ctx
.modelClassName}.";
ctx.requiredFields[field.name] = reason;
}

View file

@ -59,6 +59,10 @@ class BuildContext {
FieldElement get primaryKeyField =>
fields.firstWhere((f) => f.name == primaryKeyName);
bool get importsPackageMeta {
return clazz.library.imports.any((i) => i.uri == 'package:meta/meta.dart');
}
/// Get the aliased name (if one is defined) for a field.
String resolveFieldName(String name) =>
aliases.containsKey(name) ? aliases[name] : name;

View file

@ -30,6 +30,9 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
..name = ctx.modelClassNameRecase.pascalCase
..extend = new Reference(ctx.originalClassName);
//if (ctx.importsPackageMeta)
// clazz.annotations.add(new CodeExpression(new Code('immutable')));
for (var field in ctx.fields) {
clazz.fields.add(new Field((b) {
b

View file

@ -64,7 +64,18 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
..type = ctx.modelClassType;
}));
var buf = new StringBuffer('return {');
var buf = new StringBuffer();
ctx.requiredFields.forEach((key, msg) {
if (ctx.excluded[key]?.canSerialize == false) return;
buf.writeln('''
if (model.$key == null) {
throw new FormatException("$msg");
}
''');
});
buf.writeln('return {');
int i = 0;
// Add named parameters
@ -134,7 +145,19 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
}
}
var buf = new StringBuffer('return new ${ctx.modelClassName}(');
var buf = new StringBuffer();
ctx.requiredFields.forEach((key, msg) {
if (ctx.excluded[key]?.canDeserialize == false) return;
var name = ctx.resolveFieldName(key);
buf.writeln('''
if (map['$name'] == null) {
throw new FormatException("$msg");
}
''');
});
buf.writeln('return new ${ctx.modelClassName}(');
int i = 0;
for (var param in ctx.constructorParameters) {

View file

@ -151,7 +151,9 @@ class TypeScriptDefinitionBuilder implements Builder {
ctx, field.name, field.type, ext, buildStep);
// foo: string;
buf.writeln('$alias?: $typeScriptType;');
if (!ctx.requiredFields.containsKey(field.name))
alias += '?';
buf.writeln('$alias: $typeScriptType;');
}
buf

View file

@ -11,7 +11,7 @@ interface LibraryCollection {
interface Bookmark {
id?: string;
history?: number[];
page?: number;
page: number;
comment?: string;
created_at?: any;
updated_at?: any;

View file

@ -15,7 +15,7 @@ abstract class _Author extends Model {
@required
String get name;
@required
@Required('Custom message for missing `age`')
int get age;
List<Book> get books;
@ -41,6 +41,7 @@ abstract class _Bookmark extends Model {
List<int> get history;
@required
int get page;
String get comment;

View file

@ -136,7 +136,7 @@ class Bookmark extends _Bookmark {
Bookmark(Book book,
{this.id,
List<int> history,
this.page,
@required this.page,
this.comment,
this.createdAt,
this.updatedAt})

View file

@ -8,6 +8,14 @@ part of angel_serialize.test.models.author;
abstract class AuthorSerializer {
static Author fromMap(Map map) {
if (map['name'] == null) {
throw new FormatException("Missing required field 'name' on Author.");
}
if (map['age'] == null) {
throw new FormatException("Custom message for missing `age`");
}
return new Author(
id: map['id'],
name: map['name'],
@ -32,6 +40,14 @@ abstract class AuthorSerializer {
}
static Map<String, dynamic> toMap(Author model) {
if (model.name == null) {
throw new FormatException("Missing required field 'name' on Author.");
}
if (model.age == null) {
throw new FormatException("Custom message for missing `age`");
}
return {
'id': model.id,
'name': model.name,
@ -110,6 +126,10 @@ abstract class LibraryFields {
abstract class BookmarkSerializer {
static Bookmark fromMap(Map map, Book book) {
if (map['page'] == null) {
throw new FormatException("Missing required field 'page' on Bookmark.");
}
return new Bookmark(book,
id: map['id'],
history: map['history'],
@ -128,6 +148,10 @@ abstract class BookmarkSerializer {
}
static Map<String, dynamic> toMap(Bookmark model) {
if (model.page == null) {
throw new FormatException("Missing required field 'page' on Bookmark.");
}
return {
'id': model.id,
'history': model.history,