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

Exclude static fields from entity mapping #233

Merged
merged 5 commits into from
Jan 24, 2020
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
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