Skip to content

Commit

Permalink
Simplify the condition to invoke resolveCodec method in AnnotationRed…
Browse files Browse the repository at this point in the history
…isCodecResolver #1149

This adds a 'methodHasAtMostOneCodec' utility method to check whether a
CommandMethod has at most one codec

Original pull request: #1150.
  • Loading branch information
machi1990 authored and mp911de committed Oct 16, 2019
1 parent 68f4201 commit 45138d7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
* determine a {@link RedisCodec} that is able to handle all involved types.
*
* @author Mark Paluch
* @since 5.0
* @see Key
* @see Value
* @since 5.0
*/
public class AnnotationRedisCodecResolver implements RedisCodecResolver {

Expand Down Expand Up @@ -71,18 +71,23 @@ public AnnotationRedisCodecResolver(List<RedisCodec<?, ?>> codecs) {
return codecs.get(0);
}

if ((keyTypes.size() == 1 && (valueTypes.isEmpty() || valueTypes.size() == 1))
|| (valueTypes.size() == 1 && (keyTypes.isEmpty() || keyTypes.size() == 1))) {

RedisCodec<?, ?> codec = resolveCodec(keyTypes, valueTypes);
if (codec != null) {
return codec;
if (methodHasAtMostOneCodec(keyTypes, valueTypes)) {
RedisCodec<?, ?> resolvedCodec = resolveCodec(keyTypes, valueTypes);
if (resolvedCodec != null) {
return resolvedCodec;
}
}

throw new IllegalStateException(String.format("Cannot resolve Codec for method %s", commandMethod.getMethod()));
}

private boolean methodHasAtMostOneCodec(Set<Class<?>> keyTypes, Set<Class<?>> valueTypes) {
final int keyTypesSize = keyTypes.size();
final int valueTypesSize = valueTypes.size();

return keyTypesSize == 1 && valueTypesSize <= 1 || valueTypesSize == 1 && keyTypesSize <= 1;
}

private Voted<RedisCodec<?, ?>> voteForTypeMajority(CommandMethod commandMethod) {

List<Voted<RedisCodec<?, ?>>> votes = codecs.stream().map(redisCodec -> new Voted<RedisCodec<?, ?>>(redisCodec, 0))
Expand Down Expand Up @@ -139,12 +144,10 @@ private static void vote(List<Voted<RedisCodec<?, ?>>> votes, Parameter paramete
}

private RedisCodec<?, ?> resolveCodec(Set<Class<?>> keyTypes, Set<Class<?>> valueTypes) {

Class<?> keyType = keyTypes.isEmpty() ? null : keyTypes.iterator().next();
Class<?> valueType = valueTypes.isEmpty() ? null : valueTypes.iterator().next();

for (RedisCodec<?, ?> codec : codecs) {

ClassTypeInformation<?> typeInformation = ClassTypeInformation.from(codec.getClass());
TypeInformation<?> keyTypeArgument = typeInformation.getTypeArgument(RedisCodec.class, 0);
TypeInformation<?> valueTypeArgument = typeInformation.getTypeArgument(RedisCodec.class, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,17 @@ void shouldResolveHintedByteArrayValue() {
}

@Test
void resolutionShouldFail() {

void resolutionOfMethodWithMixedTypesShouldFail() {
Method method = ReflectionUtils.findMethod(CommandMethods.class, "mixedTypes", String.class, byte[].class);
assertThatThrownBy(() -> resolve(method)).isInstanceOf(IllegalStateException. class);
}

@Test
void resolutionOfMethodWithMixedCodecsShouldFail() {
Method method = ReflectionUtils.findMethod(CommandMethods.class, "mixedCodecs", String.class, byte[].class, String.class);
assertThatThrownBy(() -> resolve(method)).isInstanceOf(IllegalStateException. class);
}

@Test
void shouldDiscoverCodecTypesFromWrappers() {

Expand Down Expand Up @@ -129,6 +134,8 @@ private static interface CommandMethods {

String mixedTypes(@Key String key, @Value byte[] value);

String mixedCodecs(@Key String key1, @Key byte[] key2, @Value String value);

String withWrappers(@Value Range<String> range, @Value io.lettuce.core.Value<Number> value);

String withMap(Map<Integer, String> map);
Expand Down

0 comments on commit 45138d7

Please sign in to comment.