Skip to content

Commit

Permalink
(refactor) Remove duplicated and unused code in insert/update/delete …
Browse files Browse the repository at this point in the history
…method and processor
  • Loading branch information
mqus committed Mar 15, 2021
1 parent c92b156 commit 2d22690
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 168 deletions.
29 changes: 25 additions & 4 deletions floor_generator/lib/misc/change_method_processor_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:floor_generator/misc/type_utils.dart';
import 'package:floor_generator/processor/error/change_method_processor_error.dart';
import 'package:floor_generator/value_object/entity.dart';
import 'package:source_gen/source_gen.dart';

Expand All @@ -10,11 +11,14 @@ class ChangeMethodProcessorHelper {
final MethodElement _methodElement;
final List<Entity> _entities;

const ChangeMethodProcessorHelper(
final ChangeMethodProcessorError processorError;

ChangeMethodProcessorHelper(
final MethodElement methodElement,
final List<Entity> entities,
) : _methodElement = methodElement,
_entities = entities;
_entities = entities,
processorError = ChangeMethodProcessorError(methodElement);

ParameterElement getParameterElement() {
final parameters = _methodElement.parameters;
Expand All @@ -32,7 +36,7 @@ class ChangeMethodProcessorHelper {
return parameters.first;
}

DartType getFlattenedParameterType(
DartType _getFlattenedParameterType(
final ParameterElement parameterElement,
) {
final changesMultipleItems = parameterElement.type.isDartCoreList;
Expand All @@ -42,7 +46,8 @@ class ChangeMethodProcessorHelper {
: parameterElement.type;
}

Entity getEntity(final DartType flattenedParameterType) {
Entity getEntity(final ParameterElement parameterElement) {
final flattenedParameterType = _getFlattenedParameterType(parameterElement);
return _entities.firstWhere(
(entity) =>
entity.classElement.displayName ==
Expand All @@ -51,4 +56,20 @@ class ChangeMethodProcessorHelper {
'You are trying to change an object which is not an entity.',
element: _methodElement));
}

DartType getFlattenedReturnType() {
return _methodElement.library.typeSystem.flatten(_methodElement.returnType);
}

void assertMethodReturnsFuture(String methodType) {
if (!_methodElement.returnType.isDartAsyncFuture) {
throw processorError.changeMustReturnIntOrVoidFuture(methodType);
}
}

void assertMethodReturnsIntOrVoid(String methodType, final DartType flat) {
if (!flat.isVoid && !flat.isDartCoreInt) {
throw processorError.changeMustReturnIntOrVoidFuture(methodType);
}
}
}
54 changes: 7 additions & 47 deletions floor_generator/lib/processor/deletion_method_processor.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// ignore_for_file: import_of_legacy_library_into_null_safe
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:floor_generator/misc/change_method_processor_helper.dart';
import 'package:floor_generator/processor/processor.dart';
import 'package:floor_generator/value_object/deletion_method.dart';
import 'package:floor_generator/value_object/entity.dart';
import 'package:source_gen/source_gen.dart';

class DeletionMethodProcessor implements Processor<DeletionMethod> {
final MethodElement _methodElement;
Expand All @@ -21,59 +19,21 @@ class DeletionMethodProcessor implements Processor<DeletionMethod> {

@override
DeletionMethod process() {
final name = _methodElement.name;
final returnType = _methodElement.returnType;
_helper.assertMethodReturnsFuture('Deletion');

_assertMethodReturnsFuture(returnType);
final flattenedReturnType = _helper.getFlattenedReturnType();

final flattenedReturnType = _getFlattenedReturnType(returnType);
_assertMethodReturnsNoList(flattenedReturnType);

final returnsVoid = flattenedReturnType.isVoid;
final returnsInt = flattenedReturnType.isDartCoreInt;

if (!returnsVoid && !returnsInt) {
throw InvalidGenerationSourceError(
'Deletion methods have to return a Future of either void or int.',
element: _methodElement,
);
}
_helper.assertMethodReturnsIntOrVoid('Deletion', flattenedReturnType);

final parameterElement = _helper.getParameterElement();
final flattenedParameterType =
_helper.getFlattenedParameterType(parameterElement);

final entity = _helper.getEntity(flattenedParameterType);
final entity = _helper.getEntity(parameterElement);

return DeletionMethod(
_methodElement,
name,
returnType,
flattenedReturnType,
_methodElement.name,
_methodElement.returnType,
flattenedReturnType.isVoid,
parameterElement,
entity,
);
}

DartType _getFlattenedReturnType(final DartType returnType) {
return _methodElement.library.typeSystem.flatten(returnType);
}

void _assertMethodReturnsNoList(final DartType flattenedReturnType) {
if (flattenedReturnType.isDartCoreList) {
throw InvalidGenerationSourceError(
'Deletion methods have to return a Future of either void or int but not a list.',
element: _methodElement,
);
}
}

void _assertMethodReturnsFuture(final DartType returnType) {
if (!returnType.isDartAsyncFuture) {
throw InvalidGenerationSourceError(
'Deletion methods have to return a Future.',
element: _methodElement,
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:analyzer/dart/element/element.dart';
import 'package:floor_generator/processor/error/processor_error.dart';

class ChangeMethodProcessorError {
final MethodElement _methodElement;

const ChangeMethodProcessorError(final MethodElement methodElement)
: _methodElement = methodElement;

ProcessorError changeMustReturnIntOrVoidFuture(String type) {
return ProcessorError(
message: '$type methods have to return a Future of either void or int.',
todo:
'Give the method a return type of either `Future<void>` or `Future<int>`.',
element: _methodElement,
);
}

ProcessorError get insertMustReturnIntOrVoidFutureList {
return ProcessorError(
message:
'Insertion methods have to return a Future of either void or int or List<int>.',
todo:
'Give the method a return type of either `Future<void>`, `Future<int>` or `Future<List<int>>`.',
element: _methodElement,
);
}
}
68 changes: 23 additions & 45 deletions floor_generator/lib/processor/insertion_method_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,57 +26,24 @@ class InsertionMethodProcessor implements Processor<InsertionMethod> {

@override
InsertionMethod process() {
final name = _methodElement.name;
final returnType = _methodElement.returnType;
_assertMethodReturnsFuture();

_assertMethodReturnsFuture(returnType);

final returnsList = _getReturnsList(returnType);
final flattenedReturnType =
_getFlattenedReturnType(returnType, returnsList);

final returnsVoid = flattenedReturnType.isVoid;
final returnsInt = flattenedReturnType.isDartCoreInt;
final returnsIntList = returnsList && flattenedReturnType.isDartCoreInt;

if (!returnsVoid && !returnsIntList && !returnsInt) {
throw InvalidGenerationSourceError(
'Insertion methods have to return a Future of either void, int or List<int>.',
element: _methodElement,
);
}
final flattenedReturnType = _getAndCheckFlatReturnType();

final parameterElement = _helper.getParameterElement();
final flattenedParameterType =
_helper.getFlattenedParameterType(parameterElement);

final entity = _helper.getEntity(flattenedParameterType);
final entity = _helper.getEntity(parameterElement);
final onConflict = _getOnConflictStrategy();

return InsertionMethod(
_methodElement,
name,
returnType,
flattenedReturnType,
_methodElement.name,
_methodElement.returnType,
flattenedReturnType.isVoid,
parameterElement,
entity,
onConflict,
);
}

bool _getReturnsList(final DartType returnType) {
final type = _methodElement.library.typeSystem.flatten(returnType);
return type.isDartCoreList;
}

DartType _getFlattenedReturnType(
final DartType returnType,
final bool returnsList,
) {
final type = _methodElement.library.typeSystem.flatten(returnType);
return returnsList ? type.flatten() : type;
}

String _getOnConflictStrategy() {
final onConflictStrategy = _methodElement
.getAnnotation(annotations.Insert)
Expand All @@ -93,12 +60,23 @@ class InsertionMethodProcessor implements Processor<InsertionMethod> {
}
}

void _assertMethodReturnsFuture(final DartType returnType) {
if (!returnType.isDartAsyncFuture) {
throw InvalidGenerationSourceError(
'Insertion methods have to return a Future.',
element: _methodElement,
);
void _assertMethodReturnsFuture() {
if (!_methodElement.returnType.isDartAsyncFuture) {
throw _helper.processorError.insertMustReturnIntOrVoidFutureList;
}
}

DartType _getAndCheckFlatReturnType() {
DartType flattened = _helper.getFlattenedReturnType();
if (flattened.isVoid || flattened.isDartCoreInt) {
return flattened;
}
if (flattened.isDartCoreList) {
flattened = flattened.flatten();
if (flattened.isDartCoreInt) {
return flattened;
}
}
throw _helper.processorError.insertMustReturnIntOrVoidFutureList;
}
}
52 changes: 7 additions & 45 deletions floor_generator/lib/processor/update_method_processor.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// ignore_for_file: import_of_legacy_library_into_null_safe
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:floor_annotation/floor_annotation.dart' as annotations
show Update, OnConflictStrategy;
import 'package:floor_generator/misc/change_method_processor_helper.dart';
Expand All @@ -26,35 +25,20 @@ class UpdateMethodProcessor implements Processor<UpdateMethod> {

@override
UpdateMethod process() {
final name = _methodElement.name;
final returnType = _methodElement.returnType;
_helper.assertMethodReturnsFuture('Update');

_assertMethodReturnsFuture(returnType);
final flattenedReturnType = _helper.getFlattenedReturnType();

final flattenedReturnType = _getFlattenedReturnType(returnType);
_assertMethodReturnsNoList(flattenedReturnType);

final returnsInt = flattenedReturnType.isDartCoreInt;
final returnsVoid = flattenedReturnType.isVoid;

if (!returnsInt && !returnsVoid) {
throw InvalidGenerationSourceError(
'Update methods have to return a Future of either void or int.',
element: _methodElement,
);
}
_helper.assertMethodReturnsIntOrVoid('Update', flattenedReturnType);

final parameterElement = _helper.getParameterElement();
final flattenedParameterType =
_helper.getFlattenedParameterType(parameterElement);
final entity = _helper.getEntity(flattenedParameterType);
final entity = _helper.getEntity(parameterElement);
final onConflict = _getOnConflictStrategy();

return UpdateMethod(
_methodElement,
name,
returnType,
flattenedReturnType,
_methodElement.name,
_methodElement.returnType,
flattenedReturnType.isVoid,
parameterElement,
entity,
onConflict,
Expand All @@ -76,26 +60,4 @@ class UpdateMethodProcessor implements Processor<UpdateMethod> {
return onConflictStrategy;
}
}

DartType _getFlattenedReturnType(final DartType returnType) {
return _methodElement.library.typeSystem.flatten(returnType);
}

void _assertMethodReturnsNoList(final DartType flattenedReturnType) {
if (flattenedReturnType.isDartCoreList) {
throw InvalidGenerationSourceError(
'Update methods have to return a Future of either void or int but not a list.',
element: _methodElement,
);
}
}

void _assertMethodReturnsFuture(final DartType returnType) {
if (!returnType.isDartAsyncFuture) {
throw InvalidGenerationSourceError(
'Update methods have to return a Future.',
element: _methodElement,
);
}
}
}
14 changes: 5 additions & 9 deletions floor_generator/lib/value_object/change_method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,21 @@ import 'package:floor_generator/value_object/entity.dart';

/// Base class for change methods (insert, update, delete).
class ChangeMethod {
final MethodElement methodElement;
final String name;
final DartType returnType;
final DartType flattenedReturnType;
final bool returnsVoid;
final ParameterElement parameterElement;
final Entity entity;

ChangeMethod(
this.methodElement,
this.name,
this.returnType,
this.flattenedReturnType,
this.returnsVoid,
this.parameterElement,
this.entity,
);

bool get requiresAsyncModifier => flattenedReturnType.isVoid;
bool get requiresAsyncModifier => returnsVoid;

bool get changesMultipleItems => parameterElement.type.isDartCoreList;

Expand All @@ -29,19 +27,17 @@ class ChangeMethod {
identical(this, other) ||
other is ChangeMethod &&
runtimeType == other.runtimeType &&
methodElement == other.methodElement &&
name == other.name &&
returnType == other.returnType &&
flattenedReturnType == other.flattenedReturnType &&
returnsVoid == other.returnsVoid &&
parameterElement == other.parameterElement &&
entity == other.entity;

@override
int get hashCode =>
methodElement.hashCode ^
name.hashCode ^
returnType.hashCode ^
flattenedReturnType.hashCode ^
returnsVoid.hashCode ^
parameterElement.hashCode ^
entity.hashCode;
}
Loading

0 comments on commit 2d22690

Please sign in to comment.