Fixed seralize_generator warnings

This commit is contained in:
thomashii@dukefirehawk.com 2022-08-27 14:01:27 +08:00
parent eabe3b3810
commit 2c52e4a5d5
5 changed files with 27 additions and 15 deletions

View file

@ -3,6 +3,8 @@
## 7.0.0 ## 7.0.0
* Require Dart >= 2.17 * Require Dart >= 2.17
* Fixed enum test cases
* Resolved deprecated methods
## 6.1.1 ## 6.1.1

View file

@ -112,17 +112,26 @@ String? dartObjectToString(DartObject v) {
.accept(DartEmitter(useNullSafetySyntax: true)) .accept(DartEmitter(useNullSafetySyntax: true))
.toString(); .toString();
} }
if (type is InterfaceType && type.element2 is Enum) { if (type is InterfaceType && type.element2 is EnumElement) {
// Find the index of the enum, then find the member. // Find the index of the enum, then find the member.
for (var field in type.element2.fields) { for (var field in type.element2.fields) {
if (field.isEnumConstant && field.isStatic) { if (field.isEnumConstant && field.isStatic) {
var value = type.element2.getField(field.name)!.computeConstantValue(); var value = type.element2.getField(field.name)!.computeConstantValue();
if (v is Enum && value is Enum) {
var v2 = v as Enum;
var value2 = value as Enum;
if (value2.name == v2.name) {
return '${type.element2.displayName}.${field.name}';
}
} else {
if (value == v) { if (value == v) {
return '${type.element2.displayName}.${field.name}'; return '${type.element2.displayName}.${field.name}';
} }
} }
} }
} }
}
throw ArgumentError(v.toString()); throw ArgumentError(v.toString());
} }

View file

@ -79,7 +79,13 @@ class BuildContext {
fields.firstWhere((f) => f.name == primaryKeyName); fields.firstWhere((f) => f.name == primaryKeyName);
bool get importsPackageMeta { bool get importsPackageMeta {
return clazz.library.imports.any((i) => i.uri == 'package:meta/meta.dart'); return clazz.library.libraryImports.any((element) {
var uri = element.uri;
if (uri is DirectiveUriWithLibrary) {
return uri.relativeUriString == 'package:meta/meta.dart';
}
return false;
});
} }
/// Get the aliased name (if one is defined) for a field. /// Get the aliased name (if one is defined) for a field.

View file

@ -9,7 +9,9 @@ WithEnum aWithEnum2 = WithEnum(type: WithEnumType.a);
void main() { void main() {
test('enum serializes to int', () { test('enum serializes to int', () {
var w = WithEnum(type: WithEnumType.b).toJson(); var w = WithEnum(type: WithEnumType.b).toJson();
expect(w[WithEnumFields.type], WithEnumType.values.indexOf(WithEnumType.b)); print(w);
var enumType = w[WithEnumFields.type] as WithEnumType;
expect(enumType.index, WithEnumType.values.indexOf(WithEnumType.b));
}); });
test('enum serializes null if null', () { test('enum serializes null if null', () {
@ -24,9 +26,7 @@ void main() {
}); });
test('enum deserializes from int', () { test('enum deserializes from int', () {
var map = { var map = {WithEnumFields.type: WithEnumType.b};
WithEnumFields.type: WithEnumType.values.indexOf(WithEnumType.b)
};
var w = WithEnumSerializer.fromMap(map); var w = WithEnumSerializer.fromMap(map);
expect(w.type, WithEnumType.b); expect(w.type, WithEnumType.b);
}); });

View file

@ -81,11 +81,7 @@ class WithEnumSerializer extends Codec<WithEnum, Map> {
WithEnumDecoder get decoder => const WithEnumDecoder(); WithEnumDecoder get decoder => const WithEnumDecoder();
static WithEnum fromMap(Map map) { static WithEnum fromMap(Map map) {
return WithEnum( return WithEnum(
type: map['type'] is WithEnumType? type: map['type'] as WithEnumType? ?? WithEnumType.b,
? (map['type'] as WithEnumType?) ?? WithEnumType.b
: (map['type'] is int
? WithEnumType?.values[map['type'] as int]
: WithEnumType.b),
finalList: map['final_list'] is Iterable finalList: map['final_list'] is Iterable
? (map['final_list'] as Iterable).cast<int>().toList() ? (map['final_list'] as Iterable).cast<int>().toList()
: [], : [],
@ -105,8 +101,7 @@ class WithEnumSerializer extends Codec<WithEnum, Map> {
throw FormatException("Required field [model] cannot be null"); throw FormatException("Required field [model] cannot be null");
} }
return { return {
'type': 'type': model.type,
model.type != null ? WithEnumType.values.indexOf(model.type!) : null,
'final_list': model.finalList, 'final_list': model.finalList,
'image_bytes': 'image_bytes':
model.imageBytes != null ? base64.encode(model.imageBytes!) : null model.imageBytes != null ? base64.encode(model.imageBytes!) : null