Skip to content

Commit

Permalink
Refactor type check functions to use extension functions (#238)
Browse files Browse the repository at this point in the history
* Migrate type check functions to extension functions

* Delete unused imports
  • Loading branch information
vitusortner authored Jan 24, 2020
1 parent c6b2602 commit c03aa82
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 154 deletions.
4 changes: 2 additions & 2 deletions floor_generator/lib/misc/change_method_processor_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ class ChangeMethodProcessorHelper {
DartType getFlattenedParameterType(
@nonNull final ParameterElement parameterElement,
) {
final changesMultipleItems = isList(parameterElement.type);
final changesMultipleItems = parameterElement.type.isDartCoreList;

return changesMultipleItems
? flattenList(parameterElement.type)
? parameterElement.type.flatten()
: parameterElement.type;
}

Expand Down
70 changes: 22 additions & 48 deletions floor_generator/lib/misc/type_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,28 @@ import 'package:source_gen/source_gen.dart';
@nonNull
TypeChecker typeChecker(final Type type) => TypeChecker.fromRuntime(type);

@nonNull
bool isString(final DartType type) {
return type.isDartCoreString;
}

@nonNull
bool isBool(final DartType type) {
return type.isDartCoreBool;
}

@nonNull
bool isInt(final DartType type) {
return type.isDartCoreInt;
}

@nonNull
bool isDouble(final DartType type) {
return type.isDartCoreDouble;
}

@nonNull
bool isList(final DartType type) {
return type.isDartCoreList;
}

@nonNull
bool isSupportedType(final DartType type) {
return TypeChecker.any([
_stringTypeChecker,
_boolTypeChecker,
_intTypeChecker,
_doubleTypeChecker
]).isExactlyType(type);
}

@nonNull
bool isStream(final DartType type) {
return _streamTypeChecker.isExactlyType(type);
}

@nonNull
DartType flattenList(final DartType type) {
return (type as ParameterizedType).typeArguments.first;
}

@nonNull
DartType flattenStream(final DartType type) {
return (type as ParameterizedType).typeArguments.first;
extension SupportedTypeChecker on DartType {
@nonNull
bool get isSupported {
return TypeChecker.any([
_stringTypeChecker,
_boolTypeChecker,
_intTypeChecker,
_doubleTypeChecker
]).isExactlyType(this);
}
}

extension StreamTypeChecker on DartType {
@nonNull
bool get isStream => _streamTypeChecker.isExactlyType(this);
}

extension FlattenUtil on DartType {
@nonNull
DartType flatten() {
return (this as ParameterizedType).typeArguments.first;
}
}

final _stringTypeChecker = typeChecker(String);
Expand Down
5 changes: 2 additions & 3 deletions floor_generator/lib/processor/deletion_method_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:floor_generator/misc/annotations.dart';
import 'package:floor_generator/misc/change_method_processor_helper.dart';
import 'package:floor_generator/misc/type_utils.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';
Expand Down Expand Up @@ -34,7 +33,7 @@ class DeletionMethodProcessor implements Processor<DeletionMethod> {
_assertMethodReturnsNoList(flattenedReturnType);

final returnsVoid = flattenedReturnType.isVoid;
final returnsInt = isInt(flattenedReturnType);
final returnsInt = flattenedReturnType.isDartCoreInt;

if (!returnsVoid && !returnsInt) {
throw InvalidGenerationSourceError(
Expand Down Expand Up @@ -65,7 +64,7 @@ class DeletionMethodProcessor implements Processor<DeletionMethod> {
}

void _assertMethodReturnsNoList(final DartType flattenedReturnType) {
if (isList(flattenedReturnType)) {
if (flattenedReturnType.isDartCoreList) {
throw InvalidGenerationSourceError(
'Deletion methods have to return a Future of either void or int but not a list.',
element: _methodElement,
Expand Down
8 changes: 4 additions & 4 deletions floor_generator/lib/processor/entity_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,13 @@ class EntityProcessor extends Processor<Entity> {
final DartType parameterType,
final String parameterValue,
) {
if (isBool(parameterType)) {
if (parameterType.isDartCoreBool) {
return '($parameterValue as int) != 0'; // maps int to bool
} else if (isString(parameterType)) {
} else if (parameterType.isDartCoreString) {
return '$parameterValue as String';
} else if (isInt(parameterType)) {
} else if (parameterType.isDartCoreInt) {
return '$parameterValue as int';
} else if (isDouble(parameterType)) {
} else if (parameterType.isDartCoreDouble) {
return '$parameterValue as double';
} else {
return null;
Expand Down
8 changes: 4 additions & 4 deletions floor_generator/lib/processor/field_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ class FieldProcessor extends Processor<Field> {
@nonNull
String _getSqlType() {
final type = _fieldElement.type;
if (isInt(type)) {
if (type.isDartCoreInt) {
return SqlType.INTEGER;
} else if (isString(type)) {
} else if (type.isDartCoreString) {
return SqlType.TEXT;
} else if (isBool(type)) {
} else if (type.isDartCoreBool) {
return SqlType.INTEGER;
} else if (isDouble(type)) {
} else if (type.isDartCoreDouble) {
return SqlType.REAL;
}
throw InvalidGenerationSourceError(
Expand Down
8 changes: 4 additions & 4 deletions floor_generator/lib/processor/insertion_method_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class InsertionMethodProcessor implements Processor<InsertionMethod> {
_getFlattenedReturnType(returnType, returnsList);

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

if (!returnsVoid && !returnsIntList && !returnsInt) {
throw InvalidGenerationSourceError(
Expand Down Expand Up @@ -69,7 +69,7 @@ class InsertionMethodProcessor implements Processor<InsertionMethod> {
@nonNull
bool _getReturnsList(final DartType returnType) {
final type = _methodElement.library.typeSystem.flatten(returnType);
return isList(type);
return type.isDartCoreList;
}

@nonNull
Expand All @@ -78,7 +78,7 @@ class InsertionMethodProcessor implements Processor<InsertionMethod> {
final bool returnsList,
) {
final type = _methodElement.library.typeSystem.flatten(returnType);
return returnsList ? flattenList(type) : type;
return returnsList ? type.flatten() : type;
}

@nonNull
Expand Down
10 changes: 5 additions & 5 deletions floor_generator/lib/processor/query_method_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class QueryMethodProcessor extends Processor<QueryMethod> {
final rawReturnType = _methodElement.returnType;

final query = _getQuery();
final returnsStream = isStream(rawReturnType);
final returnsStream = rawReturnType.isStream;

_assertReturnsFutureOrStream(rawReturnType, returnsStream);

Expand Down Expand Up @@ -100,21 +100,21 @@ class QueryMethodProcessor extends Processor<QueryMethod> {
final returnsList = _getReturnsList(rawReturnType, returnsStream);

final type = returnsStream
? flattenStream(_methodElement.returnType)
? _methodElement.returnType.flatten()
: _methodElement.library.typeSystem.flatten(rawReturnType);
if (returnsList) {
return flattenList(type);
return type.flatten();
}
return type;
}

@nonNull
bool _getReturnsList(final DartType returnType, final bool returnsStream) {
final type = returnsStream
? flattenStream(returnType)
? returnType.flatten()
: _methodElement.library.typeSystem.flatten(returnType);

return isList(type);
return type.isDartCoreList;
}

void _assertReturnsFutureOrStream(
Expand Down
4 changes: 2 additions & 2 deletions floor_generator/lib/processor/update_method_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class UpdateMethodProcessor implements Processor<UpdateMethod> {
final flattenedReturnType = _getFlattenedReturnType(returnType);
_assertMethodReturnsNoList(flattenedReturnType);

final returnsInt = isInt(flattenedReturnType);
final returnsInt = flattenedReturnType.isDartCoreInt;
final returnsVoid = flattenedReturnType.isVoid;

if (!returnsInt && !returnsVoid) {
Expand Down Expand Up @@ -79,7 +79,7 @@ class UpdateMethodProcessor implements Processor<UpdateMethod> {
}

void _assertMethodReturnsNoList(final DartType flattenedReturnType) {
if (isList(flattenedReturnType)) {
if (flattenedReturnType.isDartCoreList) {
throw InvalidGenerationSourceError(
'Update methods have to return a Future of either void or int but not a list.',
element: _methodElement,
Expand Down
3 changes: 1 addition & 2 deletions floor_generator/lib/value_object/change_method.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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/value_object/entity.dart';

/// Base class for change methods (insert, update, delete).
Expand All @@ -23,7 +22,7 @@ class ChangeMethod {

bool get requiresAsyncModifier => flattenedReturnType.isVoid;

bool get changesMultipleItems => isList(parameterElement.type);
bool get changesMultipleItems => parameterElement.type.isDartCoreList;

@override
bool operator ==(Object other) =>
Expand Down
3 changes: 1 addition & 2 deletions floor_generator/lib/value_object/entity.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:analyzer/dart/element/element.dart';
import 'package:collection/collection.dart';
import 'package:floor_generator/misc/annotations.dart';
import 'package:floor_generator/misc/type_utils.dart';
import 'package:floor_generator/value_object/field.dart';
import 'package:floor_generator/value_object/foreign_key.dart';
import 'package:floor_generator/value_object/index.dart';
Expand Down Expand Up @@ -71,7 +70,7 @@ class Entity {
@nonNull
String _getAttributeValue(final FieldElement fieldElement) {
final parameterName = fieldElement.displayName;
return isBool(fieldElement.type)
return fieldElement.type.isDartCoreBool
? 'item.$parameterName ? 1 : 0'
: 'item.$parameterName';
}
Expand Down
6 changes: 3 additions & 3 deletions floor_generator/lib/value_object/query_method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ class QueryMethod {

bool get returnsList {
final type = returnsStream
? flattenStream(rawReturnType)
? rawReturnType.flatten()
: methodElement.library.typeSystem.flatten(rawReturnType);

return isList(type);
return type.isDartCoreList;
}

bool get returnsStream => isStream(rawReturnType);
bool get returnsStream => rawReturnType.isStream;

bool get returnsVoid => flattenedReturnType.isVoid;

Expand Down
6 changes: 3 additions & 3 deletions floor_generator/lib/writer/query_method_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class QueryMethodWriter implements Writer {

List<Parameter> _generateMethodParameters() {
return _queryMethod.parameters.map((parameter) {
if (!isSupportedType(parameter.type)) {
if (!parameter.type.isSupported) {
InvalidGenerationSourceError(
'The type of this parameter is not supported.',
element: parameter,
Expand Down Expand Up @@ -78,7 +78,7 @@ class QueryMethodWriter implements Writer {
var index = 0;
return _queryMethod.parameters
.map((parameter) {
if (isList(parameter.type)) {
if (parameter.type.isDartCoreList) {
index++;
return '''final valueList$index = ${parameter.displayName}.map((value) => "'\$value'").join(', ');''';
} else {
Expand All @@ -93,7 +93,7 @@ class QueryMethodWriter implements Writer {
List<String> _generateParameters() {
return _queryMethod.parameters
.map((parameter) {
if (!isList(parameter.type)) {
if (!parameter.type.isDartCoreList) {
return parameter.displayName;
} else {
return null;
Expand Down
Loading

0 comments on commit c03aa82

Please sign in to comment.