Skip to content

Commit

Permalink
Update RedisVersion parser to accept version numbers with non-numeric…
Browse files Browse the repository at this point in the history
… suffix

#2557 #2558
  • Loading branch information
mp911de committed Nov 30, 2023
1 parent 6b4f9d5 commit 604da99
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/main/java/io/lettuce/core/RedisHandshake.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.lettuce.core.codec.StringCodec;
import io.lettuce.core.internal.Futures;
import io.lettuce.core.internal.LettuceAssert;
import io.lettuce.core.internal.LettuceStrings;
import io.lettuce.core.protocol.AsyncCommand;
import io.lettuce.core.protocol.Command;
Expand Down Expand Up @@ -309,6 +312,8 @@ private static boolean isNoProto(Throwable error) {
*/
static class RedisVersion {

private static final Pattern DECIMALS = Pattern.compile("(\\d+)");

private final static RedisVersion UNKNOWN = new RedisVersion("0.0.0");

private final static RedisVersion UNSTABLE = new RedisVersion("255.255.255");
Expand All @@ -321,20 +326,24 @@ static class RedisVersion {

private RedisVersion(String version) {

String[] split = version.split("\\.");

int major = 0;
int minor = 0;
int bugfix = 0;
if (split.length > 0) {
major = Integer.parseInt(split[0]);
}
if (split.length > 1) {
minor = Integer.parseInt(split[1]);
}

if (split.length > 2) {
bugfix = Integer.parseInt(split[2]);
LettuceAssert.notNull(version, "Version must not be null");

Matcher matcher = DECIMALS.matcher(version);

if (matcher.find()) {
major = Integer.parseInt(matcher.group(1));

if (matcher.find()) {
minor = Integer.parseInt(matcher.group(1));
}

if (matcher.find()) {
bugfix = Integer.parseInt(matcher.group(1));
}
}

this.major = major;
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/io/lettuce/core/RedisHandshakeUnitTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ void handshakeWithDiscoveryShouldDowngrade() {
assertThat(state.getNegotiatedProtocolVersion()).isEqualTo(ProtocolVersion.RESP2);
}

@Test
void shouldParseVersionWithCharacters() {

assertThat(RedisHandshake.RedisVersion.of("1.2.3").toString()).isEqualTo("1.2.3");
assertThat(RedisHandshake.RedisVersion.of("01.02.03").toString()).isEqualTo("1.2.3");
assertThat(RedisHandshake.RedisVersion.of("01.02").toString()).isEqualTo("1.2.0");
assertThat(RedisHandshake.RedisVersion.of("01").toString()).isEqualTo("1.0.0");

assertThat(RedisHandshake.RedisVersion.of("1.2a.3").toString()).isEqualTo("1.2.3");
assertThat(RedisHandshake.RedisVersion.of("1.2.3a").toString()).isEqualTo("1.2.3");
assertThat(RedisHandshake.RedisVersion.of("1.2.3(c)").toString()).isEqualTo("1.2.3");
assertThat(RedisHandshake.RedisVersion.of("a.2.3(c)").toString()).isEqualTo("2.3.0");
}

private static void helloResponse(CommandOutput<String, String, Map<String, String>> output) {

output.multiMap(8);
Expand Down

0 comments on commit 604da99

Please sign in to comment.