Skip to content

Commit

Permalink
Merge pull request #74 from Iteo/feature/future-void-clear-method
Browse files Browse the repository at this point in the history
Added future void return type to clear method
  • Loading branch information
Axot017 committed Nov 8, 2022
2 parents 8be9004 + a58ef72 commit cbc2239
Show file tree
Hide file tree
Showing 12 changed files with 271 additions and 42 deletions.
2 changes: 1 addition & 1 deletion packages/cached/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ Possible reasons why the generator gives an error

- if method with `@cached` annotation doesn’t exist
- if method to pair doesn’t exist
- if method don’t return `bool`, `Future<bool>` or not a `void`
- if method don’t return `bool`, `Future<bool>` or not a `void`, `Future<void>`

### ClearAllCached

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,32 @@ class ClearAllCachedMethodTemplate {
final Map<String, StreamedCacheMethod> streamedCacheMethodPerName;
final AllParamsTemplate paramsTemplate;

String get asyncModifier => isFuture(method!.returnType) ? 'async' : '';
String get awaitIfNeeded => isFuture(method!.returnType) ? 'await' : '';

String _generateCacheClearMethods() =>
cachedMethods.map(_generateClearMaps).join("\n");

String generateMethod() {
if (method == null) return '';

if (method!.isAbstract) return _generateAbstractMethod();
if (isVoid(method!.returnType)) return _generateVoidMethod();

final asyncModifier = isFuture(method!.returnType) ? 'async' : '';
final awaitIfNeeded = isFuture(method!.returnType) ? 'await' : '';
if (isFutureBool(method!.returnType) || isBool(method!.returnType)) {
return _generateBoolMethod();
}

return '''
@override
${method!.returnType} ${method!.name}(${paramsTemplate.generateParams()}) $asyncModifier {
$awaitIfNeeded super.${method!.name}(${paramsTemplate.generateParamsUsage()});
${_generateCacheClearMethods()}
}
''';
}

String _generateBoolMethod() {
return '''
@override
${method!.returnType} ${method!.name}(${paramsTemplate.generateParams()}) $asyncModifier {
Expand All @@ -48,21 +62,10 @@ class ClearAllCachedMethodTemplate {
''';
}

String _generateVoidMethod() {
return '''
@override
${method!.returnType} ${method!.name}(${paramsTemplate.generateParams()}) {
super.${method!.name}(${paramsTemplate.generateParamsUsage()});
${_generateCacheClearMethods()}
}
''';
}

String _generateAbstractMethod() {
return '''
@override
void ${method!.name}() {
${method!.returnType} ${method!.name}() $asyncModifier {
${_generateCacheClearMethods()}
}
''';
Expand Down
35 changes: 19 additions & 16 deletions packages/cached/lib/src/templates/clear_cached_method_template.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,27 @@ class ClearCachedMethodTemplate {
final AllParamsTemplate paramsTemplate;
final StreamedCacheMethod? streamedCacheMethod;

String get asyncModifier => isFuture(method.returnType) ? 'async' : '';
String get awaitIfNeeded => isFuture(method.returnType) ? 'await' : '';

String generateMethod() {
if (method.isAbstract) return _generateAbstractMethod();
if (isVoid(method.returnType)) return _generateVoidMethod();

final asyncModifier = isFuture(method.returnType) ? 'async' : '';
final awaitIfNeeded = isFuture(method.returnType) ? 'await' : '';
if (isFutureBool(method.returnType) || isBool(method.returnType)) {
return _generateBoolMethod();
}

return '''
@override
${method.returnType} ${method.name}(${paramsTemplate.generateParams()}) $asyncModifier {
$awaitIfNeeded super.${method.name}(${paramsTemplate.generateParamsUsage()});
${_generateClearMaps()}
}
''';
}

String _generateBoolMethod() {
return '''
@override
${method.returnType} ${method.name}(${paramsTemplate.generateParams()}) $asyncModifier {
Expand All @@ -35,23 +49,12 @@ class ClearCachedMethodTemplate {
''';
}

String _generateVoidMethod() {
return '''
@override
${_generateClearMaps()}
super.${method.name}(${paramsTemplate.generateParamsUsage()});
${getCacheMapName(method.methodName)}.clear();
}
''';
}

String _generateAbstractMethod() {
return '''
@override
void ${method.name}() {
${method.returnType} ${method.name}() $asyncModifier {
${_generateClearMaps()}
}
}
''';
}

Expand Down
8 changes: 4 additions & 4 deletions packages/cached/lib/src/utils/asserts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ void assertValidateClearCachedMethods(
}

void assertCorrectClearMethodType(MethodElement element) {
final returnType = element.returnType.getDisplayString(withNullability: true);

if (element.isAbstract) {
if (element.isAsynchronous) {
throw InvalidGenerationSourceError(
Expand All @@ -112,9 +114,9 @@ void assertCorrectClearMethodType(MethodElement element) {
);
}

if (!element.returnType.isVoid) {
if (!(isAsyncVoid(returnType) || isVoid(returnType))) {
throw InvalidGenerationSourceError(
'[ERROR] `${element.name}` must be a void method',
'[ERROR] `${element.name}` must be a void or Future<void> method',
element: element,
);
}
Expand All @@ -126,8 +128,6 @@ void assertCorrectClearMethodType(MethodElement element) {
);
}
} else {
final returnType =
element.returnType.getDisplayString(withNullability: true);
if (!isVoid(returnType) &&
!isBool(returnType) &&
!isAsyncVoid(returnType) &&
Expand Down
3 changes: 2 additions & 1 deletion packages/cached/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ environment:
dependencies:
analyzer: ">=3.0.0 <5.0.0"
build: ^2.3.0
cached_annotation: ^1.3.0
cached_annotation:
path: ../cached_annotation
collection: ^1.16.0
meta: ^1.7.0
source_gen: ^1.2.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ Future<void> main() async {
'AbstractWithParams',
'ValidAbstractWithTtl',
'ValidAbstract',
'ValidAbstractFuture',
'ValidReturnFutureBool',
'ValidReturnFutureVoid',
};

final reader = await initializeLibraryReaderForDirectory(
Expand Down
2 changes: 2 additions & 0 deletions packages/cached/test/clear_cached_method_generation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ Future<void> main() async {
'AbstractWithParams',
'ValidAbstractWithTtl',
'ValidAbstract',
'ValidAbstractFuture',
'ValidAbstractWithTwoCachedMethod',
'ValidReturnFutureBool',
'ValidReturnFutureVoid',
};

final reader = await initializeLibraryReaderForDirectory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ abstract class MultipleClearAllMethods {
void somethingTwo();
}

@ShouldThrow('[ERROR] `something` must be a void method', element: false)
@ShouldThrow(
'[ERROR] `something` must be a void or Future<void> method',
element: false,
)
@withCache
abstract class InvalidReturnType {
factory InvalidReturnType() = _InvalidReturnType;
Expand Down Expand Up @@ -183,6 +186,58 @@ abstract class ValidAbstract {
void something();
}

@ShouldGenerate(
r'''
abstract class _$ValidAbstractFuture {}
class _ValidAbstractFuture
with ValidAbstractFuture
implements _$ValidAbstractFuture {
_ValidAbstractFuture();
final _cachedMethodCached = <String, int>{};
@override
int cachedMethod() {
final cachedValue = _cachedMethodCached[""];
if (cachedValue == null) {
final int toReturn;
try {
final result = super.cachedMethod();
toReturn = result;
} catch (_) {
rethrow;
} finally {}
_cachedMethodCached[""] = toReturn;
return toReturn;
} else {
return cachedValue;
}
}
@override
Future<void> something() async {
_cachedMethodCached.clear();
}
}
''',
)
@withCache
abstract class ValidAbstractFuture {
factory ValidAbstractFuture() = _ValidAbstractFuture;

@cached
int cachedMethod() {
return 1;
}

@clearAllCached
Future<void> something();
}

@ShouldGenerate(
r'''
abstract class _$ValidReturnFutureBool {}
Expand Down Expand Up @@ -245,3 +300,57 @@ abstract class ValidReturnFutureBool {
return true;
}
}

@ShouldGenerate(
r'''
abstract class _$ValidReturnFutureVoid {}
class _ValidReturnFutureVoid
with ValidReturnFutureVoid
implements _$ValidReturnFutureVoid {
_ValidReturnFutureVoid();
final _cachedMethodCached = <String, int>{};
@override
int cachedMethod() {
final cachedValue = _cachedMethodCached[""];
if (cachedValue == null) {
final int toReturn;
try {
final result = super.cachedMethod();
toReturn = result;
} catch (_) {
rethrow;
} finally {}
_cachedMethodCached[""] = toReturn;
return toReturn;
} else {
return cachedValue;
}
}
@override
Future<void> something() async {
await super.something();
_cachedMethodCached.clear();
}
}
''',
)
@withCache
abstract class ValidReturnFutureVoid {
factory ValidReturnFutureVoid() = _ValidReturnFutureVoid;

@cached
int cachedMethod() {
return 1;
}

@clearAllCached
Future<void> something() async {}
}
Loading

0 comments on commit cbc2239

Please sign in to comment.