diff --git a/angel_serialize_generator/lib/angel_serialize_generator.dart b/angel_serialize_generator/lib/angel_serialize_generator.dart new file mode 100644 index 00000000..2ce77263 --- /dev/null +++ b/angel_serialize_generator/lib/angel_serialize_generator.dart @@ -0,0 +1,48 @@ +library angel_serialize_generator; + +import 'dart:async'; +import 'package:analyzer/dart/element/element.dart'; +import 'package:analyzer/dart/element/type.dart'; +import 'package:angel_serialize/angel_serialize.dart'; +import 'package:build/build.dart'; +import 'package:code_builder/code_builder.dart'; +import 'package:source_gen/source_gen.dart' hide LibraryBuilder; +import 'build_context.dart'; +import 'context.dart'; +part 'model.dart'; + +class JsonModelGenerator extends GeneratorForAnnotation { + final bool autoSnakeCaseNames; + final bool autoIdAndDateFields; + + const JsonModelGenerator( + {this.autoSnakeCaseNames: true, this.autoIdAndDateFields: true}); + + @override + Future generateForAnnotatedElement(Element element, + ConstantReader reader, BuildStep buildStep) async { + if (element.kind != ElementKind.CLASS) + throw 'Only classes can be annotated with a @Serializable() annotation.'; + + var ctx = await buildContext( + element, + serializable, + buildStep, + await buildStep.resolver, + autoSnakeCaseNames != false, + autoIdAndDateFields != false); + + var lib = new File((b) { + generateClass(ctx, b); + }); + + var buf = lib.accept(new DartEmitter()); + return buf.toString(); + } + + void generateClass(BuildContext ctx, FileBuilder file) { + file.body.add(new Class((clazz) { + + })); + } +} \ No newline at end of file diff --git a/angel_serialize_generator/lib/build_context.dart b/angel_serialize_generator/lib/build_context.dart index 26024d1d..cce3cd00 100644 --- a/angel_serialize_generator/lib/build_context.dart +++ b/angel_serialize_generator/lib/build_context.dart @@ -57,7 +57,8 @@ Future buildContext( if (autoIdAndDateFields != false) { if (!fieldNames.contains('id')) { - var idField = new ShimFieldImpl('id', lib.context.typeProvider.stringType); + var idField = + new ShimFieldImpl('id', lib.context.typeProvider.stringType); ctx.fields.insert(0, idField); ctx.shimmed['id'] = true; } @@ -78,7 +79,6 @@ Future buildContext( ctx.shimmed[key] = true; } } - ; } return ctx; @@ -88,5 +88,6 @@ Future buildContext( class ShimFieldImpl extends FieldElementImpl { @override final DartType type; + ShimFieldImpl(String name, this.type) : super(name, -1); } diff --git a/angel_serialize_generator/lib/context.dart b/angel_serialize_generator/lib/context.dart index 6600c918..457d7fc6 100644 --- a/angel_serialize_generator/lib/context.dart +++ b/angel_serialize_generator/lib/context.dart @@ -1,8 +1,11 @@ import 'package:analyzer/dart/element/element.dart'; import 'package:angel_serialize/angel_serialize.dart'; +import 'package:recase/recase.dart'; /// A base context for building serializable classes. class BuildContext { + ReCase _modelClassNameRecase; + /// A map of field names to resolved names from `@Alias()` declarations. final Map aliases = {}; @@ -26,6 +29,9 @@ class BuildContext { ? originalClassName.substring(1) : originalClassName; + /// A [ReCase] instance reflecting on the [modelClassName]. + ReCase get modelClassNameRecase => _modelClassNameRecase ??= new ReCase(modelClassName); + /// The [FieldElement] pointing to the primary key. FieldElement get primaryKeyField => fields.firstWhere((f) => f.name == primaryKeyName); diff --git a/angel_serialize_generator/lib/model.dart b/angel_serialize_generator/lib/model.dart new file mode 100644 index 00000000..97a17289 --- /dev/null +++ b/angel_serialize_generator/lib/model.dart @@ -0,0 +1,2 @@ +part of angel_serialize_generator; +