Skip to content

Commit

Permalink
Retain index ordering (#542)
Browse files Browse the repository at this point in the history
  • Loading branch information
mqus authored Apr 25, 2021
1 parent 3e8908f commit 0e0836a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
15 changes: 6 additions & 9 deletions floor_generator/lib/processor/entity_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,23 +163,20 @@ class EntityProcessor extends QueryableProcessor<Entity> {
// can't happen as Index.unique is non-nullable
if (unique == null) throw ArgumentError.notNull();

final values = indexObject
final indexColumnNames = indexObject
.getField(IndexField.value)
?.toListValue()
?.mapNotNull((valueObject) => valueObject.toStringValue())
.toList();

if (values == null || values.isEmpty) {
if (indexColumnNames == null || indexColumnNames.isEmpty) {
throw _processorError.missingIndexColumnName;
}

final indexColumnNames = fields
.map((field) => field.columnName)
.where((columnName) => values.any((value) => value == columnName))
.toList();

if (indexColumnNames.isEmpty) {
throw _processorError.noMatchingColumn(values);
for (final indexColumnName in indexColumnNames) {
if (!fields.any((field) => field.columnName == indexColumnName)) {
throw _processorError.noMatchingColumn(indexColumnName);
}
}

final name = indexObject.getField(IndexField.name)?.toStringValue() ??
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ class EntityProcessorError {
}

InvalidGenerationSourceError noMatchingColumn(
final List<String> columnNames,
final String columnName,
) {
return InvalidGenerationSourceError(
'No matching columns found for the given index. (${columnNames.join(', ')})',
'No matching column found for the given index. (`$columnName`)',
todo:
"Make sure to add a correct index column name like: Index(values: ['foo'])').",
element: _classElement,
Expand Down
8 changes: 4 additions & 4 deletions floor_generator/test/processor/entity_processor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void main() {

test('Process entity with index', () async {
final classElement = await createClassElement('''
@Entity(indices: [Index(name:'i1', unique: true, value:['id']),Index(unique: false, value:['id','name'])])
@Entity(indices: [Index(name:'i1', unique: true, value:['id']),Index(unique: false, value:['name', 'id'])])
class Person {
@primaryKey
final int id;
Expand All @@ -165,7 +165,7 @@ void main() {
const foreignKeys = <ForeignKey>[];
final indices = [
Index('i1', 'Person', true, ['id']),
Index('index_Person_id_name', 'Person', false, ['id', 'name'])
Index('index_Person_name_id', 'Person', false, ['name', 'id'])
];
const constructor = "Person(row['id'] as int, row['name'] as String)";
const valueMapping = "<String, Object?>{'id': item.id, 'name': item.name}";
Expand Down Expand Up @@ -630,7 +630,7 @@ void main() {
test('no matching index column', () async {
final classElement = await createClassElement('''
@Entity(
indices:[Index(value:['notAColumn'])]
indices:[Index(value:['id', 'notAColumn'])]
)
class Dog {
@primaryKey
Expand All @@ -649,7 +649,7 @@ void main() {
expect(
processor.process,
throwsInvalidGenerationSourceError(EntityProcessorError(classElement)
.noMatchingColumn(['notAColumn'])));
.noMatchingColumn('notAColumn')));
});
test('auto-increment not usable with `WITHOUT ROWID`', () async {
final classElement = await createClassElement('''
Expand Down

0 comments on commit 0e0836a

Please sign in to comment.