From 92a8c137dcef32970a0ec189e5d2a989e02e5ea7 Mon Sep 17 00:00:00 2001 From: hendrikvanderkaaden Date: Tue, 28 May 2024 10:20:52 +0200 Subject: [PATCH] Fix type converter in the queryable_processor.dart --- example/lib/task.dart | 6 +++- example/lib/timestamp.dart | 10 +++--- .../lib/processor/queryable_processor.dart | 35 +++++++++++-------- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/example/lib/task.dart b/example/lib/task.dart index c66741a2..b6042a1c 100644 --- a/example/lib/task.dart +++ b/example/lib/task.dart @@ -57,7 +57,11 @@ class Task { id, isRead ?? false, message ?? 'empty', - timestamp!, + timestamp ?? + Timestamp( + createdAt: DateTime.now(), + updatedAt: DateTime.now(), + ), status, type, ); diff --git a/example/lib/timestamp.dart b/example/lib/timestamp.dart index 8428e419..5b568c6f 100644 --- a/example/lib/timestamp.dart +++ b/example/lib/timestamp.dart @@ -14,10 +14,10 @@ class Timestamp { @override bool operator ==(Object other) => identical(this, other) || - other is Timestamp && - runtimeType == other.runtimeType && - createdAt == other.createdAt && - updatedAt == other.updatedAt; + other is Timestamp && + runtimeType == other.runtimeType && + createdAt == other.createdAt && + updatedAt == other.updatedAt; @override int get hashCode => createdAt.hashCode ^ updatedAt.hashCode; @@ -27,6 +27,6 @@ class Timestamp { final DateFormat formatter = DateFormat('yyyy-MM-dd'); final String formattedCreatedAt = formatter.format(createdAt); final String formattedUpdatedAt = formatter.format(updatedAt); - return 'Timestamp{createdAt: $formattedCreatedAt, updatedAt: $formattedUpdatedAt}'; + return 'createdAt: $formattedCreatedAt \n updatedAt: $formattedUpdatedAt'; } } diff --git a/floor_generator/lib/processor/queryable_processor.dart b/floor_generator/lib/processor/queryable_processor.dart index 0faf23a7..b498bb30 100644 --- a/floor_generator/lib/processor/queryable_processor.dart +++ b/floor_generator/lib/processor/queryable_processor.dart @@ -14,6 +14,7 @@ import 'package:floor_generator/value_object/field.dart'; import 'package:floor_generator/value_object/queryable.dart'; import 'package:floor_generator/value_object/type_converter.dart'; import 'package:meta/meta.dart'; +import 'package:source_gen/source_gen.dart'; import '../value_object/embed.dart'; @@ -90,13 +91,26 @@ abstract class QueryableProcessor extends Processor { final String prefix = '', }) { final parameterName = parameterElement.displayName; - final field = fields.firstWhereOrNull( - (field) => field.fieldElement.displayName == parameterName); + final field = + fields.firstWhereOrNull((field) => field.name == parameterName); if (field != null) { final databaseValue = "row['$prefix${field.columnName}']"; String parameterValue; - if (parameterElement.type.isDefaultSqlType || + + final typeConverter = [...queryableTypeConverters, field.typeConverter] + .whereNotNull() + .getClosestOrNull(parameterElement.type); + + if (typeConverter != null) { + final castedDatabaseValue = databaseValue.cast( + typeConverter.databaseType, + parameterElement, + ); + + parameterValue = + '_${typeConverter.name.decapitalize()}.decode($castedDatabaseValue)'; + } else if (parameterElement.type.isDefaultSqlType || parameterElement.type.isEnumType) { parameterValue = databaseValue.cast( parameterElement.type, @@ -108,18 +122,11 @@ abstract class QueryableProcessor extends Processor { field.embedConverter!.classElement, field.embedConverter!.fields, prefix: '$prefix$embedVar'); } else { - final typeConverter = [ - ...queryableTypeConverters, - field.typeConverter, - ].whereNotNull().getClosest(parameterElement.type); - - final castedDatabaseValue = databaseValue.cast( - typeConverter.databaseType, - parameterElement, + throw InvalidGenerationSourceError( + 'Column type is not supported for ${parameterElement.type}', + todo: + 'Either use a supported type https://pinchbv.github.io/floor/entities/#supported-types or supply a type converter.', ); - - parameterValue = - '_${typeConverter.name.decapitalize()}.decode($castedDatabaseValue)'; } if (parameterElement.isNamed) {