43 lines
1.2 KiB
Dart
43 lines
1.2 KiB
Dart
|
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;
|
||
|
}));
|
||
|
}
|
||
|
}
|