Publish serialize_generator

This commit is contained in:
thomashii 2021-05-15 22:37:52 +08:00
parent 203f99f2f6
commit 9bf1fbdafb
17 changed files with 62 additions and 68 deletions

View file

@ -1,4 +1,11 @@
# angel_serialize_generator # angel3_serialize_generator
[![version](https://img.shields.io/badge/pub-v4.0.0-brightgreen)](https://pub.dartlang.org/packages/angel3_serialize_generator)
[![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)
[![License](https://img.shields.io/github/license/dukefirehawk/angel)](https://github.com/dukefirehawk/angel/tree/angel3/packages/serialize/angel_serialize_generator/LICENSE)
The builder for Angel's model serialization. The builder for Angel's model serialization.
Find documentation in the main project repo: Find documentation in the main project repo:

View file

@ -1,5 +1,5 @@
// ignore_for_file: unused_element // ignore_for_file: unused_element
import 'package:angel_serialize/angel_serialize.dart'; import 'package:angel3_serialize/angel3_serialize.dart';
part 'main.g.dart'; part 'main.g.dart';
@serializable @serializable

View file

@ -21,6 +21,7 @@ class Todo extends _Todo {
text: text ?? this.text, completed: completed ?? this.completed); text: text ?? this.text, completed: completed ?? this.completed);
} }
@override
bool operator ==(other) { bool operator ==(other) {
return other is _Todo && other.text == text && other.completed == completed; return other is _Todo && other.text == text && other.completed == completed;
} }
@ -32,7 +33,7 @@ class Todo extends _Todo {
@override @override
String toString() { String toString() {
return "Todo(text=$text, completed=$completed)"; return 'Todo(text=$text, completed=$completed)';
} }
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {
@ -64,9 +65,11 @@ class TodoSerializer extends Codec<Todo, Map> {
const TodoSerializer(); const TodoSerializer();
@override @override
get encoder => const TodoEncoder(); TodoEncoder get encoder => const TodoEncoder();
@override @override
get decoder => const TodoDecoder(); TodoDecoder get decoder => const TodoDecoder();
static Todo fromMap(Map map) { static Todo fromMap(Map map) {
return Todo( return Todo(
text: map['text'] as String?, completed: map['completed'] as bool?); text: map['text'] as String?, completed: map['completed'] as bool?);

View file

@ -1,4 +1,4 @@
library angel_serialize_generator; library angel3_serialize_generator;
import 'dart:async'; import 'dart:async';
import 'dart:mirrors'; import 'dart:mirrors';
@ -6,10 +6,10 @@ import 'dart:typed_data';
import 'package:analyzer/dart/constant/value.dart'; import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart'; import 'package:analyzer/dart/element/type.dart';
import 'package:angel_model/angel_model.dart'; import 'package:angel3_model/angel3_model.dart';
import 'package:angel_serialize/angel_serialize.dart'; import 'package:angel3_serialize/angel3_serialize.dart';
import 'package:build/build.dart'; import 'package:build/build.dart';
import 'package:code_buffer/code_buffer.dart'; import 'package:angel3_code_buffer/angel3_code_buffer.dart';
import 'package:code_builder/code_builder.dart'; import 'package:code_builder/code_builder.dart';
import 'package:path/path.dart' as p; import 'package:path/path.dart' as p;
import 'package:recase/recase.dart'; import 'package:recase/recase.dart';

View file

@ -3,7 +3,7 @@ import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart'; import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/src/dart/element/element.dart'; import 'package:analyzer/src/dart/element/element.dart';
import 'package:angel_serialize/angel_serialize.dart'; import 'package:angel3_serialize/angel3_serialize.dart';
import 'package:build/build.dart'; import 'package:build/build.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
import 'package:path/path.dart' as p; import 'package:path/path.dart' as p;

View file

@ -1,7 +1,7 @@
import 'package:analyzer/dart/constant/value.dart'; import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart'; import 'package:analyzer/dart/element/type.dart';
import 'package:angel_serialize/angel_serialize.dart'; import 'package:angel3_serialize/angel3_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'; import 'package:source_gen/source_gen.dart';

View file

@ -1,4 +1,4 @@
part of angel_serialize_generator; part of angel3_serialize_generator;
class JsonModelGenerator extends GeneratorForAnnotation<Serializable> { class JsonModelGenerator extends GeneratorForAnnotation<Serializable> {
const JsonModelGenerator(); const JsonModelGenerator();

View file

@ -1,4 +1,4 @@
part of angel_serialize_generator; part of angel3_serialize_generator;
class SerializerGenerator extends GeneratorForAnnotation<Serializable> { class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
final bool autoSnakeCaseNames; final bool autoSnakeCaseNames;
@ -13,7 +13,7 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
} }
var ctx = await buildContext(element as ClassElement, annotation, buildStep, var ctx = await buildContext(element as ClassElement, annotation, buildStep,
await buildStep.resolver, autoSnakeCaseNames != false); buildStep.resolver, autoSnakeCaseNames != false);
var serializers = annotation.peek('serializers')?.listValue ?? []; var serializers = annotation.peek('serializers')?.listValue ?? [];
@ -44,27 +44,26 @@ class SerializerGenerator extends GeneratorForAnnotation<Serializable> {
file.body.add(Code(''' file.body.add(Code('''
const ${pascal}Serializer ${camel}Serializer = ${pascal}Serializer(); const ${pascal}Serializer ${camel}Serializer = ${pascal}Serializer();
class ${pascal}Encoder extends Converter<${pascal}, Map> { class ${pascal}Encoder extends Converter<$pascal, Map> {
const ${pascal}Encoder(); const ${pascal}Encoder();
@override @override
Map convert(${pascal} model) => ${pascal}Serializer.toMap(model); Map convert($pascal model) => ${pascal}Serializer.toMap(model);
} }
class ${pascal}Decoder extends Converter<Map, ${pascal}> { class ${pascal}Decoder extends Converter<Map, $pascal> {
const ${pascal}Decoder(); const ${pascal}Decoder();
@override @override
${pascal} convert(Map map) => ${pascal}Serializer.fromMap(map); $pascal convert(Map map) => ${pascal}Serializer.fromMap(map);
} }
''')); '''));
} }
file.body.add(Class((clazz) { file.body.add(Class((clazz) {
clazz..name = '${pascal}Serializer'; clazz.name = '${pascal}Serializer';
if (ctx.constructorParameters.isEmpty) { if (ctx.constructorParameters.isEmpty) {
clazz clazz.extend = TypeReference((b) => b
..extend = TypeReference((b) => b
..symbol = 'Codec' ..symbol = 'Codec'
..types.addAll([ctx.modelClassType, refer('Map')])); ..types.addAll([ctx.modelClassType, refer('Map')]));
@ -120,7 +119,7 @@ class ${pascal}Decoder extends Converter<Map, ${pascal}> {
}); });
buf.writeln('return {'); buf.writeln('return {');
int i = 0; var i = 0;
// Add named parameters // Add named parameters
for (var field in ctx.fields) { for (var field in ctx.fields) {
@ -133,7 +132,7 @@ class ${pascal}Decoder extends Converter<Map, ${pascal}> {
if (i++ > 0) buf.write(', '); if (i++ > 0) buf.write(', ');
String serializedRepresentation = 'model.${field.name}'; var serializedRepresentation = 'model.${field.name}';
String serializerToMap(ReCase rc, String value) { String serializerToMap(ReCase rc, String value) {
// if (rc.pascalCase == ctx.modelClassName) { // if (rc.pascalCase == ctx.modelClassName) {
@ -143,7 +142,8 @@ class ${pascal}Decoder extends Converter<Map, ${pascal}> {
} }
if (ctx.fieldInfo[field.name]?.serializer != null) { if (ctx.fieldInfo[field.name]?.serializer != null) {
var name = MirrorSystem.getName(ctx.fieldInfo[field.name]!.serializer!); var name =
MirrorSystem.getName(ctx.fieldInfo[field.name]!.serializer!);
serializedRepresentation = '$name(model.${field.name})'; serializedRepresentation = '$name(model.${field.name})';
} }
@ -236,7 +236,7 @@ class ${pascal}Decoder extends Converter<Map, ${pascal}> {
}); });
buf.writeln('return ${ctx.modelClassName}('); buf.writeln('return ${ctx.modelClassName}(');
int i = 0; var i = 0;
for (var param in ctx.constructorParameters) { for (var param in ctx.constructorParameters) {
if (i++ > 0) buf.write(', '); if (i++ > 0) buf.write(', ');
@ -252,7 +252,7 @@ class ${pascal}Decoder extends Converter<Map, ${pascal}> {
if (i++ > 0) buf.write(', '); if (i++ > 0) buf.write(', ');
String deserializedRepresentation = var deserializedRepresentation =
"map['$alias'] as ${typeToString(type)}"; "map['$alias'] as ${typeToString(type)}";
String? defaultValue = 'null'; String? defaultValue = 'null';
@ -271,7 +271,7 @@ class ${pascal}Decoder extends Converter<Map, ${pascal}> {
} else if (dateTimeTypeChecker.isAssignableFromType(type)) { } else if (dateTimeTypeChecker.isAssignableFromType(type)) {
deserializedRepresentation = "map['$alias'] != null ? " deserializedRepresentation = "map['$alias'] != null ? "
"(map['$alias'] is DateTime ? (map['$alias'] as DateTime) : DateTime.parse(map['$alias'].toString()))" "(map['$alias'] is DateTime ? (map['$alias'] as DateTime) : DateTime.parse(map['$alias'].toString()))"
" : $defaultValue"; ' : $defaultValue';
} }
// Serialize model classes via `XSerializer.toMap` // Serialize model classes via `XSerializer.toMap`
@ -279,15 +279,15 @@ class ${pascal}Decoder extends Converter<Map, ${pascal}> {
var rc = ReCase(type.name!); var rc = ReCase(type.name!);
deserializedRepresentation = "map['$alias'] != null" deserializedRepresentation = "map['$alias'] != null"
" ? ${rc.pascalCase}Serializer.fromMap(map['$alias'] as Map)" " ? ${rc.pascalCase}Serializer.fromMap(map['$alias'] as Map)"
" : $defaultValue"; ' : $defaultValue';
} else if (type is InterfaceType) { } else if (type is InterfaceType) {
if (isListOfModelType(type)) { if (isListOfModelType(type)) {
var rc = ReCase(type.typeArguments[0].name!); var rc = ReCase(type.typeArguments[0].name!);
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}Serializer.fromMap))'
" : $defaultValue"; ' : $defaultValue';
} else if (isMapToModelType(type)) { } else if (isMapToModelType(type)) {
var rc = ReCase(type.typeArguments[1].name!); var rc = ReCase(type.typeArguments[1].name!);
deserializedRepresentation = ''' deserializedRepresentation = '''

View file

@ -1,4 +1,4 @@
part of angel_serialize_generator; part of angel3_serialize_generator;
class TypeScriptDefinitionBuilder implements Builder { class TypeScriptDefinitionBuilder implements Builder {
final bool autoSnakeCaseNames; final bool autoSnakeCaseNames;

View file

@ -1,27 +1,14 @@
name: angel_serialize_generator name: angel3_serialize_generator
version: 4.0.0 version: 4.0.0
description: Model serialization generators, designed for use with Angel. Combine with angel_serialize for flexible modeling. description: Model serialization generators, designed for use with Angel. Combine with angel_serialize for flexible modeling.
homepage: https://github.com/dukefirehawk/angel homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/serialize/angel_serialize_generator
publish_to: none
environment: environment:
sdk: '>=2.12.0 <3.0.0' sdk: '>=2.12.0 <3.0.0'
dependencies: dependencies:
analyzer: ^1.5.0 analyzer: ^1.5.0
angel_model: angel3_model: ^3.0.0
git: angel3_serialize: ^4.0.0
url: https://github.com/dukefirehawk/angel.git angel3_code_buffer: ^2.0.0
ref: sdk-2.12.x_nnbd
path: packages/model
angel_serialize:
git:
url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x_nnbd
path: packages/serialize/angel_serialize
code_buffer:
git:
url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x_nnbd
path: packages/code_buffer
build: ^2.0.1 build: ^2.0.1
build_config: ^1.0.0 build_config: ^1.0.0
code_builder: ^4.0.0 code_builder: ^4.0.0
@ -34,7 +21,4 @@ dev_dependencies:
build_runner: ^2.0.1 build_runner: ^2.0.1
collection: ^1.15.0 collection: ^1.15.0
pedantic: ^1.11.0 pedantic: ^1.11.0
test: ^1.17.3 test: ^1.17.4
# dependency_overrides:
# angel_serialize:
# path: ../angel_serialize

View file

@ -3,7 +3,7 @@ import 'models/book.dart';
const String deathlyHallowsIsbn = '0-545-01022-5'; const String deathlyHallowsIsbn = '0-545-01022-5';
main() { void main() {
var deathlyHallows = Book( var deathlyHallows = Book(
id: '0', id: '0',
author: 'J.K. Rowling', author: 'J.K. Rowling',
@ -21,8 +21,8 @@ main() {
age: 51, age: 51,
books: [deathlyHallows], books: [deathlyHallows],
newestBook: deathlyHallows); newestBook: deathlyHallows);
Map<dynamic, dynamic> serializedJkRowling = authorSerializer.encode(jkRowling); var serializedJkRowling = authorSerializer.encode(jkRowling);
Map<dynamic, dynamic>? deathlyHallowsMap = bookSerializer.encode(deathlyHallows); var deathlyHallowsMap = bookSerializer.encode(deathlyHallows);
print('J.K. Rowling: $jkRowling'); print('J.K. Rowling: $jkRowling');
var library = Library(collection: {deathlyHallowsIsbn: deathlyHallows}); var library = Library(collection: {deathlyHallowsIsbn: deathlyHallows});
@ -63,7 +63,7 @@ main() {
test('heeds canDeserialize', () { test('heeds canDeserialize', () {
var map = Map.from(serializedJkRowling)..['obscured'] = 'foo'; var map = Map.from(serializedJkRowling)..['obscured'] = 'foo';
Author author = authorSerializer.decode(map); var author = authorSerializer.decode(map);
expect(author.obscured, 'foo'); expect(author.obscured, 'foo');
}); });

View file

@ -1,7 +1,7 @@
library angel_serialize.test.models.book; library angel_serialize.test.models.book;
import 'package:angel_model/angel_model.dart'; import 'package:angel3_model/angel3_model.dart';
import 'package:angel_serialize/angel_serialize.dart'; import 'package:angel3_serialize/angel3_serialize.dart';
import 'package:collection/collection.dart'; import 'package:collection/collection.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
part 'book.g.dart'; part 'book.g.dart';

View file

@ -1,4 +1,4 @@
import 'package:angel_serialize/angel_serialize.dart'; import 'package:angel3_serialize/angel3_serialize.dart';
part 'game_pad_button.g.dart'; part 'game_pad_button.g.dart';
@serializable @serializable

View file

@ -1,4 +1,4 @@
import 'package:angel_serialize/angel_serialize.dart'; import 'package:angel3_serialize/angel3_serialize.dart';
import 'package:collection/collection.dart'; import 'package:collection/collection.dart';
part 'goat.g.dart'; part 'goat.g.dart';

View file

@ -1,7 +1,7 @@
import 'dart:convert'; import 'dart:convert';
import 'package:angel_serialize/angel_serialize.dart'; import 'package:angel3_serialize/angel3_serialize.dart';
import 'package:collection/collection.dart'; import 'package:collection/collection.dart';
import 'package:meta/meta.dart'; //import 'package:meta/meta.dart';
part 'has_map.g.dart'; part 'has_map.g.dart';
Map? _fromString(v) => json.decode(v.toString()) as Map?; Map? _fromString(v) => json.decode(v.toString()) as Map?;

View file

@ -1,4 +1,4 @@
import 'package:angel_serialize/angel_serialize.dart'; import 'package:angel3_serialize/angel3_serialize.dart';
part 'subclass.g.dart'; part 'subclass.g.dart';
@serializable @serializable

View file

@ -1,6 +1,6 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:angel_serialize/angel_serialize.dart'; import 'package:angel3_serialize/angel3_serialize.dart';
import 'package:collection/collection.dart'; import 'package:collection/collection.dart';
part 'with_enum.g.dart'; part 'with_enum.g.dart';