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
* Require Dart >= 2.17
* Fixed enum test cases
* Resolved deprecated methods
## 6.1.1

View file

@ -112,13 +112,22 @@ String? dartObjectToString(DartObject v) {
.accept(DartEmitter(useNullSafetySyntax: true))
.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.
for (var field in type.element2.fields) {
if (field.isEnumConstant && field.isStatic) {
var value = type.element2.getField(field.name)!.computeConstantValue();
if (value == v) {
return '${type.element2.displayName}.${field.name}';
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) {
return '${type.element2.displayName}.${field.name}';
}
}
}
}

View file

@ -79,7 +79,13 @@ class BuildContext {
fields.firstWhere((f) => f.name == primaryKeyName);
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.

View file

@ -9,7 +9,9 @@ WithEnum aWithEnum2 = WithEnum(type: WithEnumType.a);
void main() {
test('enum serializes to int', () {
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', () {
@ -24,9 +26,7 @@ void main() {
});
test('enum deserializes from int', () {
var map = {
WithEnumFields.type: WithEnumType.values.indexOf(WithEnumType.b)
};
var map = {WithEnumFields.type: WithEnumType.b};
var w = WithEnumSerializer.fromMap(map);
expect(w.type, WithEnumType.b);
});

View file

@ -81,11 +81,7 @@ class WithEnumSerializer extends Codec<WithEnum, Map> {
WithEnumDecoder get decoder => const WithEnumDecoder();
static WithEnum fromMap(Map map) {
return WithEnum(
type: map['type'] is WithEnumType?
? (map['type'] as WithEnumType?) ?? WithEnumType.b
: (map['type'] is int
? WithEnumType?.values[map['type'] as int]
: WithEnumType.b),
type: map['type'] as WithEnumType? ?? WithEnumType.b,
finalList: map['final_list'] is Iterable
? (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");
}
return {
'type':
model.type != null ? WithEnumType.values.indexOf(model.type!) : null,
'type': model.type,
'final_list': model.finalList,
'image_bytes':
model.imageBytes != null ? base64.encode(model.imageBytes!) : null