Skip to content

Commit

Permalink
Fix format issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrikvdkaaden committed May 24, 2024
1 parent 389f281 commit 989ca78
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 73 deletions.
7 changes: 3 additions & 4 deletions floor_generator/lib/misc/extension/embeds_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ extension EmbedsExtension on Iterable<Embed> {
/// Returns the [Embed] in the closest [TypeConverterScope] for
/// [dartType] or null
Embed? getClosestOrNull(DartType dartType) {
return toList()
.firstWhereOrNull(
(embed) => embed.classElement.name == dartType.toString());
return toList().firstWhereOrNull(
(embed) => embed.classElement.name == dartType.toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extension FieldElementExtension on FieldElement {
}

bool get isEmbedded {
return (type.element?.hasAnnotation(annotations.Embed) ?? false) && type.element is ClassElement;
return (type.element?.hasAnnotation(annotations.Embed) ?? false) &&
type.element is ClassElement;
}
}
}
6 changes: 4 additions & 2 deletions floor_generator/lib/processor/embed_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ class EmbedProcessor extends Processor<Embed> {
List<Field> _getFields() {
final fields = _classElement.fields
.where((fieldElement) => fieldElement.shouldBeIncluded())
.map((field) => FieldProcessor(field, typeConverters.getClosestOrNull(field.type), null).process())
.map((field) => FieldProcessor(
field, typeConverters.getClosestOrNull(field.type), null)
.process())
.toList();

return fields;
}
}
}
5 changes: 2 additions & 3 deletions floor_generator/lib/processor/entity_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,8 @@ class EntityProcessor extends QueryableProcessor<Entity> {
final Map<String, String> map = {};
_processFields(map, fields);

final keyValueList = map.entries
.map((entry) => "'${entry.key}': ${entry.value}")
.toList();
final keyValueList =
map.entries.map((entry) => "'${entry.key}': ${entry.value}").toList();

return '<String, Object?>{${keyValueList.join(', ')}}';
}
Expand Down
22 changes: 11 additions & 11 deletions floor_generator/lib/processor/field_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@ class FieldProcessor extends Processor<Field> {
}.whereNotNull().closestOrNull;

return Field(
_fieldElement,
name,
columnName,
isNullable,
_getSqlType(typeConverter, _embedConverter),
typeConverter,
_embedConverter
);
_fieldElement,
name,
columnName,
isNullable,
_getSqlType(typeConverter, _embedConverter),
typeConverter,
_embedConverter);
}

