Skip to content

Commit

Permalink
Ignore field of entity b adding ignore annotation (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitusortner authored Jan 25, 2020
1 parent 29fee08 commit d048684
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions floor_annotation/lib/floor_annotation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
7 changes: 7 additions & 0 deletions floor_annotation/lib/src/ignore.dart
Original file line number Diff line number Diff line change
@@ -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();
4 changes: 3 additions & 1 deletion floor_generator/lib/processor/entity_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ class EntityProcessor extends Processor<Entity> {

extension on FieldElement {
bool shouldBeIncluded() {
return !(isStatic || displayName == 'hashCode');
final isIgnored = hasAnnotation(annotations.ignore.runtimeType);
final isHashCode = displayName == 'hashCode';
return !(isStatic || isHashCode || isIgnored);
}
}
49 changes: 49 additions & 0 deletions floor_generator/test/processor/entity_processor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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('''
Expand Down

0 comments on commit d048684

Please sign in to comment.