Skip to content

Commit

Permalink
Merge pull request #1 from Ghomi-Dev/transient-annotation-implementation
Browse files Browse the repository at this point in the history
transient annotation is implemented
  • Loading branch information
Ghomi-Dev authored Nov 7, 2019
2 parents 1ce95e1 + 45acb13 commit fddfd76
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 63 deletions.
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

0 comments on commit fddfd76

Please sign in to comment.