diff --git a/angel_serialize/lib/angel_serialize.dart b/angel_serialize/lib/angel_serialize.dart index 03a6e0cc..90d3733c 100644 --- a/angel_serialize/lib/angel_serialize.dart +++ b/angel_serialize/lib/angel_serialize.dart @@ -7,13 +7,37 @@ const Exclude exclude = const Exclude(); /// Marks a class as eligible for serialization. class Serializable { - const Serializable(); + const Serializable( + {this.serializers: const [Serializers.MAP, Serializers.JSON], + this.autoSnakeCaseNames: true, + this.autoIdAndDateFields: true}); + + /// A list of enabled serialization modes. + /// + /// See [Serializers]. + final List serializers; + + /// Overrides the setting in `SerializerGenerator`. + final bool autoSnakeCaseNames; + + /// Overrides the setting in `JsonModelGenerator`. + final bool autoIdAndDateFields; } const Serializable serializable = const Serializable(); +/// The supported serialization types. +abstract class Serializers { + /// Enable `fromMap` and `toMap` methods on the model class. + static const int MAP = 0; + + /// Enable a `toJson` method on the model class. + static const int JSON = 1; +} + /// Specifies an alias for a field within its JSON representation. class Alias { final String name; + const Alias(this.name); -} \ 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 5400747d..76ccabf4 100644 --- a/angel_serialize_generator/lib/build_context.dart +++ b/angel_serialize_generator/lib/build_context.dart @@ -19,7 +19,7 @@ const TypeChecker serializableTypeChecker = /// Create a [BuildContext]. Future buildContext( ClassElement clazz, - Serializable annotation, + ConstantReader annotation, BuildStep buildStep, Resolver resolver, bool autoSnakeCaseNames, @@ -57,6 +57,10 @@ Future buildContext( } } + // Check for autoIdAndDateFields, autoSnakeCaseNames + autoIdAndDateFields = annotation.peek('autoIdAndDateFields')?.boolValue ?? autoIdAndDateFields; + autoSnakeCaseNames = annotation.peek('autoSnakeCaseNames')?.boolValue ?? autoSnakeCaseNames; + if (autoIdAndDateFields != false) { if (!fieldNames.contains('id')) { var idField = diff --git a/angel_serialize_generator/lib/context.dart b/angel_serialize_generator/lib/context.dart index b5c99268..afa9f5f6 100644 --- a/angel_serialize_generator/lib/context.dart +++ b/angel_serialize_generator/lib/context.dart @@ -2,6 +2,7 @@ import 'package:analyzer/dart/element/element.dart'; import 'package:angel_serialize/angel_serialize.dart'; import 'package:code_builder/code_builder.dart'; import 'package:recase/recase.dart'; +import 'package:source_gen/source_gen.dart'; /// A base context for building serializable classes. class BuildContext { @@ -22,7 +23,7 @@ class BuildContext { /// The fields declared on the original class. final List fields = []; - final Serializable annotation; + final ConstantReader annotation; /// The name of the field that identifies data of this model type. String primaryKeyName = 'id'; diff --git a/angel_serialize_generator/lib/model.dart b/angel_serialize_generator/lib/model.dart index f0f599c5..ed7e9b1d 100644 --- a/angel_serialize_generator/lib/model.dart +++ b/angel_serialize_generator/lib/model.dart @@ -1,25 +1,18 @@ part of angel_serialize_generator; class JsonModelGenerator extends GeneratorForAnnotation { - final bool autoSnakeCaseNames; final bool autoIdAndDateFields; - const JsonModelGenerator( - {this.autoSnakeCaseNames: true, this.autoIdAndDateFields: true}); + const JsonModelGenerator({this.autoIdAndDateFields: true}); @override Future generateForAnnotatedElement( - Element element, ConstantReader reader, BuildStep buildStep) async { + 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, - serializable, - buildStep, - await buildStep.resolver, - autoSnakeCaseNames != false, - autoIdAndDateFields != false); + var ctx = await buildContext(element, annotation, buildStep, + await buildStep.resolver, true, autoIdAndDateFields != false); var lib = new File((b) { generateClass(ctx, b);