Fixed Enum null check warnings

This commit is contained in:
thomashii@dukefirehawk.com 2022-12-03 11:14:24 +08:00
parent 56e0e7a05a
commit cf8d9814e8
3 changed files with 18 additions and 7 deletions

View file

@ -249,7 +249,7 @@ class OrmGenerator extends GeneratorForAnnotation<Orm> {
var args = <String, Expression>{};
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<Orm> {
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<Orm> {
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)) {

View file

@ -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();

View file

@ -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) {