Basic toMap()

This commit is contained in:
Tobe O 2018-02-27 20:10:57 -05:00
parent b5ca9c6d7b
commit 81e6263191
6 changed files with 76 additions and 6 deletions

View file

@ -11,6 +11,8 @@ import 'context.dart';
const TypeChecker aliasTypeChecker = const TypeChecker.fromRuntime(Alias); const TypeChecker aliasTypeChecker = const TypeChecker.fromRuntime(Alias);
const TypeChecker dateTimeTypeChecker = const TypeChecker.fromRuntime(DateTime);
const TypeChecker excludeTypeChecker = const TypeChecker.fromRuntime(Exclude); const TypeChecker excludeTypeChecker = const TypeChecker.fromRuntime(Exclude);
const TypeChecker serializableTypeChecker = const TypeChecker serializableTypeChecker =

View file

@ -24,7 +24,7 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
} }
var lib = new File((b) { var lib = new File((b) {
generateClass(ctx, b); generateClass(serializers.map((s) => s.toIntValue()).toList(), ctx, b);
}); });
var buf = lib.accept(new DartEmitter()); var buf = lib.accept(new DartEmitter());
@ -32,11 +32,54 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
} }
/// Generate a serializer class. /// Generate a serializer class.
void generateClass(BuildContext ctx, FileBuilder file) { void generateClass(
List<int> serializers, BuildContext ctx, FileBuilder file) {
file.body.add(new Class((clazz) { file.body.add(new Class((clazz) {
clazz clazz
..name = '${ctx.modelClassNameRecase.pascalCase}Serializer' ..name = '${ctx.modelClassNameRecase.pascalCase}Serializer'
..abstract = true; ..abstract = true;
if (serializers.contains(Serializers.map)) {
// TODO: Generate fromMap
}
if (serializers.contains(Serializers.map) ||
serializers.contains(Serializers.json)) {
generateToMapMethod(clazz, ctx, file);
// TODO: Generate toJson
}
}));
}
void generateToMapMethod(
ClassBuilder clazz, BuildContext ctx, FileBuilder file) {
clazz.methods.add(new Method((method) {
method
..name = 'toMap'
..returns = new Reference('Map<String, dynamic>');
var buf = new StringBuffer('return {');
int i = 0;
// Add named parameters
for (var field in ctx.fields) {
// Skip excluded fields
if (ctx.excluded.containsKey(field.name)) continue;
var alias = ctx.resolveFieldName(field.name);
if (i++ > 0) buf.write(', ');
String serializedRepresentation = field.name;
if (dateTimeTypeChecker.isAssignableFromType(field.type))
serializedRepresentation = '${field.name}.toIso8601String()';
buf.write("'$alias': $serializedRepresentation");
}
buf.write('};');
method.body = new Code(buf.toString());
})); }));
} }
} }

View file

@ -3,8 +3,8 @@ 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 'book.dart'; import 'book.dart';
part 'author.g.dart'; part 'author.g.dart';
part 'author.serializer.g.dart';
@serializable @serializable
abstract class _Author extends Model { abstract class _Author extends Model {

View file

@ -6,6 +6,22 @@ part of angel_serialize.test.models.author;
// Generator: SerializerGenerator // Generator: SerializerGenerator
// ************************************************************************** // **************************************************************************
abstract class AuthorSerializer {} abstract class AuthorSerializer {
Map<String, dynamic> toMap() {
return {
'id': id,
'created_at': createdAt.toIso8601String(),
'updated_at': updatedAt.toIso8601String()
};
}
}
abstract class LibrarySerializer {} abstract class LibrarySerializer {
Map<String, dynamic> toMap() {
return {
'id': id,
'created_at': createdAt.toIso8601String(),
'updated_at': updatedAt.toIso8601String()
};
}
}

View file

@ -3,6 +3,7 @@ library angel_serialize.test.models.book;
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';
part 'book.g.dart'; part 'book.g.dart';
part 'book.serializer.g.dart';
@serializable @serializable
abstract class _Book extends Model { abstract class _Book extends Model {

View file

@ -6,4 +6,12 @@ part of angel_serialize.test.models.book;
// Generator: SerializerGenerator // Generator: SerializerGenerator
// ************************************************************************** // **************************************************************************
class BookSerializer extends _Book {} abstract class BookSerializer {
Map<String, dynamic> toMap() {
return {
'id': id,
'created_at': createdAt.toIso8601String(),
'updated_at': updatedAt.toIso8601String()
};
}
}