Skip to content

Commit

Permalink
Fix type converter in the queryable_processor.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrikvdkaaden committed May 28, 2024
1 parent c950102 commit 92a8c13
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
6 changes: 5 additions & 1 deletion example/lib/task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ class Task {
id,
isRead ?? false,
message ?? 'empty',
timestamp!,
timestamp ??
Timestamp(
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
),
status,
type,
);
Expand Down
10 changes: 5 additions & 5 deletions example/lib/timestamp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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';
}
}
35 changes: 21 additions & 14 deletions floor_generator/lib/processor/queryable_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -90,13 +91,26 @@ abstract class QueryableProcessor<T extends Queryable> extends Processor<T> {
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,
Expand All @@ -108,18 +122,11 @@ abstract class QueryableProcessor<T extends Queryable> extends Processor<T> {
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) {
Expand Down

0 comments on commit 92a8c13

Please sign in to comment.