From d0486849b0fdb0efc76882c1cbb4bee986eec147 Mon Sep 17 00:00:00 2001 From: Vitus Date: Sat, 25 Jan 2020 22:46:21 +0100 Subject: [PATCH] Ignore field of entity b adding ignore annotation (#248) --- floor_annotation/lib/floor_annotation.dart | 1 + floor_annotation/lib/src/ignore.dart | 7 +++ .../lib/processor/entity_processor.dart | 4 +- .../test/processor/entity_processor_test.dart | 49 +++++++++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 floor_annotation/lib/src/ignore.dart diff --git a/floor_annotation/lib/floor_annotation.dart b/floor_annotation/lib/floor_annotation.dart index 760a38d9..1986d9a3 100644 --- a/floor_annotation/lib/floor_annotation.dart +++ b/floor_annotation/lib/floor_annotation.dart @@ -6,6 +6,7 @@ export 'src/database.dart'; export 'src/delete.dart'; export 'src/entity.dart'; export 'src/foreign_key.dart'; +export 'src/ignore.dart'; export 'src/index.dart'; export 'src/insert.dart'; export 'src/on_conflict_strategy.dart'; diff --git a/floor_annotation/lib/src/ignore.dart b/floor_annotation/lib/src/ignore.dart new file mode 100644 index 00000000..b806801e --- /dev/null +++ b/floor_annotation/lib/src/ignore.dart @@ -0,0 +1,7 @@ +class _Ignore { + const _Ignore(); +} + +/// Ignores the marked element from Floor's processing logic. +/// It can only be applied to entity's fields. +const ignore = _Ignore(); diff --git a/floor_generator/lib/processor/entity_processor.dart b/floor_generator/lib/processor/entity_processor.dart index ce2016d3..42bff6b9 100644 --- a/floor_generator/lib/processor/entity_processor.dart +++ b/floor_generator/lib/processor/entity_processor.dart @@ -274,6 +274,8 @@ class EntityProcessor extends Processor { extension on FieldElement { bool shouldBeIncluded() { - return !(isStatic || displayName == 'hashCode'); + final isIgnored = hasAnnotation(annotations.ignore.runtimeType); + final isHashCode = displayName == 'hashCode'; + return !(isStatic || isHashCode || isIgnored); } } diff --git a/floor_generator/test/processor/entity_processor_test.dart b/floor_generator/test/processor/entity_processor_test.dart index 9019566b..8c778b4e 100644 --- a/floor_generator/test/processor/entity_processor_test.dart +++ b/floor_generator/test/processor/entity_processor_test.dart @@ -229,6 +229,55 @@ void main() { }); }); + group('@Ignore', () { + test('ignore field not present in constructor', () async { + final classElement = await _createClassElement(''' + @entity + class Person { + @primaryKey + final int id; + + final String name; + + @ignore + String foo; + + Person(this.id, this.name); + } + '''); + + final actual = EntityProcessor(classElement) + .process() + .fields + .map((field) => field.name); + + const expected = 'foo'; + expect(actual, isNot(contains(expected))); + }); + + test('ignore field present in constructor', () async { + final classElement = await _createClassElement(''' + @entity + class Person { + @primaryKey + final int id; + + final String name; + + @ignore + String foo; + + Person(this.id, this.name, [this.foo = 'foo']); + } + '''); + + final actual = EntityProcessor(classElement).process().constructor; + + const expected = "Person(row['id'] as int, row['name'] as String)"; + expect(actual, equals(expected)); + }); + }); + group('foreign keys', () { test('foreign key holds correct values', () async { final classElements = await _createClassElements('''