Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix code generation for enum with generics #2581

Merged
merged 2 commits into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions drift_dev/lib/src/analysis/resolver/shared/dart_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,9 @@ AppliedTypeConverter readEnumConverter(
}
builder
..addText('<')
..addDartType(dartEnumType)
..addTopLevelElement(dartEnumType.element!)
..addText('>(')
..addDartType(dartEnumType)
..addTopLevelElement(dartEnumType.element!)
..addText('.values)');
});

Expand Down
5 changes: 5 additions & 0 deletions drift_dev/test/analysis/resolver/dart/enum_columns_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ void main() {
apple, orange, banana
}

enum FruitsWithGeneric<T> {
apple, orange, banana
}

class NotAnEnum {}

class ValidUsage extends Table {
IntColumn get intFruit => intEnum<Fruits>()();
IntColumn get intFruitsWithGeneric => intEnum<FruitsWithGeneric>()();
TextColumn get textFruit => textEnum<Fruits>()();
}

Expand Down
21 changes: 21 additions & 0 deletions drift_dev/test/analysis/resolver/drift/table_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ CREATE TABLE b (

CREATE TABLE foo (
fruitIndex ENUM(Fruits) NOT NULL,
fruitWithGenericIndex ENUM(FruitsWithGeneric) NOT NULL,
fruitName ENUMNAME(Fruits) NOT NULL,
anotherIndex ENUM(DoesNotExist) NOT NULL,
anotherName ENUMNAME(DoesNotExist) NOT NULL
Expand All @@ -98,6 +99,10 @@ CREATE TABLE b (
enum Fruits {
apple, orange, banana
}

enum FruitsWithGeneric<T> {
apple, orange, banana
}
''',
});

Expand All @@ -118,6 +123,22 @@ CREATE TABLE b (
.having((e) => e.dartType.getDisplayString(withNullability: true),
'dartType', 'Fruits'),
);

final withGenericIndexColumn = table.columns
.singleWhere((c) => c.nameInSql == 'fruitWithGenericIndex');
expect(withGenericIndexColumn.sqlType, DriftSqlType.int);
expect(
withGenericIndexColumn.typeConverter,
isA<AppliedTypeConverter>()
.having(
(e) => e.expression.toString(),
'expression',
contains('EnumIndexConverter<FruitsWithGeneric>'),
)
.having(
(e) => e.dartType.element!.name, 'dartType', 'FruitsWithGeneric'),
);

final nameColumn =
table.columns.singleWhere((c) => c.nameInSql == 'fruitName');

Expand Down
Loading