String _getColumnName(final String name) {
Expand All @@ -56,15 +55,16 @@ class FieldProcessor extends Processor<Field> {
: name;
}

String _getSqlType(final TypeConverter? typeConverter, final Embed? embedConverter) {
String _getSqlType(
final TypeConverter? typeConverter, final Embed? embedConverter) {
final type = _fieldElement.type;
if (typeConverter != null) {
return typeConverter.databaseType.asSqlType();
} else if (type.isDefaultSqlType || type.isEnumType) {
return type.asSqlType();
} else if (embedConverter != null) {
} else if (embedConverter != null) {
return '';
}else {
} else {
throw InvalidGenerationSourceError(
'Column type is not supported for $type.',
todo: 'Either make to use a supported type or supply a type converter.',
Expand Down
34 changes: 21 additions & 13 deletions floor_generator/lib/processor/queryable_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ abstract class QueryableProcessor<T extends Queryable> extends Processor<T> {
QueryableProcessor(
this.classElement,
final Set<TypeConverter> typeConverters,
this.embedConverters,
) : _queryableProcessorError = QueryableProcessorError(classElement),
this.embedConverters,
) : _queryableProcessorError = QueryableProcessorError(classElement),
queryableTypeConverters = typeConverters +
classElement.getTypeConverters(TypeConverterScope.queryable);

Expand All @@ -48,9 +48,13 @@ abstract class QueryableProcessor<T extends Queryable> extends Processor<T> {

return fields.map((field) {
if (field.isEmbedded) {
return FieldProcessor(field, null, embedConverters.getClosestOrNull(field.type)).process();
return FieldProcessor(
field, null, embedConverters.getClosestOrNull(field.type))
.process();
} else {
return FieldProcessor(field, queryableTypeConverters.getClosestOrNull(field.type), null).process();
return FieldProcessor(field,
queryableTypeConverters.getClosestOrNull(field.type), null)
.process();
}
}).toList();
}
Expand All @@ -60,7 +64,8 @@ abstract class QueryableProcessor<T extends Queryable> extends Processor<T> {
return _getConstructor(classElement, fields);
}

String _getConstructor(ClassElement classElement, final List<Field> fields, {String prefix = ''}) {
String _getConstructor(ClassElement classElement, final List<Field> fields,
{String prefix = ''}) {
final constructorParameters = classElement.constructors
.firstWhereOrNull((element) => element.isPublic && !element.isFactory)
?.parameters;
Expand All @@ -81,12 +86,12 @@ abstract class QueryableProcessor<T extends Queryable> extends Processor<T> {
/// Returns `null` whenever field is @ignored
String? _getParameterValue(
final ParameterElement parameterElement,
final List<Field> fields, {
final String prefix = '',
}
) {
final List<Field> fields, {
final String prefix = '',
}) {
final parameterName = parameterElement.displayName;
final field = fields.firstWhereOrNull((field) => field.fieldElement.displayName == parameterName);
final field = fields.firstWhereOrNull(
(field) => field.fieldElement.displayName == parameterName);
if (field != null) {
final databaseValue = "row['$prefix${field.columnName}']";

Expand All @@ -98,7 +103,9 @@ abstract class QueryableProcessor<T extends Queryable> extends Processor<T> {
);
} else if (field.embedConverter != null) {
final embedVar = field.columnName.isEmpty ? '' : '${field.columnName}_';
parameterValue = _getConstructor(field.embedConverter!.classElement, field.embedConverter!.fields, prefix: '$prefix$embedVar');
parameterValue = _getConstructor(
field.embedConverter!.classElement, field.embedConverter!.fields,
prefix: '$prefix$embedVar');
} else {
final typeConverter = [
...queryableTypeConverters,
Expand All @@ -110,14 +117,15 @@ abstract class QueryableProcessor<T extends Queryable> extends Processor<T> {
parameterElement,
);

parameterValue = '_${typeConverter.name.decapitalize()}.decode($castedDatabaseValue)';
parameterValue =
'_${typeConverter.name.decapitalize()}.decode($castedDatabaseValue)';
}

if (parameterElement.isNamed) {
return '$parameterName: $parameterValue';
}
return parameterValue; // also covers positional parameter
}
}
return null;
}
}
8 changes: 4 additions & 4 deletions floor_generator/lib/value_object/embed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class Embed {
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Embed &&
runtimeType == other.runtimeType &&
const ListEquality<Field>().equals(fields, other.fields);
other is Embed &&
runtimeType == other.runtimeType &&
const ListEquality<Field>().equals(fields, other.fields);

@override
int get hashCode => classElement.hashCode ^ fields.hashCode;
Expand All @@ -22,4 +22,4 @@ class Embed {
String toString() {
return 'Embed{classElement: $classElement, fields: $fields}';
}
}
}
19 changes: 11 additions & 8 deletions floor_generator/lib/value_object/field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Field {
String getDatabaseDefinition(final bool autoGenerate) {
if (embedConverter != null) {
throw InvalidGenerationSourceError(
'You ',
'You ',
todo: 'Either make to use a supported type or supply a type converter.',
element: fieldElement,
);
Expand All @@ -47,13 +47,16 @@ class Field {

Field copyWith({
String columnNamePrefix = '',
}) => Field(fieldElement,
name,
'$columnNamePrefix$columnName',
isNullable,
sqlType,
typeConverter, embedConverter,
);
}) =>
Field(
fieldElement,
name,
'$columnNamePrefix$columnName',
isNullable,
sqlType,
typeConverter,
embedConverter,
);

@override
bool operator ==(Object other) =>
Expand Down
30 changes: 20 additions & 10 deletions floor_generator/test/processor/entity_processor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ void main() {

const name = 'Person';
final fields = classElement.fields
.map((fieldElement) => FieldProcessor(fieldElement, null, null).process())
.map((fieldElement) =>
FieldProcessor(fieldElement, null, null).process())
.toList();
final primaryKey = PrimaryKey([fields[0]], false);
const foreignKeys = <ForeignKey>[];
Expand Down Expand Up @@ -81,7 +82,8 @@ void main() {

const name = 'Person';
final fields = classElement.fields
.map((fieldElement) => FieldProcessor(fieldElement, null, null).process())
.map((fieldElement) =>
FieldProcessor(fieldElement, null, null).process())
.toList();
final primaryKey = PrimaryKey([fields[0]], false);
const foreignKeys = <ForeignKey>[];
Expand Down Expand Up @@ -119,7 +121,8 @@ void main() {

const name = 'Person';
final fields = classElement.fields
.map((fieldElement) => FieldProcessor(fieldElement, null, null).process())
.map((fieldElement) =>
FieldProcessor(fieldElement, null, null).process())
.toList();
final primaryKey = PrimaryKey(fields, false);
const foreignKeys = <ForeignKey>[];
Expand Down Expand Up @@ -158,7 +161,8 @@ void main() {

const name = 'Person';
final fields = classElement.fields
.map((fieldElement) => FieldProcessor(fieldElement, null, null).process())
.map((fieldElement) =>
FieldProcessor(fieldElement, null, null).process())
.toList();
final primaryKey = PrimaryKey(fields.sublist(0, 1), false);
const foreignKeys = <ForeignKey>[];
Expand Down Expand Up @@ -206,7 +210,8 @@ void main() {

const name = 'Person';
final fields = classElement.fields
.map((fieldElement) => FieldProcessor(fieldElement, null, null).process())
.map((fieldElement) =>
FieldProcessor(fieldElement, null, null).process())
.toList();
final primaryKey = PrimaryKey([fields[0]], false);
const foreignKeys = <ForeignKey>[];
Expand Down Expand Up @@ -346,7 +351,8 @@ void main() {

const name = 'Person';
final fields = classElement.fields
.map((fieldElement) => FieldProcessor(fieldElement, null, null).process())
.map((fieldElement) =>
FieldProcessor(fieldElement, null, null).process())
.toList();
final primaryKey = PrimaryKey([fields[0]], false);
const foreignKeys = <ForeignKey>[];
Expand Down Expand Up @@ -381,7 +387,8 @@ void main() {
}
''');

final actual = EntityProcessor(classElement, {}, {}).process().valueMapping;
final actual =
EntityProcessor(classElement, {}, {}).process().valueMapping;

const expected = '<String, Object?>{'
"'id': item.id, "
Expand All @@ -403,7 +410,8 @@ void main() {
}
''');

final actual = EntityProcessor(classElement, {}, {}).process().valueMapping;
final actual =
EntityProcessor(classElement, {}, {}).process().valueMapping;

const expected = '<String, Object?>{'
"'id': item.id, "
Expand All @@ -428,7 +436,8 @@ void main() {
}
''');

final actual = EntityProcessor(classElement, {}, {}).process().valueMapping;
final actual =
EntityProcessor(classElement, {}, {}).process().valueMapping;

const expected = '<String, Object?>{'
"'id': item.id, "
Expand All @@ -453,7 +462,8 @@ void main() {
}
''');

final actual = EntityProcessor(classElement, {}, {}).process().valueMapping;
final actual =
EntityProcessor(classElement, {}, {}).process().valueMapping;

const expected = '<String, Object?>{'
"'id': item.id, "
Expand Down
12 changes: 8 additions & 4 deletions floor_generator/test/processor/queryable_processor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ void main() {
final actual = TestProcessor(classElement).process();

final fields = classElement.fields
.map((fieldElement) => FieldProcessor(fieldElement, null, null).process())
.map((fieldElement) =>
FieldProcessor(fieldElement, null, null).process())
.toList();
const constructor = "Person(row['id'] as int, row['name'] as String)";
final expected = TestQueryable(
Expand Down Expand Up @@ -57,7 +58,8 @@ void main() {

final actual = TestProcessor(classElement, {typeConverter}).process();

final idField = FieldProcessor(classElement.fields[0], null, null).process();
final idField =
FieldProcessor(classElement.fields[0], null, null).process();
final dateTimeField =
FieldProcessor(classElement.fields[1], typeConverter, null).process();
final fields = [idField, dateTimeField];
Expand Down Expand Up @@ -103,7 +105,8 @@ void main() {
await intDartType,
TypeConverterScope.queryable,
);
final idField = FieldProcessor(classElement.fields[0], null, null).process();
final idField =
FieldProcessor(classElement.fields[0], null, null).process();
final dateTimeField =
FieldProcessor(classElement.fields[1], typeConverter, null).process();
final fields = [idField, dateTimeField];
Expand Down Expand Up @@ -157,7 +160,8 @@ void main() {
await intDartType,
TypeConverterScope.queryable,
);
final idField = FieldProcessor(classElement.fields[0], null, null).process();
final idField =
FieldProcessor(classElement.fields[0], null, null).process();
final dateTimeField =
FieldProcessor(classElement.fields[1], typeConverter, null).process();
final fields = [idField, dateTimeField];
Expand Down
9 changes: 6 additions & 3 deletions floor_generator/test/processor/view_processor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ void main() {

const name = 'Person';
final fields = classElement.fields
.map((fieldElement) => FieldProcessor(fieldElement, null, null).process())
.map((fieldElement) =>
FieldProcessor(fieldElement, null, null).process())
.toList();
const query = 'SELECT * from otherentity';
const constructor = "Person(row['id'] as int, row['name'] as String)";
Expand Down Expand Up @@ -52,7 +53,8 @@ void main() {

const name = 'Person';
final fields = classElement.fields
.map((fieldElement) => FieldProcessor(fieldElement, null, null).process())
.map((fieldElement) =>
FieldProcessor(fieldElement, null, null).process())
.toList();
const query =
'WITH subquery as (SELECT * from otherentity) SELECT subquery.*';
Expand Down Expand Up @@ -179,7 +181,8 @@ void main() {

const name = 'personview';
final fields = classElement.fields
.map((fieldElement) => FieldProcessor(fieldElement, null, null).process())
.map((fieldElement) =>
FieldProcessor(fieldElement, null, null).process())
.toList();
const query = 'SELECT * from otherentity';
const constructor = "Person(row['id'] as int, row['name'] as String)";
Expand Down
Loading

0 comments on commit 989ca78

Please sign in to comment.