Skip to content

Commit

Permalink
Exclude static fields from entity mapping (#233)
Browse files Browse the repository at this point in the history
The static fields are excluded from the field list for mapping.

* Fix the call to _isNotHashCode

* Filter fields with extension function + add tests

Co-authored-by: Vitus <[email protected]>
  • Loading branch information
finger-software and vitusortner committed Jan 24, 2020
1 parent f2ba7f2 commit d9f1689
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
13 changes: 7 additions & 6 deletions floor_generator/lib/processor/entity_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,11 @@ class EntityProcessor extends Processor<Entity> {
@nonNull
List<Field> _getFields() {
return _classElement.fields
.where(_isNotHashCode)
.where((fieldElement) => fieldElement.shouldBeIncluded())
.map((field) => FieldProcessor(field).process())
.toList();
}

@nonNull
bool _isNotHashCode(final FieldElement fieldElement) {
return fieldElement.displayName != 'hashCode';
}

@nonNull
List<ForeignKey> _getForeignKeys() {
return _classElement
Expand Down Expand Up @@ -266,3 +261,9 @@ class EntityProcessor extends Processor<Entity> {
}
}
}

extension on FieldElement {
bool shouldBeIncluded() {
return !(isStatic || displayName == 'hashCode');
}
}
41 changes: 41 additions & 0 deletions floor_generator/test/processor/entity_processor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,47 @@ void main() {
expect(actual, equals(expected));
});

test('Ignore hashCode field', () async {
final classElement = await _createClassElement('''
@entity
class Person {
@primaryKey
final int id;
final String name;
Person(this.id, this.name);
@override
int get hashCode => id.hashCode ^ name.hashCode;
}
''');

final actual = EntityProcessor(classElement).process();

expect(actual.fields.length, equals(2));
});

test('Ignore static field', () async {
final classElement = await _createClassElement('''
@entity
class Person {
@primaryKey
final int id;
final String name;
Person(this.id, this.name);
static String foo = 'foo';
}
''');

final actual = EntityProcessor(classElement).process();

expect(actual.fields.length, equals(2));
});

group('foreign keys', () {
test('foreign key holds correct values', () async {
final classElements = await _createClassElements('''
Expand Down

0 comments on commit d9f1689

Please sign in to comment.