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.
|
/// Marks a class as eligible for serialization.
|
||||||
class Serializable {
|
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();
|
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.
|
/// Specifies an alias for a field within its JSON representation.
|
||||||
class Alias {
|
class Alias {
|
||||||
final String name;
|
final String name;
|
||||||
|
|
||||||
const Alias(this.name);
|
const Alias(this.name);
|
||||||
}
|
}
|
|
@ -19,7 +19,7 @@ const TypeChecker serializableTypeChecker =
|
||||||
/// Create a [BuildContext].
|
/// Create a [BuildContext].
|
||||||
Future<BuildContext> buildContext(
|
Future<BuildContext> buildContext(
|
||||||
ClassElement clazz,
|
ClassElement clazz,
|
||||||
Serializable annotation,
|
ConstantReader annotation,
|
||||||
BuildStep buildStep,
|
BuildStep buildStep,
|
||||||
Resolver resolver,
|
Resolver resolver,
|
||||||
bool autoSnakeCaseNames,
|
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 (autoIdAndDateFields != false) {
|
||||||
if (!fieldNames.contains('id')) {
|
if (!fieldNames.contains('id')) {
|
||||||
var idField =
|
var idField =
|
||||||
|
|
|
@ -2,6 +2,7 @@ import 'package:analyzer/dart/element/element.dart';
|
||||||
import 'package:angel_serialize/angel_serialize.dart';
|
import 'package:angel_serialize/angel_serialize.dart';
|
||||||
import 'package:code_builder/code_builder.dart';
|
import 'package:code_builder/code_builder.dart';
|
||||||
import 'package:recase/recase.dart';
|
import 'package:recase/recase.dart';
|
||||||
|
import 'package:source_gen/source_gen.dart';
|
||||||
|
|
||||||
/// A base context for building serializable classes.
|
/// A base context for building serializable classes.
|
||||||
class BuildContext {
|
class BuildContext {
|
||||||
|
@ -22,7 +23,7 @@ class BuildContext {
|
||||||
/// The fields declared on the original class.
|
/// The fields declared on the original class.
|
||||||
final List<FieldElement> fields = [];
|
final List<FieldElement> fields = [];
|
||||||
|
|
||||||
final Serializable annotation;
|
final ConstantReader annotation;
|
||||||
|
|
||||||
/// The name of the field that identifies data of this model type.
|
/// The name of the field that identifies data of this model type.
|
||||||
String primaryKeyName = 'id';
|
String primaryKeyName = 'id';
|
||||||
|
|
|
@ -1,25 +1,18 @@
|
||||||
part of angel_serialize_generator;
|
part of angel_serialize_generator;
|
||||||
|
|
||||||
class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
|
class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
|
||||||
final bool autoSnakeCaseNames;
|
|
||||||
final bool autoIdAndDateFields;
|
final bool autoIdAndDateFields;
|
||||||
|
|
||||||
const JsonModelGenerator(
|
const JsonModelGenerator({this.autoIdAndDateFields: true});
|
||||||
{this.autoSnakeCaseNames: true, this.autoIdAndDateFields: true});
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<String> generateForAnnotatedElement(
|
Future<String> generateForAnnotatedElement(
|
||||||
Element element, ConstantReader reader, BuildStep buildStep) async {
|
Element element, ConstantReader annotation, BuildStep buildStep) async {
|
||||||
if (element.kind != ElementKind.CLASS)
|
if (element.kind != ElementKind.CLASS)
|
||||||
throw 'Only classes can be annotated with a @Serializable() annotation.';
|
throw 'Only classes can be annotated with a @Serializable() annotation.';
|
||||||
|
|
||||||
var ctx = await buildContext(
|
var ctx = await buildContext(element, annotation, buildStep,
|
||||||
element,
|
await buildStep.resolver, true, autoIdAndDateFields != false);
|
||||||
serializable,
|
|
||||||
buildStep,
|
|
||||||
await buildStep.resolver,
|
|
||||||
autoSnakeCaseNames != false,
|
|
||||||
autoIdAndDateFields != false);
|
|
||||||
|
|
||||||
var lib = new File((b) {
|
var lib = new File((b) {
|
||||||
generateClass(ctx, b);
|
generateClass(ctx, b);
|
||||||
|
|
Loading…
Reference in a new issue