From 7c4d8d958c67542013adfe7513499b6fb3aa728e Mon Sep 17 00:00:00 2001 From: Vitus Date: Sat, 16 Feb 2019 18:03:33 +0100 Subject: [PATCH] Only use async modifier when necessary (#54) --- floor_generator/lib/model/change_method.dart | 16 +++++++++++----- .../lib/writer/change_method_writer.dart | 12 ++++++++---- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/floor_generator/lib/model/change_method.dart b/floor_generator/lib/model/change_method.dart index 9de982f2..a4be90b3 100644 --- a/floor_generator/lib/model/change_method.dart +++ b/floor_generator/lib/model/change_method.dart @@ -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; @@ -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 { @@ -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; } @@ -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); - } } diff --git a/floor_generator/lib/writer/change_method_writer.dart b/floor_generator/lib/writer/change_method_writer.dart index 277dd050..b13864de 100644 --- a/floor_generator/lib/writer/change_method_writer.dart +++ b/floor_generator/lib/writer/change_method_writer.dart @@ -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() {