Only gen serializer if serializers are specifid

This commit is contained in:
Tobe O 2018-02-27 19:59:43 -05:00
parent 4319721ce3
commit b5ca9c6d7b
6 changed files with 89 additions and 14 deletions

View file

@ -28,6 +28,9 @@ const Serializable serializable = const Serializable();
/// The supported serialization types. /// The supported serialization types.
abstract class Serializers { abstract class Serializers {
/// All supported serialization types.
static const List<int> all = const [map, json];
/// Enable `fromMap` and `toMap` methods on the model class. /// Enable `fromMap` and `toMap` methods on the model class.
static const int map = 0; static const int map = 0;

View file

@ -10,6 +10,7 @@ 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';
part 'serialize.dart';
/// Converts a [DartType] to a [TypeReference]. /// Converts a [DartType] to a [TypeReference].
TypeReference convertTypeReference(DartType t) { TypeReference convertTypeReference(DartType t) {

View file

@ -0,0 +1,42 @@
part of angel_serialize_generator;
class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
final bool autoSnakeCaseNames;
const SerializerGenerator({this.autoSnakeCaseNames: true});
@override
Future<String> generateForAnnotatedElement(
Element element, ConstantReader annotation, BuildStep buildStep) async {
if (element.kind != ElementKind.CLASS)
throw 'Only classes can be annotated with a @Serializable() annotation.';
var ctx = await buildContext(element, annotation, buildStep,
await buildStep.resolver, true, autoSnakeCaseNames != false);
var serializers = annotation.peek('serializers')?.listValue ?? [];
if (serializers.isEmpty) return null;
// Check if any serializer is recognized
if (!serializers.any((s) => Serializers.all.contains(s.toIntValue()))) {
return null;
}
var lib = new File((b) {
generateClass(ctx, b);
});
var buf = lib.accept(new DartEmitter());
return buf.toString();
}
/// Generate a serializer class.
void generateClass(BuildContext ctx, FileBuilder file) {
file.body.add(new Class((clazz) {
clazz
..name = '${ctx.modelClassNameRecase.pascalCase}Serializer'
..abstract = true;
}));
}
}

View file

@ -0,0 +1,11 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of angel_serialize.test.models.author;
// **************************************************************************
// Generator: SerializerGenerator
// **************************************************************************
abstract class AuthorSerializer {}
abstract class LibrarySerializer {}

View file

@ -0,0 +1,9 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of angel_serialize.test.models.book;
// **************************************************************************
// Generator: SerializerGenerator
// **************************************************************************
class BookSerializer extends _Book {}

View file

@ -3,18 +3,27 @@ import 'package:source_gen/source_gen.dart';
import 'package:angel_serialize_generator/angel_serialize_generator.dart'; import 'package:angel_serialize_generator/angel_serialize_generator.dart';
final List<BuildAction> actions = [ final List<BuildAction> actions = [
new BuildAction( jsonModel(const ['test/models/book.dart']),
new PartBuilder([const JsonModelGenerator()]), jsonModel(const ['test/models/author.dart']),
'angel_serialize_generator', angelSerialize(const ['test/models/book.dart']),
inputs: const [ angelSerialize(const ['test/models/author.dart']),
'test/models/book.dart',
],
),
new BuildAction(
new PartBuilder([const JsonModelGenerator()]),
'angel_serialize_generator',
inputs: const [
'test/models/author.dart',
],
),
]; ];
BuildAction jsonModel(List<String> inputs) {
return new BuildAction(
new PartBuilder([const JsonModelGenerator()]),
'angel_serialize_generator',
inputs: inputs,
);
}
BuildAction angelSerialize(List<String> inputs) {
return new BuildAction(
new PartBuilder(
[const SerializerGenerator()],
generatedExtension: '.serializer.g.dart',
),
'angel_serialize_generator',
inputs: inputs,
);
}