forked from pinchbv/floor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(refactor) Pull out query method return type into own class
- Loading branch information
Showing
7 changed files
with
160 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,52 @@ | ||
import 'package:analyzer/dart/element/element.dart'; | ||
import 'package:analyzer/dart/element/type.dart'; | ||
import 'package:collection/collection.dart'; | ||
import 'package:floor_generator/misc/extension/set_equality_extension.dart'; | ||
import 'package:floor_generator/misc/type_utils.dart'; | ||
import 'package:floor_generator/value_object/queryable.dart'; | ||
import 'package:floor_generator/value_object/query_method_return_type.dart'; | ||
import 'package:floor_generator/value_object/type_converter.dart'; | ||
|
||
/// Wraps a method annotated with Query | ||
/// to enable easy access to code generation relevant data. | ||
class QueryMethod { | ||
final MethodElement methodElement; | ||
|
||
final String name; | ||
|
||
/// Query where ':' got replaced with '$'. | ||
final String query; | ||
|
||
final DartType rawReturnType; | ||
|
||
/// Flattened return type. | ||
/// | ||
/// E.g. | ||
/// Future<T> -> T, | ||
/// Future<List<T>> -> T | ||
/// | ||
/// Stream<T> -> T | ||
/// Stream<List<T>> -> T | ||
final DartType flattenedReturnType; | ||
final QueryMethodReturnType returnType; | ||
|
||
final List<ParameterElement> parameters; | ||
|
||
final Queryable? queryable; | ||
|
||
final Set<TypeConverter> typeConverters; | ||
|
||
QueryMethod( | ||
this.methodElement, | ||
this.name, | ||
this.query, | ||
this.rawReturnType, | ||
this.flattenedReturnType, | ||
this.parameters, | ||
this.queryable, | ||
this.returnType, | ||
this.typeConverters, | ||
); | ||
|
||
bool get returnsList { | ||
final type = returnsStream | ||
? rawReturnType.flatten() | ||
: methodElement.library.typeSystem.flatten(rawReturnType); | ||
|
||
return type.isDartCoreList; | ||
} | ||
|
||
bool get returnsStream => rawReturnType.isStream; | ||
|
||
bool get returnsVoid => flattenedReturnType.isVoid; | ||
|
||
@override | ||
bool operator ==(Object other) => | ||
identical(this, other) || | ||
other is QueryMethod && | ||
runtimeType == other.runtimeType && | ||
methodElement == other.methodElement && | ||
name == other.name && | ||
query == other.query && | ||
rawReturnType == other.rawReturnType && | ||
flattenedReturnType == other.flattenedReturnType && | ||
parameters.equals(other.parameters) && | ||
queryable == other.queryable && | ||
returnType == other.returnType && | ||
typeConverters.equals(other.typeConverters); | ||
|
||
@override | ||
int get hashCode => | ||
methodElement.hashCode ^ | ||
name.hashCode ^ | ||
query.hashCode ^ | ||
rawReturnType.hashCode ^ | ||
flattenedReturnType.hashCode ^ | ||
parameters.hashCode ^ | ||
queryable.hashCode ^ | ||
returnType.hashCode ^ | ||
typeConverters.hashCode; | ||
|
||
@override | ||
String toString() { | ||
return 'QueryMethod{methodElement: $methodElement, name: $name, query: $query, rawReturnType: $rawReturnType, flattenedReturnType: $flattenedReturnType, parameters: $parameters, queryable: $queryable, typeConverters: $typeConverters}'; | ||
return 'QueryMethod{name: $name, query: $query, parameters: $parameters, returnType: $returnType, typeConverters: $typeConverters}'; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
floor_generator/lib/value_object/query_method_return_type.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import 'package:floor_generator/misc/type_utils.dart'; | ||
import 'package:analyzer/dart/element/type.dart'; | ||
import 'package:floor_generator/value_object/queryable.dart'; | ||
|
||
/// A simple accessor class for providing all properties of | ||
/// the return type of a query method | ||
class QueryMethodReturnType { | ||
final DartType raw; | ||
|
||
late Queryable? queryable; | ||
|
||
// the following attributes are derived on construction and stored. | ||
final bool isStream; | ||
final bool isFuture; | ||
final bool isList; | ||
|
||
/// Flattened return type | ||
/// | ||
/// E.g. | ||
/// Future<T> -> T | ||
/// Stream<List<T>> -> T | ||
final DartType flat; | ||
|
||
bool get isVoid => flat.isVoid; | ||
bool get isPrimitive => | ||
flat.isVoid || | ||
flat.isDartCoreDouble || | ||
flat.isDartCoreInt || | ||
flat.isDartCoreBool || | ||
flat.isDartCoreString || | ||
flat.isUint8List; | ||
|
||
QueryMethodReturnType(this.raw) | ||
: isStream = raw.isStream, | ||
isFuture = raw.isDartAsyncFuture, | ||
isList = raw.flatten().isDartCoreList, | ||
flat = _flattenWithList(raw); | ||
|
||
@override | ||
bool operator ==(Object other) => | ||
identical(this, other) || | ||
other is QueryMethodReturnType && | ||
runtimeType == other.runtimeType && | ||
raw == other.raw && | ||
queryable == other.queryable; | ||
|
||
@override | ||
int get hashCode => raw.hashCode ^ queryable.hashCode; | ||
|
||
@override | ||
String toString() { | ||
return 'QueryMethodReturnType{raw: $raw, flat: $flat, queryable: $queryable}'; | ||
} | ||
|
||
static DartType _flattenWithList(DartType raw) { | ||
final flattenedOnce = raw.flatten(); | ||
if (flattenedOnce.isDartCoreList) { | ||
return flattenedOnce.flatten(); | ||
} else { | ||
return flattenedOnce; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.