Skip to content

Commit

Permalink
Merge pull request #2581 from nikitadol/develop
Browse files Browse the repository at this point in the history
fix code generation for `enum` with generics
  • Loading branch information
simolus3 authored Aug 27, 2023
2 parents 84e2511 + f408991 commit 18b11d7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
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

0 comments on commit 18b11d7

Please sign in to comment.