Merge pull request #104 from dukefirehawk/bug-fix/serialize_generator
Bug fix/serialize generator
This commit is contained in:
commit
bfd05b6854
8 changed files with 226 additions and 11 deletions
13
README.md
13
README.md
|
@ -60,21 +60,22 @@ For more details, checkout [Project Status](https://github.com/dukefirehawk/ange
|
||||||
### Release 8.0.0 (Current)
|
### Release 8.0.0 (Current)
|
||||||
|
|
||||||
* Updated `angel3_` packages to require dart >= 3.0.x
|
* Updated `angel3_` packages to require dart >= 3.0.x
|
||||||
|
* Updated dependencies to the latest
|
||||||
* Resolved issues related to generated container
|
* Resolved issues related to generated container
|
||||||
|
|
||||||
### Release 7.0.0
|
### Release 7.0.0
|
||||||
|
|
||||||
* Updated `angel3_` packages to require dart >= 2.17.x
|
* Updated `angel3_` packages to require dart >= 2.17.x
|
||||||
* Updated dependencies to the latest libraries
|
* Updated dependencies to the latest
|
||||||
* Updated code generator to use `analyzer` 5.x.x
|
* Updated code generator to use `analyzer` 5.x.x
|
||||||
* Fix ORM issues
|
* Fixed ORM issues
|
||||||
|
|
||||||
### Release 6.0.0
|
### Release 6.0.0
|
||||||
|
|
||||||
* Update all `angel3_` packages to require dart >= 2.16.x
|
* Updated all `angel3_` packages to require dart >= 2.16.x
|
||||||
* Update ORM to support MariaDB 10.2.x (stable) and MySQL 8.x (beta)
|
* Updated ORM to support MariaDB 10.2.x (stable) and MySQL 8.x (beta)
|
||||||
* Update code generator to use `analyzer` 3.x.x
|
* Updated code generator to use `analyzer` 3.x.x
|
||||||
* Update exception handling
|
* Updated exception handling
|
||||||
* Added default logger to generate standardised logging messages
|
* Added default logger to generate standardised logging messages
|
||||||
* Added `melos` support
|
* Added `melos` support
|
||||||
* Removed deprecated API
|
* Removed deprecated API
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
# Change Log
|
# Change Log
|
||||||
|
|
||||||
|
## 8.0.1
|
||||||
|
|
||||||
|
* Fixed `JsonModelGenerator` from generating duplicated fields
|
||||||
|
* Fixed `JsonModelGenerator` to priortize `id` field as the first argument
|
||||||
|
|
||||||
## 8.0.0
|
## 8.0.0
|
||||||
|
|
||||||
* Require Dart >= 3.0
|
* Require Dart >= 3.0
|
||||||
|
|
|
@ -56,16 +56,37 @@ Future<BuildContext?> buildContext(
|
||||||
annotation.peek('includeAnnotations')?.listValue ?? <DartObject>[],
|
annotation.peek('includeAnnotations')?.listValue ?? <DartObject>[],
|
||||||
);
|
);
|
||||||
// var lib = await resolver.libraryFor(buildStep.inputId);
|
// var lib = await resolver.libraryFor(buildStep.inputId);
|
||||||
var fieldNames = <String>[];
|
|
||||||
var fields = <FieldElement>[];
|
var fields = <FieldElement>[];
|
||||||
|
var fieldNames = <String>[];
|
||||||
|
|
||||||
|
for (var field in fields) {
|
||||||
|
fieldNames.add(field.name);
|
||||||
|
}
|
||||||
|
|
||||||
// Crawl for classes from parent classes.
|
// Crawl for classes from parent classes.
|
||||||
void crawlClass(InterfaceType? t) {
|
void crawlClass(InterfaceType? t) {
|
||||||
while (t != null) {
|
while (t != null) {
|
||||||
fields.insertAll(0, t.element.fields);
|
// Check and skip fields with same name
|
||||||
|
var parentClassFields = <FieldElement>[];
|
||||||
|
for (var el in t.element.fields) {
|
||||||
|
if (fieldNames.contains(el.name)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
parentClassFields.add(el);
|
||||||
|
fieldNames.add(el.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
fields.insertAll(0, parentClassFields);
|
||||||
t.interfaces.forEach(crawlClass);
|
t.interfaces.forEach(crawlClass);
|
||||||
t = t.superclass;
|
t = t.superclass;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Move id field to the front if exist
|
||||||
|
var item = fields.firstWhereOrNull((el) => el.name == 'id');
|
||||||
|
if (item != null) {
|
||||||
|
fields.removeWhere((el) => el.name == 'id');
|
||||||
|
fields.insert(0, item);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
crawlClass(clazz.thisType);
|
crawlClass(clazz.thisType);
|
||||||
|
@ -79,7 +100,7 @@ Future<BuildContext?> buildContext(
|
||||||
if (field.getter != null &&
|
if (field.getter != null &&
|
||||||
(field.setter != null || field.getter!.isAbstract)) {
|
(field.setter != null || field.getter!.isAbstract)) {
|
||||||
var el = field.setter == null ? field.getter! : field;
|
var el = field.setter == null ? field.getter! : field;
|
||||||
fieldNames.add(field.name);
|
//fieldNames.add(field.name);
|
||||||
|
|
||||||
// Check for @SerializableField
|
// Check for @SerializableField
|
||||||
var fieldAnn = serializableFieldTypeChecker.firstAnnotationOf(el);
|
var fieldAnn = serializableFieldTypeChecker.firstAnnotationOf(el);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
name: angel3_serialize_generator
|
name: angel3_serialize_generator
|
||||||
version: 8.0.0
|
version: 8.0.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/master/packages/serialize/angel_serialize_generator
|
repository: https://github.com/dukefirehawk/angel/tree/master/packages/serialize/angel_serialize_generator
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
abstract class Character {
|
||||||
|
String? get id;
|
||||||
|
|
||||||
|
String? get name;
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
import 'package:angel3_serialize/angel3_serialize.dart';
|
||||||
|
|
||||||
|
import 'character.dart';
|
||||||
|
|
||||||
|
part 'droid.g.dart';
|
||||||
|
|
||||||
|
@serializable
|
||||||
|
abstract class _Droid extends Model implements Character {
|
||||||
|
String get position;
|
||||||
|
|
||||||
|
num get age;
|
||||||
|
}
|
|
@ -0,0 +1,171 @@
|
||||||
|
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||||
|
|
||||||
|
part of 'droid.dart';
|
||||||
|
|
||||||
|
// **************************************************************************
|
||||||
|
// JsonModelGenerator
|
||||||
|
// **************************************************************************
|
||||||
|
|
||||||
|
@generatedSerializable
|
||||||
|
class Droid extends _Droid {
|
||||||
|
Droid({
|
||||||
|
this.id,
|
||||||
|
this.createdAt,
|
||||||
|
this.updatedAt,
|
||||||
|
this.name,
|
||||||
|
required this.position,
|
||||||
|
required this.age,
|
||||||
|
});
|
||||||
|
|
||||||
|
@override
|
||||||
|
String? id;
|
||||||
|
|
||||||
|
/// The time at which this item was created.
|
||||||
|
@override
|
||||||
|
DateTime? createdAt;
|
||||||
|
|
||||||
|
/// The last time at which this item was updated.
|
||||||
|
@override
|
||||||
|
DateTime? updatedAt;
|
||||||
|
|
||||||
|
@override
|
||||||
|
String? name;
|
||||||
|
|
||||||
|
@override
|
||||||
|
String position;
|
||||||
|
|
||||||
|
@override
|
||||||
|
num age;
|
||||||
|
|
||||||
|
Droid copyWith({
|
||||||
|
String? id,
|
||||||
|
DateTime? createdAt,
|
||||||
|
DateTime? updatedAt,
|
||||||
|
String? name,
|
||||||
|
String? position,
|
||||||
|
num? age,
|
||||||
|
}) {
|
||||||
|
return Droid(
|
||||||
|
id: id ?? this.id,
|
||||||
|
createdAt: createdAt ?? this.createdAt,
|
||||||
|
updatedAt: updatedAt ?? this.updatedAt,
|
||||||
|
name: name ?? this.name,
|
||||||
|
position: position ?? this.position,
|
||||||
|
age: age ?? this.age);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool operator ==(other) {
|
||||||
|
return other is _Droid &&
|
||||||
|
other.id == id &&
|
||||||
|
other.createdAt == createdAt &&
|
||||||
|
other.updatedAt == updatedAt &&
|
||||||
|
other.name == name &&
|
||||||
|
other.position == position &&
|
||||||
|
other.age == age;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
int get hashCode {
|
||||||
|
return hashObjects([
|
||||||
|
id,
|
||||||
|
createdAt,
|
||||||
|
updatedAt,
|
||||||
|
name,
|
||||||
|
position,
|
||||||
|
age,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() {
|
||||||
|
return 'Droid(id=$id, createdAt=$createdAt, updatedAt=$updatedAt, name=$name, position=$position, age=$age)';
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() {
|
||||||
|
return DroidSerializer.toMap(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// **************************************************************************
|
||||||
|
// SerializerGenerator
|
||||||
|
// **************************************************************************
|
||||||
|
|
||||||
|
const DroidSerializer droidSerializer = DroidSerializer();
|
||||||
|
|
||||||
|
class DroidEncoder extends Converter<Droid, Map> {
|
||||||
|
const DroidEncoder();
|
||||||
|
|
||||||
|
@override
|
||||||
|
Map convert(Droid model) => DroidSerializer.toMap(model);
|
||||||
|
}
|
||||||
|
|
||||||
|
class DroidDecoder extends Converter<Map, Droid> {
|
||||||
|
const DroidDecoder();
|
||||||
|
|
||||||
|
@override
|
||||||
|
Droid convert(Map map) => DroidSerializer.fromMap(map);
|
||||||
|
}
|
||||||
|
|
||||||
|
class DroidSerializer extends Codec<Droid, Map> {
|
||||||
|
const DroidSerializer();
|
||||||
|
|
||||||
|
@override
|
||||||
|
DroidEncoder get encoder => const DroidEncoder();
|
||||||
|
@override
|
||||||
|
DroidDecoder get decoder => const DroidDecoder();
|
||||||
|
static Droid fromMap(Map map) {
|
||||||
|
return Droid(
|
||||||
|
id: map['id'] as String?,
|
||||||
|
createdAt: map['created_at'] != null
|
||||||
|
? (map['created_at'] is DateTime
|
||||||
|
? (map['created_at'] as DateTime)
|
||||||
|
: DateTime.parse(map['created_at'].toString()))
|
||||||
|
: null,
|
||||||
|
updatedAt: map['updated_at'] != null
|
||||||
|
? (map['updated_at'] is DateTime
|
||||||
|
? (map['updated_at'] as DateTime)
|
||||||
|
: DateTime.parse(map['updated_at'].toString()))
|
||||||
|
: null,
|
||||||
|
name: map['name'] as String?,
|
||||||
|
position: map['position'] as String,
|
||||||
|
age: map['age'] as num);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Map<String, dynamic> toMap(_Droid? model) {
|
||||||
|
if (model == null) {
|
||||||
|
throw FormatException("Required field [model] cannot be null");
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
'id': model.id,
|
||||||
|
'created_at': model.createdAt?.toIso8601String(),
|
||||||
|
'updated_at': model.updatedAt?.toIso8601String(),
|
||||||
|
'name': model.name,
|
||||||
|
'position': model.position,
|
||||||
|
'age': model.age
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract class DroidFields {
|
||||||
|
static const List<String> allFields = <String>[
|
||||||
|
id,
|
||||||
|
createdAt,
|
||||||
|
updatedAt,
|
||||||
|
name,
|
||||||
|
position,
|
||||||
|
age,
|
||||||
|
];
|
||||||
|
|
||||||
|
static const String id = 'id';
|
||||||
|
|
||||||
|
static const String createdAt = 'created_at';
|
||||||
|
|
||||||
|
static const String updatedAt = 'updated_at';
|
||||||
|
|
||||||
|
static const String name = 'name';
|
||||||
|
|
||||||
|
static const String position = 'position';
|
||||||
|
|
||||||
|
static const String age = 'age';
|
||||||
|
}
|
|
@ -4,4 +4,4 @@ environment:
|
||||||
sdk: '>=2.18.0 <3.0.0'
|
sdk: '>=2.18.0 <3.0.0'
|
||||||
|
|
||||||
dependencies:
|
dependencies:
|
||||||
melos: 3.0.0
|
melos: ^3.1.0
|
||||||
|
|
Loading…
Reference in a new issue