Skip to content

Commit

Permalink
Improve Kotlin API generator to generate replacement for a deprecated…
Browse files Browse the repository at this point in the history
… method #1759

Also, fix typo.
  • Loading branch information
sokomishalov authored and mp911de committed Sep 3, 2021
1 parent 867b7c8 commit 0a1aae7
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public interface RedisScriptingAsyncCommands<K, V> {
/**
* Remove all the scripts from the script cache using the specified {@link FlushMode}.
*
* @param flushMode the flush mode (sync/asnync).
* @param flushMode the flush mode (sync/async).
* @return String simple-string-reply.
* @since 6.1
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public interface RedisServerAsyncCommands<K, V> {
/**
* Remove all keys from all databases using the specified {@link FlushMode}.
*
* @param flushMode the flush mode (sync/asnync).
* @param flushMode the flush mode (sync/async).
* @return String simple-string-reply.
* @since 6.1
*/
Expand All @@ -309,7 +309,7 @@ public interface RedisServerAsyncCommands<K, V> {
/**
* Remove all keys from the current database using the specified {@link FlushMode}.
*
* @param flushMode the flush mode (sync/asnync).
* @param flushMode the flush mode (sync/async).
* @return String simple-string-reply.
* @since 6.1
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public interface RedisScriptingReactiveCommands<K, V> {
/**
* Remove all the scripts from the script cache using the specified {@link FlushMode}.
*
* @param flushMode the flush mode (sync/asnync).
* @param flushMode the flush mode (sync/async).
* @return String simple-string-reply.
* @since 6.1
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public interface RedisServerReactiveCommands<K, V> {
/**
* Remove all keys from all databases using the specified {@link FlushMode}.
*
* @param flushMode the flush mode (sync/asnync).
* @param flushMode the flush mode (sync/async).
* @return String simple-string-reply.
* @since 6.1
*/
Expand All @@ -309,7 +309,7 @@ public interface RedisServerReactiveCommands<K, V> {
/**
* Remove all keys from the current database using the specified {@link FlushMode}.
*
* @param flushMode the flush mode (sync/asnync).
* @param flushMode the flush mode (sync/async).
* @return String simple-string-reply.
* @since 6.1
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public interface RedisScriptingCommands<K, V> {
/**
* Remove all the scripts from the script cache using the specified {@link FlushMode}.
*
* @param flushMode the flush mode (sync/asnync).
* @param flushMode the flush mode (sync/async).
* @return String simple-string-reply.
* @since 6.1
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public interface RedisServerCommands<K, V> {
/**
* Remove all keys from all databases using the specified {@link FlushMode}.
*
* @param flushMode the flush mode (sync/asnync).
* @param flushMode the flush mode (sync/async).
* @return String simple-string-reply.
* @since 6.1
*/
Expand All @@ -308,7 +308,7 @@ public interface RedisServerCommands<K, V> {
/**
* Remove all keys from the current database using the specified {@link FlushMode}.
*
* @param flushMode the flush mode (sync/asnync).
* @param flushMode the flush mode (sync/async).
* @return String simple-string-reply.
* @since 6.1
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public interface NodeSelectionScriptingAsyncCommands<K, V> {
/**
* Remove all the scripts from the script cache using the specified {@link FlushMode}.
*
* @param flushMode the flush mode (sync/asnync).
* @param flushMode the flush mode (sync/async).
* @return String simple-string-reply.
* @since 6.1
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public interface NodeSelectionServerAsyncCommands<K, V> {
/**
* Remove all keys from all databases using the specified {@link FlushMode}.
*
* @param flushMode the flush mode (sync/asnync).
* @param flushMode the flush mode (sync/async).
* @return String simple-string-reply.
* @since 6.1
*/
Expand All @@ -294,7 +294,7 @@ public interface NodeSelectionServerAsyncCommands<K, V> {
/**
* Remove all keys from the current database using the specified {@link FlushMode}.
*
* @param flushMode the flush mode (sync/asnync).
* @param flushMode the flush mode (sync/async).
* @return String simple-string-reply.
* @since 6.1
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public interface NodeSelectionScriptingCommands<K, V> {
/**
* Remove all the scripts from the script cache using the specified {@link FlushMode}.
*
* @param flushMode the flush mode (sync/asnync).
* @param flushMode the flush mode (sync/async).
* @return String simple-string-reply.
* @since 6.1
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public interface NodeSelectionServerCommands<K, V> {
/**
* Remove all keys from all databases using the specified {@link FlushMode}.
*
* @param flushMode the flush mode (sync/asnync).
* @param flushMode the flush mode (sync/async).
* @return String simple-string-reply.
* @since 6.1
*/
Expand All @@ -294,7 +294,7 @@ public interface NodeSelectionServerCommands<K, V> {
/**
* Remove all keys from the current database using the specified {@link FlushMode}.
*
* @param flushMode the flush mode (sync/asnync).
* @param flushMode the flush mode (sync/async).
* @return String simple-string-reply.
* @since 6.1
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ interface RedisScriptingCoroutinesCommands<K : Any, V : Any> {
/**
* Remove all the scripts from the script cache using the specified [FlushMode].
*
* @param flushMode the flush mode (sync/asnync).
* @param flushMode the flush mode (sync/async).
* @return String simple-string-reply.
* @since 6.1
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ interface RedisServerCoroutinesCommands<K : Any, V : Any> {
/**
* Remove all keys from all databases using the specified [FlushMode].
*
* @param flushMode the flush mode (sync/asnync).
* @param flushMode the flush mode (sync/async).
* @return String simple-string-reply.
* @since 6.1
*/
Expand All @@ -291,7 +291,7 @@ interface RedisServerCoroutinesCommands<K : Any, V : Any> {
* @return String simple-string-reply.
* @deprecated since 6.1, use [flushall(FlushMode)] instead.
*/
@Deprecated("since 6.1, use [flushall(FlushMode)] instead")
@Deprecated("Use [flushall(FlushMode.ASYNC)] instead.", ReplaceWith("flushall(FlushMode.ASYNC)"))
suspend fun flushallAsync(): String?

/**
Expand All @@ -304,7 +304,7 @@ interface RedisServerCoroutinesCommands<K : Any, V : Any> {
/**
* Remove all keys from the current database using the specified [FlushMode].
*
* @param flushMode the flush mode (sync/asnync).
* @param flushMode the flush mode (sync/async).
* @return String simple-string-reply.
* @since 6.1
*/
Expand All @@ -316,7 +316,7 @@ interface RedisServerCoroutinesCommands<K : Any, V : Any> {
* @return String simple-string-reply.
* @deprecated since 6.1, use [flushdb(FlushMode)] instead.
*/
@Deprecated("since 6.1, use [flushdb(FlushMode)] instead.")
@Deprecated("Use [flushdb(FlushMode.ASYNC)] instead.", ReplaceWith("flushdb(FlushMode.ASYNC)"))
suspend fun flushdbAsync(): String?

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public interface RedisScriptingCommands<K, V> {
/**
* Remove all the scripts from the script cache using the specified {@link FlushMode}.
*
* @param flushMode the flush mode (sync/asnync).
* @param flushMode the flush mode (sync/async).
* @return String simple-string-reply.
* @since 6.1
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public interface RedisServerCommands<K, V> {
/**
* Remove all keys from all databases using the specified {@link FlushMode}.
*
* @param flushMode the flush mode (sync/asnync).
* @param flushMode the flush mode (sync/async).
* @return String simple-string-reply.
* @since 6.1
*/
Expand All @@ -307,7 +307,7 @@ public interface RedisServerCommands<K, V> {
/**
* Remove all keys from the current database using the specified {@link FlushMode}.
*
* @param flushMode the flush mode (sync/asnync).
* @param flushMode the flush mode (sync/async).
* @return String simple-string-reply.
* @since 6.1
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ class KotlinCompilationUnitFactory {
private static final Set<String> NON_SUSPENDABLE_METHODS = LettuceSets.unmodifiableSet("isOpen", "flushCommands", "setAutoFlushCommands");
private static final Set<String> SKIP_METHODS = LettuceSets.unmodifiableSet("BaseRedisCommands.reset", "getStatefulConnection");

private static final Set<String> KEEP_DEPRECATED_METHODS = LettuceSets.unmodifiableSet("flushallAsync", "flushdbAsync");

private static final Set<String> FLOW_METHODS = LettuceSets.unmodifiableSet("aclList", "aclLog", "dispatch", "geohash", "georadius",
"georadiusbymember", "geosearch",
"hgetall", "hkeys", "hmget", "hvals", "keys", "mget", "sdiff", "sinter", "smembers", "smismember", "sort", "srandmember", "sunion",
Expand All @@ -75,13 +73,20 @@ class KotlinCompilationUnitFactory {
"RedisSentinelCoroutinesCommands.clientKill", "RedisSentinelCoroutinesCommands.clientPause",
"RedisSentinelCoroutinesCommands.clientList", "RedisSentinelCoroutinesCommands.info",
"RedisSentinelCoroutinesCommands.ping", "pubsubNumsub", "pubsubNumpat", "echo", "ping", "readOnly", "readWrite");

private static final Map<String, String> RESULT_SPEC;
private static final Map<String, String> KEEP_DEPRECATED_METHODS;

static {
Map<String, String> resultSpec = new HashMap<>();
resultSpec.put("hgetall", "Flow<KeyValue<K, V>>");
resultSpec.put("zmscore", "List<Double?>");
RESULT_SPEC = resultSpec;

Map<String, String> deprecatedMethodsSpec = new HashMap<>();
deprecatedMethodsSpec.put("flushallAsync", "flushall(FlushMode.ASYNC)");
deprecatedMethodsSpec.put("flushdbAsync", "flushdb(FlushMode.ASYNC)");
KEEP_DEPRECATED_METHODS = deprecatedMethodsSpec;
}

private static final String FORMATTING_INDENT = " ";
Expand Down Expand Up @@ -171,11 +176,12 @@ private class MethodVisitor extends VoidVisitorAdapter<Object> {
public void visit(MethodDeclaration method, Object arg) {

// Skip deprecated and StreamingChannel methods
if (!contains(KEEP_DEPRECATED_METHODS, method) && (method.isAnnotationPresent(Deprecated.class)
if (!contains(KEEP_DEPRECATED_METHODS.keySet(), method) && (method.isAnnotationPresent(Deprecated.class)
|| contains(SKIP_METHODS, method)
|| method.getParameters().stream().anyMatch(p -> p.getType().asString().contains("StreamingChannel")))) {
return;
}

result
.append(FORMATTING_INDENT)
.append(extractJavadoc(method.getJavadoc().get()).replace("\n", "\n" + FORMATTING_INDENT))
Expand All @@ -195,7 +201,16 @@ private String extractAnnotations(MethodDeclaration method) {
return method
.getAnnotations()
.stream()
.map(a -> a.getNameAsString() + "\n" + FORMATTING_INDENT)
.map(a -> {
String annotation = a.getNameAsString();
if (annotation.equals("Deprecated")) {
String identifier = method.getName().getIdentifier();
String replacement = KEEP_DEPRECATED_METHODS.get(identifier);
return "@" + annotation + "(\"Use [" + replacement + "] instead.\", ReplaceWith(\"" + replacement + "\"))\n" + FORMATTING_INDENT;
} else {
return "@" + annotation + "\n" + FORMATTING_INDENT;
}
})
.collect(joining());
}

Expand Down

0 comments on commit 0a1aae7

Please sign in to comment.