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

Only use async modifier when necessary #54

Merged
merged 1 commit into from
Feb 16, 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
16 changes: 11 additions & 5 deletions floor_generator/lib/model/change_method.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ class ChangeMethod {

DartType get returnType => method.returnType;

/// Whether this method returns an int or a list of ints.
bool get returnsInt => isInt(_flattenedReturnType);

/// Whether this methods 'returns' void.
bool get returnsVoid => _flattenedReturnType.isVoid;

String get name => method.displayName;
Expand All @@ -34,6 +36,8 @@ class ChangeMethod {
return parameters.first;
}

/// Whether this method takes a list as parameter and thus
/// changes multiple items.
bool get changesMultipleItems => isList(parameter.type);

ClassElement get flattenedParameterClass {
Expand All @@ -53,6 +57,13 @@ class ChangeMethod {
.any((entity) => entity == _flattenedParameterType.displayName);
}

bool get requiresAsyncModifier => returnsVoid || changesMultipleItems;

bool get returnsList {
final type = method.returnType.flattenFutures(method.context.typeSystem);
return isList(type);
}

DartType get _flattenedParameterType {
return changesMultipleItems ? flattenList(parameter.type) : parameter.type;
}
Expand All @@ -68,9 +79,4 @@ class ChangeMethod {
final type = method.returnType.flattenFutures(method.context.typeSystem);
return returnsList ? flattenList(type) : type;
}

bool get returnsList {
final type = method.returnType.flattenFutures(method.context.typeSystem);
return isList(type);
}
}
12 changes: 8 additions & 4 deletions floor_generator/lib/writer/change_method_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ class ChangeMethodWriter implements Writer {
return _generateChangeMethod();
}

// TODO remove async modifier when returning single int (one id)
Method _generateChangeMethod() {
return Method((builder) => builder
final builder = MethodBuilder()
..annotations.add(overrideAnnotationExpression)
..returns = refer(method.returnType.displayName)
..name = method.name
..requiredParameters.add(_generateParameter())
..modifier = MethodModifier.async
..body = methodBodyWriter.write());
..body = methodBodyWriter.write();

if (method.requiresAsyncModifier) {
builder..modifier = MethodModifier.async;
}

return builder.build();
}

Parameter _generateParameter() {
Expand Down