Add final fields
This commit is contained in:
parent
50153dedf3
commit
c0d57bf8b1
4 changed files with 34 additions and 179 deletions
|
@ -9,6 +9,7 @@ import 'package:code_builder/code_builder.dart';
|
||||||
import 'package:source_gen/source_gen.dart' hide LibraryBuilder;
|
import 'package:source_gen/source_gen.dart' hide LibraryBuilder;
|
||||||
import 'build_context.dart';
|
import 'build_context.dart';
|
||||||
import 'context.dart';
|
import 'context.dart';
|
||||||
|
|
||||||
part 'model.dart';
|
part 'model.dart';
|
||||||
|
|
||||||
class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
|
class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
|
||||||
|
@ -19,8 +20,8 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
|
||||||
{this.autoSnakeCaseNames: true, this.autoIdAndDateFields: true});
|
{this.autoSnakeCaseNames: true, this.autoIdAndDateFields: true});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<String> generateForAnnotatedElement(Element element,
|
Future<String> generateForAnnotatedElement(
|
||||||
ConstantReader reader, BuildStep buildStep) async {
|
Element element, ConstantReader reader, BuildStep buildStep) async {
|
||||||
if (element.kind != ElementKind.CLASS)
|
if (element.kind != ElementKind.CLASS)
|
||||||
throw 'Only classes can be annotated with a @Serializable() annotation.';
|
throw 'Only classes can be annotated with a @Serializable() annotation.';
|
||||||
|
|
||||||
|
@ -42,7 +43,16 @@ class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
|
||||||
|
|
||||||
void generateClass(BuildContext ctx, FileBuilder file) {
|
void generateClass(BuildContext ctx, FileBuilder file) {
|
||||||
file.body.add(new Class((clazz) {
|
file.body.add(new Class((clazz) {
|
||||||
|
clazz.name = ctx.modelClassNameRecase.pascalCase;
|
||||||
|
|
||||||
|
for (var field in ctx.fields) {
|
||||||
|
clazz.fields.add(new Field((b) {
|
||||||
|
b
|
||||||
|
..name = field.name
|
||||||
|
..modifier = FieldModifier.final$
|
||||||
|
..type = new Reference(field.type.name);
|
||||||
|
}));
|
||||||
|
}
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,132 +6,28 @@ part of angel_serialize.test.models.author;
|
||||||
// Generator: JsonModelGenerator
|
// Generator: JsonModelGenerator
|
||||||
// **************************************************************************
|
// **************************************************************************
|
||||||
|
|
||||||
class Author extends _Author {
|
class Author {
|
||||||
@override
|
final String id;
|
||||||
String id;
|
|
||||||
|
|
||||||
@override
|
final String name;
|
||||||
String name;
|
|
||||||
|
|
||||||
@override
|
final int age;
|
||||||
int age;
|
|
||||||
|
|
||||||
@override
|
final List books;
|
||||||
List<Book> books;
|
|
||||||
|
|
||||||
@override
|
final Book newestBook;
|
||||||
Book newestBook;
|
|
||||||
|
|
||||||
@override
|
final DateTime createdAt;
|
||||||
DateTime createdAt;
|
|
||||||
|
|
||||||
@override
|
final DateTime updatedAt;
|
||||||
DateTime updatedAt;
|
|
||||||
|
|
||||||
Author(
|
|
||||||
{this.id,
|
|
||||||
this.name,
|
|
||||||
this.age,
|
|
||||||
this.books,
|
|
||||||
this.newestBook,
|
|
||||||
this.createdAt,
|
|
||||||
this.updatedAt});
|
|
||||||
|
|
||||||
factory Author.fromJson(Map data) {
|
|
||||||
return new Author(
|
|
||||||
id: data['id'],
|
|
||||||
name: data['name'],
|
|
||||||
age: data['age'],
|
|
||||||
books: data['books'] is List
|
|
||||||
? data['books']
|
|
||||||
.map((x) =>
|
|
||||||
x == null ? null : (x is Book ? x : new Book.fromJson(x)))
|
|
||||||
.toList()
|
|
||||||
: null,
|
|
||||||
newestBook: data['newest_book'] == null
|
|
||||||
? null
|
|
||||||
: (data['newest_book'] is Book
|
|
||||||
? data['newest_book']
|
|
||||||
: new Book.fromJson(data['newest_book'])),
|
|
||||||
createdAt: data['created_at'] is DateTime
|
|
||||||
? data['created_at']
|
|
||||||
: (data['created_at'] is String
|
|
||||||
? DateTime.parse(data['created_at'])
|
|
||||||
: null),
|
|
||||||
updatedAt: data['updated_at'] is DateTime
|
|
||||||
? data['updated_at']
|
|
||||||
: (data['updated_at'] is String
|
|
||||||
? DateTime.parse(data['updated_at'])
|
|
||||||
: null));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, dynamic> toJson() => {
|
class Library {
|
||||||
'id': id,
|
final String id;
|
||||||
'name': name,
|
|
||||||
'age': age,
|
|
||||||
'books': books,
|
|
||||||
'newest_book': newestBook,
|
|
||||||
'created_at': createdAt == null ? null : createdAt.toIso8601String(),
|
|
||||||
'updated_at': updatedAt == null ? null : updatedAt.toIso8601String()
|
|
||||||
};
|
|
||||||
|
|
||||||
static Author parse(Map map) => new Author.fromJson(map);
|
final Map collection;
|
||||||
|
|
||||||
Author clone() {
|
final DateTime createdAt;
|
||||||
return new Author.fromJson(toJson());
|
|
||||||
}
|
final DateTime updatedAt;
|
||||||
}
|
|
||||||
|
|
||||||
class Library extends _Library {
|
|
||||||
@override
|
|
||||||
String id;
|
|
||||||
|
|
||||||
@override
|
|
||||||
Map<String, Book> collection;
|
|
||||||
|
|
||||||
@override
|
|
||||||
DateTime createdAt;
|
|
||||||
|
|
||||||
@override
|
|
||||||
DateTime updatedAt;
|
|
||||||
|
|
||||||
Library({this.id, this.collection, this.createdAt, this.updatedAt});
|
|
||||||
|
|
||||||
factory Library.fromJson(Map data) {
|
|
||||||
return new Library(
|
|
||||||
id: data['id'],
|
|
||||||
collection: data['collection'] is Map
|
|
||||||
? data['collection'].keys.fold({}, (out, k) {
|
|
||||||
out[k] = data['collection'][k] == null
|
|
||||||
? null
|
|
||||||
: (data['collection'][k] is Book
|
|
||||||
? data['collection'][k]
|
|
||||||
: new Book.fromJson(data['collection'][k]));
|
|
||||||
return out;
|
|
||||||
})
|
|
||||||
: null,
|
|
||||||
createdAt: data['created_at'] is DateTime
|
|
||||||
? data['created_at']
|
|
||||||
: (data['created_at'] is String
|
|
||||||
? DateTime.parse(data['created_at'])
|
|
||||||
: null),
|
|
||||||
updatedAt: data['updated_at'] is DateTime
|
|
||||||
? data['updated_at']
|
|
||||||
: (data['updated_at'] is String
|
|
||||||
? DateTime.parse(data['updated_at'])
|
|
||||||
: null));
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<String, dynamic> toJson() => {
|
|
||||||
'id': id,
|
|
||||||
'collection': collection,
|
|
||||||
'created_at': createdAt == null ? null : createdAt.toIso8601String(),
|
|
||||||
'updated_at': updatedAt == null ? null : updatedAt.toIso8601String()
|
|
||||||
};
|
|
||||||
|
|
||||||
static Library parse(Map map) => new Library.fromJson(map);
|
|
||||||
|
|
||||||
Library clone() {
|
|
||||||
return new Library.fromJson(toJson());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,69 +6,18 @@ part of angel_serialize.test.models.book;
|
||||||
// Generator: JsonModelGenerator
|
// Generator: JsonModelGenerator
|
||||||
// **************************************************************************
|
// **************************************************************************
|
||||||
|
|
||||||
class Book extends _Book {
|
class Book {
|
||||||
@override
|
final String id;
|
||||||
String id;
|
|
||||||
|
|
||||||
@override
|
final String author;
|
||||||
String author;
|
|
||||||
|
|
||||||
@override
|
final String title;
|
||||||
String title;
|
|
||||||
|
|
||||||
@override
|
final String description;
|
||||||
String description;
|
|
||||||
|
|
||||||
@override
|
final int pageCount;
|
||||||
int pageCount;
|
|
||||||
|
|
||||||
@override
|
final DateTime createdAt;
|
||||||
DateTime createdAt;
|
|
||||||
|
|
||||||
@override
|
final DateTime updatedAt;
|
||||||
DateTime updatedAt;
|
|
||||||
|
|
||||||
Book(
|
|
||||||
{this.id,
|
|
||||||
this.author,
|
|
||||||
this.title,
|
|
||||||
this.description,
|
|
||||||
this.pageCount,
|
|
||||||
this.createdAt,
|
|
||||||
this.updatedAt});
|
|
||||||
|
|
||||||
factory Book.fromJson(Map data) {
|
|
||||||
return new Book(
|
|
||||||
id: data['id'],
|
|
||||||
author: data['author'],
|
|
||||||
title: data['title'],
|
|
||||||
description: data['description'],
|
|
||||||
pageCount: data['page_count'],
|
|
||||||
createdAt: data['created_at'] is DateTime
|
|
||||||
? data['created_at']
|
|
||||||
: (data['created_at'] is String
|
|
||||||
? DateTime.parse(data['created_at'])
|
|
||||||
: null),
|
|
||||||
updatedAt: data['updated_at'] is DateTime
|
|
||||||
? data['updated_at']
|
|
||||||
: (data['updated_at'] is String
|
|
||||||
? DateTime.parse(data['updated_at'])
|
|
||||||
: null));
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<String, dynamic> toJson() => {
|
|
||||||
'id': id,
|
|
||||||
'author': author,
|
|
||||||
'title': title,
|
|
||||||
'description': description,
|
|
||||||
'page_count': pageCount,
|
|
||||||
'created_at': createdAt == null ? null : createdAt.toIso8601String(),
|
|
||||||
'updated_at': updatedAt == null ? null : updatedAt.toIso8601String()
|
|
||||||
};
|
|
||||||
|
|
||||||
static Book parse(Map map) => new Book.fromJson(map);
|
|
||||||
|
|
||||||
Book clone() {
|
|
||||||
return new Book.fromJson(toJson());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue