Complete @required support
This commit is contained in:
parent
058d9236b4
commit
76129721f2
10 changed files with 67 additions and 7 deletions
|
@ -1,5 +1,8 @@
|
||||||
# 2.0.7
|
# 2.0.7
|
||||||
* Create unmodifiable Lists and Maps.
|
* Create unmodifiable Lists and Maps.
|
||||||
|
* Support `@required` on fields.
|
||||||
|
* Affix an `@immutable` annotation to classes, if
|
||||||
|
`package:meta` is imported.
|
||||||
|
|
||||||
# 2.0.6
|
# 2.0.6
|
||||||
* Support for using `abstract` to create immutable model classes.
|
* Support for using `abstract` to create immutable model classes.
|
||||||
|
|
|
@ -80,7 +80,7 @@ Future<BuildContext> buildContext(
|
||||||
if (required != null) {
|
if (required != null) {
|
||||||
var cr = new ConstantReader(required);
|
var cr = new ConstantReader(required);
|
||||||
var reason = cr.peek('reason')?.stringValue ??
|
var reason = cr.peek('reason')?.stringValue ??
|
||||||
"Missing field '${ctx.resolveFieldName(field.name)}' on ${ctx
|
"Missing required field '${ctx.resolveFieldName(field.name)}' on ${ctx
|
||||||
.modelClassName}.";
|
.modelClassName}.";
|
||||||
ctx.requiredFields[field.name] = reason;
|
ctx.requiredFields[field.name] = reason;
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,6 +59,10 @@ class BuildContext {
|
||||||
FieldElement get primaryKeyField =>
|
FieldElement get primaryKeyField =>
|
||||||
fields.firstWhere((f) => f.name == primaryKeyName);
|
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.
|
/// Get the aliased name (if one is defined) for a field.
|
||||||
String resolveFieldName(String name) =>
|
String resolveFieldName(String name) =>
|
||||||
aliases.containsKey(name) ? aliases[name] : name;
|
aliases.containsKey(name) ? aliases[name] : name;
|
||||||
|
|
|
@ -30,6 +30,9 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
|
||||||
..name = ctx.modelClassNameRecase.pascalCase
|
..name = ctx.modelClassNameRecase.pascalCase
|
||||||
..extend = new Reference(ctx.originalClassName);
|
..extend = new Reference(ctx.originalClassName);
|
||||||
|
|
||||||
|
//if (ctx.importsPackageMeta)
|
||||||
|
// clazz.annotations.add(new CodeExpression(new Code('immutable')));
|
||||||
|
|
||||||
for (var field in ctx.fields) {
|
for (var field in ctx.fields) {
|
||||||
clazz.fields.add(new Field((b) {
|
clazz.fields.add(new Field((b) {
|
||||||
b
|
b
|
||||||
|
|
|
@ -64,7 +64,18 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
|
||||||
..type = ctx.modelClassType;
|
..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;
|
int i = 0;
|
||||||
|
|
||||||
// Add named parameters
|
// 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;
|
int i = 0;
|
||||||
|
|
||||||
for (var param in ctx.constructorParameters) {
|
for (var param in ctx.constructorParameters) {
|
||||||
|
|
|
@ -151,7 +151,9 @@ class TypeScriptDefinitionBuilder implements Builder {
|
||||||
ctx, field.name, field.type, ext, buildStep);
|
ctx, field.name, field.type, ext, buildStep);
|
||||||
|
|
||||||
// foo: string;
|
// foo: string;
|
||||||
buf.writeln('$alias?: $typeScriptType;');
|
if (!ctx.requiredFields.containsKey(field.name))
|
||||||
|
alias += '?';
|
||||||
|
buf.writeln('$alias: $typeScriptType;');
|
||||||
}
|
}
|
||||||
|
|
||||||
buf
|
buf
|
||||||
|
|
|
@ -11,7 +11,7 @@ interface LibraryCollection {
|
||||||
interface Bookmark {
|
interface Bookmark {
|
||||||
id?: string;
|
id?: string;
|
||||||
history?: number[];
|
history?: number[];
|
||||||
page?: number;
|
page: number;
|
||||||
comment?: string;
|
comment?: string;
|
||||||
created_at?: any;
|
created_at?: any;
|
||||||
updated_at?: any;
|
updated_at?: any;
|
||||||
|
|
|
@ -15,7 +15,7 @@ abstract class _Author extends Model {
|
||||||
@required
|
@required
|
||||||
String get name;
|
String get name;
|
||||||
|
|
||||||
@required
|
@Required('Custom message for missing `age`')
|
||||||
int get age;
|
int get age;
|
||||||
|
|
||||||
List<Book> get books;
|
List<Book> get books;
|
||||||
|
@ -41,6 +41,7 @@ abstract class _Bookmark extends Model {
|
||||||
|
|
||||||
List<int> get history;
|
List<int> get history;
|
||||||
|
|
||||||
|
@required
|
||||||
int get page;
|
int get page;
|
||||||
|
|
||||||
String get comment;
|
String get comment;
|
||||||
|
|
|
@ -136,7 +136,7 @@ class Bookmark extends _Bookmark {
|
||||||
Bookmark(Book book,
|
Bookmark(Book book,
|
||||||
{this.id,
|
{this.id,
|
||||||
List<int> history,
|
List<int> history,
|
||||||
this.page,
|
@required this.page,
|
||||||
this.comment,
|
this.comment,
|
||||||
this.createdAt,
|
this.createdAt,
|
||||||
this.updatedAt})
|
this.updatedAt})
|
||||||
|
|
|
@ -8,6 +8,14 @@ part of angel_serialize.test.models.author;
|
||||||
|
|
||||||
abstract class AuthorSerializer {
|
abstract class AuthorSerializer {
|
||||||
static Author fromMap(Map map) {
|
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(
|
return new Author(
|
||||||
id: map['id'],
|
id: map['id'],
|
||||||
name: map['name'],
|
name: map['name'],
|
||||||
|
@ -32,6 +40,14 @@ abstract class AuthorSerializer {
|
||||||
}
|
}
|
||||||
|
|
||||||
static Map<String, dynamic> toMap(Author model) {
|
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 {
|
return {
|
||||||
'id': model.id,
|
'id': model.id,
|
||||||
'name': model.name,
|
'name': model.name,
|
||||||
|
@ -110,6 +126,10 @@ abstract class LibraryFields {
|
||||||
|
|
||||||
abstract class BookmarkSerializer {
|
abstract class BookmarkSerializer {
|
||||||
static Bookmark fromMap(Map map, Book book) {
|
static Bookmark fromMap(Map map, Book book) {
|
||||||
|
if (map['page'] == null) {
|
||||||
|
throw new FormatException("Missing required field 'page' on Bookmark.");
|
||||||
|
}
|
||||||
|
|
||||||
return new Bookmark(book,
|
return new Bookmark(book,
|
||||||
id: map['id'],
|
id: map['id'],
|
||||||
history: map['history'],
|
history: map['history'],
|
||||||
|
@ -128,6 +148,10 @@ abstract class BookmarkSerializer {
|
||||||
}
|
}
|
||||||
|
|
||||||
static Map<String, dynamic> toMap(Bookmark model) {
|
static Map<String, dynamic> toMap(Bookmark model) {
|
||||||
|
if (model.page == null) {
|
||||||
|
throw new FormatException("Missing required field 'page' on Bookmark.");
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'id': model.id,
|
'id': model.id,
|
||||||
'history': model.history,
|
'history': model.history,
|
||||||
|
|
Loading…
Reference in a new issue