Updated serializer generator
This commit is contained in:
parent
ab347f1b3e
commit
4f36170ead
4 changed files with 32 additions and 13 deletions
|
@ -1,8 +1,12 @@
|
||||||
# Change Log
|
# Change Log
|
||||||
|
|
||||||
|
## 4.1.1
|
||||||
|
|
||||||
|
* Fixed `SerializerGenerator` to recognize nullable class
|
||||||
|
|
||||||
## 4.1.0
|
## 4.1.0
|
||||||
|
|
||||||
* Upgraded to support major `analyzer` 2.0.0 release
|
* Upgraded to support `analyzer` 2.0.0 major release
|
||||||
|
|
||||||
## 4.0.3
|
## 4.0.3
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
# Angel3 Serialize Generator
|
# Angel3 Serialize Generator
|
||||||
|
|
||||||
[![version](https://img.shields.io/badge/pub-v4.1.0-brightgreen)](https://pub.dartlang.org/packages/angel3_serialize_generator)
|
[![version](https://img.shields.io/badge/pub-v4.1.1-brightgreen)](https://pub.dartlang.org/packages/angel3_serialize_generator)
|
||||||
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
|
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
|
||||||
[![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion)
|
[![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion)
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,8 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
|
||||||
@override
|
@override
|
||||||
Future<String?> generateForAnnotatedElement(
|
Future<String?> generateForAnnotatedElement(
|
||||||
Element element, ConstantReader annotation, BuildStep buildStep) async {
|
Element element, ConstantReader annotation, BuildStep buildStep) async {
|
||||||
|
log.fine('Running SerializerGenerator');
|
||||||
|
|
||||||
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.';
|
||||||
}
|
}
|
||||||
|
@ -15,9 +17,16 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
|
||||||
var ctx = await buildContext(element as ClassElement, annotation, buildStep,
|
var ctx = await buildContext(element as ClassElement, annotation, buildStep,
|
||||||
buildStep.resolver, autoSnakeCaseNames != false);
|
buildStep.resolver, autoSnakeCaseNames != false);
|
||||||
|
|
||||||
|
if (ctx == null) {
|
||||||
|
log.severe('Invalid builder context');
|
||||||
|
throw 'Invalid builder context';
|
||||||
|
}
|
||||||
|
|
||||||
var serializers = annotation.peek('serializers')?.listValue ?? [];
|
var serializers = annotation.peek('serializers')?.listValue ?? [];
|
||||||
|
|
||||||
if (serializers.isEmpty) return null;
|
if (serializers.isEmpty) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
// Check if any serializer is recognized
|
// Check if any serializer is recognized
|
||||||
if (!serializers.any((s) => Serializers.all.contains(s.toIntValue()))) {
|
if (!serializers.any((s) => Serializers.all.contains(s.toIntValue()))) {
|
||||||
|
@ -26,7 +35,7 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
|
||||||
|
|
||||||
var lib = Library((b) {
|
var lib = Library((b) {
|
||||||
generateClass(
|
generateClass(
|
||||||
serializers.map((s) => s.toIntValue() ?? 0).toList(), ctx!, b);
|
serializers.map((s) => s.toIntValue() ?? 0).toList(), ctx, b);
|
||||||
generateFieldsClass(ctx, b);
|
generateFieldsClass(ctx, b);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -37,9 +46,13 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
|
||||||
/// Generate a serializer class.
|
/// Generate a serializer class.
|
||||||
void generateClass(
|
void generateClass(
|
||||||
List<int> serializers, BuildContext ctx, LibraryBuilder file) {
|
List<int> serializers, BuildContext ctx, LibraryBuilder file) {
|
||||||
|
log.fine('Generate serailizer class');
|
||||||
|
|
||||||
// Generate canonical codecs, etc.
|
// Generate canonical codecs, etc.
|
||||||
var pascal = ctx.modelClassNameRecase.pascalCase,
|
var pascal = ctx.modelClassNameRecase.pascalCase.replaceAll('?', '');
|
||||||
camel = ctx.modelClassNameRecase.camelCase;
|
var camel = ctx.modelClassNameRecase.camelCase.replaceAll('?', '');
|
||||||
|
|
||||||
|
log.fine('Pascal = $pascal, camel = $camel');
|
||||||
|
|
||||||
if (ctx.constructorParameters.isEmpty) {
|
if (ctx.constructorParameters.isEmpty) {
|
||||||
file.body.add(Code('''
|
file.body.add(Code('''
|
||||||
|
@ -143,7 +156,7 @@ class ${pascal}Decoder extends Converter<Map, $pascal> {
|
||||||
// if (rc.pascalCase == ctx.modelClassName) {
|
// if (rc.pascalCase == ctx.modelClassName) {
|
||||||
// return '($value)?.toJson()';
|
// return '($value)?.toJson()';
|
||||||
// }
|
// }
|
||||||
return '${rc.pascalCase}Serializer.toMap($value)';
|
return '${rc.pascalCase.replaceAll('?', '')}Serializer.toMap($value)';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctx.fieldInfo[field.name]?.serializer != null) {
|
if (ctx.fieldInfo[field.name]?.serializer != null) {
|
||||||
|
@ -285,7 +298,7 @@ class ${pascal}Decoder extends Converter<Map, $pascal> {
|
||||||
else if (isModelClass(type)) {
|
else if (isModelClass(type)) {
|
||||||
var rc = ReCase(type.getDisplayString(withNullability: true));
|
var rc = ReCase(type.getDisplayString(withNullability: true));
|
||||||
deserializedRepresentation = "map['$alias'] != null"
|
deserializedRepresentation = "map['$alias'] != null"
|
||||||
" ? ${rc.pascalCase}Serializer.fromMap(map['$alias'] as Map)"
|
" ? ${rc.pascalCase.replaceAll('?', '')}Serializer.fromMap(map['$alias'] as Map)"
|
||||||
' : $defaultValue';
|
' : $defaultValue';
|
||||||
} else if (type is InterfaceType) {
|
} else if (type is InterfaceType) {
|
||||||
if (isListOfModelType(type)) {
|
if (isListOfModelType(type)) {
|
||||||
|
@ -294,7 +307,7 @@ class ${pascal}Decoder extends Converter<Map, $pascal> {
|
||||||
deserializedRepresentation = "map['$alias'] is Iterable"
|
deserializedRepresentation = "map['$alias'] is Iterable"
|
||||||
" ? List.unmodifiable(((map['$alias'] as Iterable)"
|
" ? List.unmodifiable(((map['$alias'] as Iterable)"
|
||||||
'.whereType<Map>())'
|
'.whereType<Map>())'
|
||||||
'.map(${rc.pascalCase}Serializer.fromMap))'
|
'.map(${rc.pascalCase.replaceAll('?', '')}Serializer.fromMap))'
|
||||||
' : $defaultValue';
|
' : $defaultValue';
|
||||||
} else if (isMapToModelType(type)) {
|
} else if (isMapToModelType(type)) {
|
||||||
var rc = ReCase(
|
var rc = ReCase(
|
||||||
|
@ -302,7 +315,7 @@ class ${pascal}Decoder extends Converter<Map, $pascal> {
|
||||||
deserializedRepresentation = '''
|
deserializedRepresentation = '''
|
||||||
map['$alias'] is Map
|
map['$alias'] is Map
|
||||||
? Map.unmodifiable((map['$alias'] as Map).keys.fold({}, (out, key) {
|
? Map.unmodifiable((map['$alias'] as Map).keys.fold({}, (out, key) {
|
||||||
return out..[key] = ${rc.pascalCase}Serializer
|
return out..[key] = ${rc.pascalCase.replaceAll('?', '')}Serializer
|
||||||
.fromMap(((map['$alias'] as Map)[key]) as Map);
|
.fromMap(((map['$alias'] as Map)[key]) as Map);
|
||||||
}))
|
}))
|
||||||
: $defaultValue
|
: $defaultValue
|
||||||
|
@ -368,11 +381,13 @@ class ${pascal}Decoder extends Converter<Map, $pascal> {
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
void generateFieldsClass(BuildContext? ctx, LibraryBuilder file) {
|
void generateFieldsClass(BuildContext ctx, LibraryBuilder file) {
|
||||||
|
log.fine('Generate serializer fields');
|
||||||
|
|
||||||
file.body.add(Class((clazz) {
|
file.body.add(Class((clazz) {
|
||||||
clazz
|
clazz
|
||||||
..abstract = true
|
..abstract = true
|
||||||
..name = '${ctx!.modelClassNameRecase.pascalCase}Fields';
|
..name = '${ctx.modelClassNameRecase.pascalCase}Fields';
|
||||||
|
|
||||||
clazz.fields.add(Field((b) {
|
clazz.fields.add(Field((b) {
|
||||||
b
|
b
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
name: angel3_serialize_generator
|
name: angel3_serialize_generator
|
||||||
version: 4.1.0
|
version: 4.1.1
|
||||||
description: Angel3 model serialization generators, designed for use with Angel. Combine with angel_serialize for flexible modeling.
|
description: Angel3 model serialization generators, designed for use with Angel. Combine with angel_serialize for flexible modeling.
|
||||||
homepage: https://angel3-framework.web.app/
|
homepage: https://angel3-framework.web.app/
|
||||||
repository: https://github.com/dukefirehawk/angel/tree/angel3/packages/serialize/angel_serialize_generator
|
repository: https://github.com/dukefirehawk/angel/tree/angel3/packages/serialize/angel_serialize_generator
|
||||||
|
|
Loading…
Reference in a new issue