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

Ignore fields of entity by adding ignore annotation #248

Merged
merged 1 commit into from
Jan 25, 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
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