Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parameter verification for query methods #57

Merged
merged 1 commit into from
Feb 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions floor_generator/lib/model/query_method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,40 @@ import 'package:floor_generator/misc/constants.dart';
import 'package:floor_generator/model/entity.dart';
import 'package:source_gen/source_gen.dart';

/// Raps a method annotated with Query
/// Wraps a method annotated with Query
/// to enable easy access to code generation relevant data.
class QueryMethod {
final MethodElement method;

QueryMethod(final this.method);

/// Query as defined in by user in Dart code.
String get rawQuery {
String get _rawQuery {
final query = method.metadata
.firstWhere(isQueryAnnotation)
.computeConstantValue()
.getField(AnnotationField.QUERY_VALUE)
.toStringValue();

if (query.isEmpty || query == null) {
throw InvalidGenerationSourceError("You didn't define a query.",
element: method);
throw InvalidGenerationSourceError(
"You didn't define a query.",
element: method,
);
}

return query;
}

/// Query where ':' got replaced with '$'.
String get query => rawQuery.replaceAll(RegExp(':'), '\$');
String get query => _rawQuery.replaceAll(RegExp(':'), r'$');

List<String> get queryParameterNames {
return RegExp(r'\$.[^\s]+')
.allMatches(query)
.map((match) => match.group(0).replaceFirst(RegExp(r'\$'), ''))
.toList();
}

String get name => method.displayName;

Expand Down
37 changes: 31 additions & 6 deletions floor_generator/lib/writer/query_method_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ class QueryMethodWriter implements Writer {

@override
Method write() {
return _generateQueryMethod(queryMethod);
return _generateQueryMethod();
}

Method _generateQueryMethod(final QueryMethod queryMethod) {
Method _generateQueryMethod() {
_assertReturnsFuture();
_assertReturnsEntity();
_assertQueryParameters();

return Method((builder) => builder
..annotations.add(overrideAnnotationExpression)
Expand Down Expand Up @@ -113,17 +114,41 @@ class QueryMethodWriter implements Writer {
''';
}

void _assertQueryParameters() {
final queryParameterNames = queryMethod.queryParameterNames;
final methodSignatureParameterNames =
queryMethod.parameters.map((parameter) => parameter.name).toList();

final sameAmountParameters =
queryParameterNames.length == methodSignatureParameterNames.length;

final allParametersAreAvailable = queryParameterNames.every(
(parameterName) =>
methodSignatureParameterNames.any((name) => name == parameterName));

if (!allParametersAreAvailable || !sameAmountParameters) {
throw InvalidGenerationSourceError(
"Parameters of method signature don't match with parameters in the query.",
element: queryMethod.method,
);
}
}

void _assertReturnsFuture() {
if (!queryMethod.rawReturnType.isDartAsyncFuture) {
throw InvalidGenerationSourceError('All queries have to return a Future.',
element: queryMethod.method);
throw InvalidGenerationSourceError(
'All queries have to return a Future.',
element: queryMethod.method,
);
}
}

void _assertReturnsEntity() {
if (!queryMethod.returnsEntity(library)) {
throw InvalidGenerationSourceError('The return type is not an entity.',
element: queryMethod.method);
throw InvalidGenerationSourceError(
'The return type is not an entity.',
element: queryMethod.method,
);
}
}
}
2 changes: 1 addition & 1 deletion floor_test/test/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ abstract class TestDatabase extends FloorDatabase {
@Query('SELECT * FROM person WHERE id = :id')
Future<Person> findPersonById(int id);

@Query('SELECT * FROM person WHERE id = :id AND name = :name')
@Query('SELECT * FROM person WHERE id = :id AND custom_name = :name')
Future<Person> findPersonByIdAndName(int id, String name);

@insert
Expand Down