Skip to content

Commit

Permalink
Only use async modifier when necessary (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitusortner authored Feb 16, 2019
1 parent 183db39 commit 7c4d8d9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
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

0 comments on commit 7c4d8d9

Please sign in to comment.