part of angel_serialize_generator; class SerializerGenerator extends GeneratorForAnnotation { final bool autoSnakeCaseNames; const SerializerGenerator({this.autoSnakeCaseNames: true}); @override Future 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; })); } }