From 31cac244faa0d1fab03682a4e8309eaa0c084aac Mon Sep 17 00:00:00 2001 From: finger-software <41995333+finger-software@users.noreply.github.com> Date: Sat, 18 Jan 2020 10:29:44 +1100 Subject: [PATCH 1/3] Fix issue #231 The static fields are excluded from the field list for mapping. --- floor_generator/lib/processor/entity_processor.dart | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/floor_generator/lib/processor/entity_processor.dart b/floor_generator/lib/processor/entity_processor.dart index 1f80fb4f..ad2b7426 100644 --- a/floor_generator/lib/processor/entity_processor.dart +++ b/floor_generator/lib/processor/entity_processor.dart @@ -55,7 +55,7 @@ class EntityProcessor extends Processor { @nonNull List _getFields() { return _classElement.fields - .where(_isNotHashCode) + .where(_shouldIncludeField) .map((field) => FieldProcessor(field).process()) .toList(); } @@ -65,6 +65,11 @@ class EntityProcessor extends Processor { return fieldElement.displayName != 'hashCode'; } + @nonNull + bool _shouldIncludeField(final FieldElement fieldElement) { + return !fieldElement.isStatic && !_isNotHashCode(fieldElement); + } + @nonNull List _getForeignKeys() { return _entityTypeChecker From ef0f9ea16a7296ab305908d9ad61970df0edd5c8 Mon Sep 17 00:00:00 2001 From: finger-software <41995333+finger-software@users.noreply.github.com> Date: Sat, 18 Jan 2020 11:49:53 +1100 Subject: [PATCH 2/3] Fix the call to _isNotHashCode --- floor_generator/lib/processor/entity_processor.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/floor_generator/lib/processor/entity_processor.dart b/floor_generator/lib/processor/entity_processor.dart index ad2b7426..8cf5e713 100644 --- a/floor_generator/lib/processor/entity_processor.dart +++ b/floor_generator/lib/processor/entity_processor.dart @@ -67,7 +67,7 @@ class EntityProcessor extends Processor { @nonNull bool _shouldIncludeField(final FieldElement fieldElement) { - return !fieldElement.isStatic && !_isNotHashCode(fieldElement); + return !fieldElement.isStatic && _isNotHashCode(fieldElement); } @nonNull From 54fafa017cb67c19e68ff0898ba55acbedaa1bf9 Mon Sep 17 00:00:00 2001 From: vitusortner Date: Fri, 24 Jan 2020 15:30:23 +0100 Subject: [PATCH 3/3] Filter fields with extension function + add tests --- .../lib/processor/entity_processor.dart | 18 ++++---- .../test/processor/entity_processor_test.dart | 41 +++++++++++++++++++ 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/floor_generator/lib/processor/entity_processor.dart b/floor_generator/lib/processor/entity_processor.dart index 2b64e34b..4fd0090d 100644 --- a/floor_generator/lib/processor/entity_processor.dart +++ b/floor_generator/lib/processor/entity_processor.dart @@ -53,21 +53,11 @@ class EntityProcessor extends Processor { @nonNull List _getFields() { return _classElement.fields - .where(_shouldIncludeField) + .where((fieldElement) => fieldElement.shouldBeIncluded()) .map((field) => FieldProcessor(field).process()) .toList(); } - @nonNull - bool _isNotHashCode(final FieldElement fieldElement) { - return fieldElement.displayName != 'hashCode'; - } - - @nonNull - bool _shouldIncludeField(final FieldElement fieldElement) { - return !fieldElement.isStatic && _isNotHashCode(fieldElement); - } - @nonNull List _getForeignKeys() { return _classElement @@ -271,3 +261,9 @@ class EntityProcessor extends Processor { } } } + +extension on FieldElement { + bool shouldBeIncluded() { + return !(isStatic || displayName == 'hashCode'); + } +} diff --git a/floor_generator/test/processor/entity_processor_test.dart b/floor_generator/test/processor/entity_processor_test.dart index 510ed7c3..d51794e4 100644 --- a/floor_generator/test/processor/entity_processor_test.dart +++ b/floor_generator/test/processor/entity_processor_test.dart @@ -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('''