From cf8d9814e8ab26ed727f0578176114e9005aa097 Mon Sep 17 00:00:00 2001 From: "thomashii@dukefirehawk.com" Date: Sat, 3 Dec 2022 11:14:24 +0800 Subject: [PATCH] Fixed Enum null check warnings --- .../angel_orm_generator/lib/src/orm_generator.dart | 13 ++++++++++--- .../orm/angel_orm_test/lib/src/models/has_car.dart | 1 - .../lib/angel3_serialize_generator.dart | 11 ++++++++--- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/packages/orm/angel_orm_generator/lib/src/orm_generator.dart b/packages/orm/angel_orm_generator/lib/src/orm_generator.dart index e7bd8683..69e607e9 100644 --- a/packages/orm/angel_orm_generator/lib/src/orm_generator.dart +++ b/packages/orm/angel_orm_generator/lib/src/orm_generator.dart @@ -249,7 +249,7 @@ class OrmGenerator extends GeneratorForAnnotation { var args = {}; for (var field in ctx.effectiveFields) { var fType = field.type; - Reference type = convertTypeReference(field.type); + Reference type = convertTypeReference(fType); if (isSpecialId(ctx, field)) { type = refer('int'); } @@ -272,9 +272,16 @@ class OrmGenerator extends GeneratorForAnnotation { expr = refer('mapToDouble').call([expr]); } else if (fType is InterfaceType && fType.element is EnumElement) { + /* + * fields.contains('type') ? row[3] == null ? null : + * EnumType.values[(row[3] as int)] : null, + */ var isNull = expr.equalTo(literalNull); + + Reference enumType = + convertTypeReference(fType, ignoreNullabilityCheck: true); expr = isNull.conditional(literalNull, - type.property('values').index(expr.asA(refer('int')))); + enumType.property('values').index(expr.asA(refer('int')))); } else if (fType.isDartCoreBool) { // Generated Code: mapToBool(row[i]) expr = refer('mapToBool').call([expr]); @@ -886,7 +893,7 @@ class OrmGenerator extends GeneratorForAnnotation { if (fType is InterfaceType && fType.element is EnumElement) { var asInt = value.asA(refer('int')); - var t = convertTypeReference(fType); + var t = convertTypeReference(fType, ignoreNullabilityCheck: true); value = t.property('values').index(asInt); } else if (const TypeChecker.fromRuntime(List) .isAssignableFromType(fType)) { diff --git a/packages/orm/angel_orm_test/lib/src/models/has_car.dart b/packages/orm/angel_orm_test/lib/src/models/has_car.dart index f9b1af00..7fef8b81 100644 --- a/packages/orm/angel_orm_test/lib/src/models/has_car.dart +++ b/packages/orm/angel_orm_test/lib/src/models/has_car.dart @@ -3,7 +3,6 @@ import 'package:angel3_orm/angel3_orm.dart'; import 'package:angel3_serialize/angel3_serialize.dart'; import 'package:optional/optional.dart'; -// import 'car.dart'; part 'has_car.g.dart'; // Map _carToMap(Car car) => car.toJson(); diff --git a/packages/serialize/angel_serialize_generator/lib/angel3_serialize_generator.dart b/packages/serialize/angel_serialize_generator/lib/angel3_serialize_generator.dart index 48beaa51..d9914a5b 100644 --- a/packages/serialize/angel_serialize_generator/lib/angel3_serialize_generator.dart +++ b/packages/serialize/angel_serialize_generator/lib/angel3_serialize_generator.dart @@ -38,13 +38,18 @@ Builder typescriptDefinitionBuilder(_) { } /// Converts a [DartType] to a [TypeReference]. -TypeReference convertTypeReference(DartType t, {bool forceNullable = false}) { +TypeReference convertTypeReference(DartType t, + {bool forceNullable = false, bool ignoreNullabilityCheck = false}) { return TypeReference((b) { b.symbol = t.element?.displayName; // Generate nullable type - if (t.nullabilitySuffix == NullabilitySuffix.question || forceNullable) { - b.isNullable = true; + if (ignoreNullabilityCheck) { + b.isNullable = false; + } else { + if (t.nullabilitySuffix == NullabilitySuffix.question || forceNullable) { + b.isNullable = true; + } } if (t is InterfaceType) {