Static serializer types, also autoIdAndDateFields, autoSnakeCaseNames
This commit is contained in:
parent
29ed032e61
commit
53f6b468a5
4 changed files with 37 additions and 15 deletions
|
@ -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<int> 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);
|
||||
}
|
|
@ -19,7 +19,7 @@ const TypeChecker serializableTypeChecker =
|
|||
/// Create a [BuildContext].
|
||||
Future<BuildContext> buildContext(
|
||||
ClassElement clazz,
|
||||
Serializable annotation,
|
||||
ConstantReader annotation,
|
||||
BuildStep buildStep,
|
||||
Resolver resolver,
|
||||
bool autoSnakeCaseNames,
|
||||
|
@ -57,6 +57,10 @@ Future<BuildContext> 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 =
|
||||
|
|
|
@ -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<FieldElement> fields = [];
|
||||
|
||||
final Serializable annotation;
|
||||
final ConstantReader annotation;
|
||||
|
||||
/// The name of the field that identifies data of this model type.
|
||||
String primaryKeyName = 'id';
|
||||
|
|
|
@ -1,25 +1,18 @@
|
|||
part of angel_serialize_generator;
|
||||
|
||||
class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
|
||||
final bool autoSnakeCaseNames;
|
||||
final bool autoIdAndDateFields;
|
||||
|
||||
const JsonModelGenerator(
|
||||
{this.autoSnakeCaseNames: true, this.autoIdAndDateFields: true});
|
||||
const JsonModelGenerator({this.autoIdAndDateFields: true});
|
||||
|
||||
@override
|
||||
Future<String> 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);
|
||||
|
|
Loading…
Reference in a new issue