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

transient annotation is implemented #1

Merged
merged 1 commit into from
Nov 7, 2019
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
5 changes: 4 additions & 1 deletion example/lib/task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ class Task {

final String message;

Task(this.id, this.message);
@transient
String description;

Task(this.id, this.message, [this.description='']);

@override
bool operator ==(Object other) =>
Expand Down
1 change: 1 addition & 0 deletions floor_annotation/lib/floor_annotation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export 'src/on_conflict_strategy.dart';
export 'src/primary_key.dart';
export 'src/query.dart';
export 'src/transaction.dart';
export 'src/transient.dart';
export 'src/update.dart';
7 changes: 7 additions & 0 deletions floor_annotation/lib/src/transient.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// Marks a field in an [Entity] as the transient.
class Transient {
const Transient();
}

/// Marks a field in an [Entity] as the transient.
const transient = Transient();
22 changes: 14 additions & 8 deletions floor_generator/lib/processor/entity_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ class EntityProcessor extends Processor<Entity> {
@override
Entity process() {
final name = _getName();
final fields = _getFields();
final allFieldsButTransients = _getAllButTransientsFields();

return Entity(
_classElement,
name,
fields,
_getPrimaryKey(fields),
allFieldsButTransients,
_getPrimaryKey(allFieldsButTransients),
_getForeignKeys(),
_getIndices(fields, name),
_getConstructor(fields),
_getIndices(allFieldsButTransients, name),
_getConstructor(allFieldsButTransients),
);
}

Expand All @@ -53,9 +53,9 @@ class EntityProcessor extends Processor<Entity> {
}

@nonNull
List<Field> _getFields() {
List<Field> _getAllButTransientsFields() {
return _classElement.fields
.where(_isNotHashCode)
.where(_isNotHashCode).where(_isNotTransient)
.map((field) => FieldProcessor(field).process())
.toList();
}
Expand All @@ -65,6 +65,12 @@ class EntityProcessor extends Processor<Entity> {
return fieldElement.displayName != 'hashCode';
}

@nonNull
bool _isNotTransient(final FieldElement fieldElement) {
return !typeChecker(annotations.Transient)
.hasAnnotationOfExact(fieldElement);
}

@nonNull
List<ForeignKey> _getForeignKeys() {
return _entityTypeChecker
Expand Down Expand Up @@ -231,7 +237,7 @@ class EntityProcessor extends Processor<Entity> {
@nonNull
String _getConstructor(final List<Field> fields) {
final columnNames = fields.map((field) => field.columnName).toList();
final constructorParameters = _classElement.constructors.first.parameters;
final constructorParameters = _classElement.constructors.first.parameters. where((f)=> columnNames.contains(f.name)).toList();

final parameterValues = <String>[];

Expand Down
21 changes: 21 additions & 0 deletions floor_generator/lib/value_object/transient.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:collection/collection.dart';
import 'package:floor_generator/value_object/field.dart';

/// Transient representation of a field in an Entity
class Transient {
final List<Field> fields;

Transient(this.fields);

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Transient &&
runtimeType == other.runtimeType &&
const ListEquality<Field>().equals(fields, other.fields);

@override
String toString() {
return 'Transient{fields: $fields}';
}
}
39 changes: 39 additions & 0 deletions floor_generator/test/processor/entity_processor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,45 @@ void main() {
expect(actual, equals(expected));
});
});

test('Should entity with transient field be not equals to floor processed entity', () async {
final classElement = await _createClassElement('''
@entity
class Person {
@primaryKey
final int id;

final String name;

@transient
String nickName;

Person(this.id, this.name, [this.nickName='']);
}
''');

final actual = EntityProcessor(classElement).process();

const name = 'Person';
final fields = classElement.fields
.map((fieldElement) => FieldProcessor(fieldElement).process())
.toList();
final primaryKey = PrimaryKey([fields[0]], false);
const foreignKeys = <ForeignKey>[];
const indices = <Index>[];
const constructor = "Person(row['id'] as int, row['name'] as String)";
final expected = Entity(
classElement,
name,
fields,
primaryKey,
foreignKeys,
indices,
constructor,
);

expect(actual.fields, isNot(equals(expected.fields)));
});
}

Future<ClassElement> _createClassElement(final String clazz) async {
Expand Down
Loading