Merge pull request #104 from dukefirehawk/bug-fix/serialize_generator

Bug fix/serialize generator
This commit is contained in:
Thomas Hii 2023-06-25 09:53:34 +08:00 committed by GitHub
commit bfd05b6854
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 226 additions and 11 deletions

View file

@ -60,21 +60,22 @@ For more details, checkout [Project Status](https://github.com/dukefirehawk/ange
### Release 8.0.0 (Current)
* Updated `angel3_` packages to require dart >= 3.0.x
* Updated dependencies to the latest
* Resolved issues related to generated container
### Release 7.0.0
* 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
* Fix ORM issues
* Fixed ORM issues
### Release 6.0.0
* Update all `angel3_` packages to require dart >= 2.16.x
* Update ORM to support MariaDB 10.2.x (stable) and MySQL 8.x (beta)
* Update code generator to use `analyzer` 3.x.x
* Update exception handling
* Updated all `angel3_` packages to require dart >= 2.16.x
* Updated ORM to support MariaDB 10.2.x (stable) and MySQL 8.x (beta)
* Updated code generator to use `analyzer` 3.x.x
* Updated exception handling
* Added default logger to generate standardised logging messages
* Added `melos` support
* Removed deprecated API

View file

@ -1,5 +1,10 @@
# Change Log
## 8.0.1
* Fixed `JsonModelGenerator` from generating duplicated fields
* Fixed `JsonModelGenerator` to priortize `id` field as the first argument
## 8.0.0
* Require Dart >= 3.0

View file

@ -56,16 +56,37 @@ Future<BuildContext?> buildContext(
annotation.peek('includeAnnotations')?.listValue ?? <DartObject>[],
);
// var lib = await resolver.libraryFor(buildStep.inputId);
var fieldNames = <String>[];
var fields = <FieldElement>[];
var fieldNames = <String>[];
for (var field in fields) {
fieldNames.add(field.name);
}
// Crawl for classes from parent classes.
void crawlClass(InterfaceType? t) {
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 = 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);
@ -79,7 +100,7 @@ Future<BuildContext?> buildContext(
if (field.getter != null &&
(field.setter != null || field.getter!.isAbstract)) {
var el = field.setter == null ? field.getter! : field;
fieldNames.add(field.name);
//fieldNames.add(field.name);
// Check for @SerializableField
var fieldAnn = serializableFieldTypeChecker.firstAnnotationOf(el);

View file

@ -1,5 +1,5 @@
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.
homepage: https://angel3-framework.web.app/
repository: https://github.com/dukefirehawk/angel/tree/master/packages/serialize/angel_serialize_generator

View file

@ -0,0 +1,5 @@
abstract class Character {
String? get id;
String? get name;
}

View file

@ -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;
}

View file

@ -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';
}

View file

@ -4,4 +4,4 @@ environment:
sdk: '>=2.18.0 <3.0.0'
dependencies:
melos: 3.0.0
melos: ^3.1.0