Search in sources :

Example 1 with ResolvableType

use of io.lettuce.core.dynamic.support.ResolvableType in project lettuce-core by lettuce-io.

the class LettuceExtension method findSupplier.

@SuppressWarnings("unchecked")
private static Supplier<Object> findSupplier(Type type) {
    ResolvableType requested = ResolvableType.forType(type);
    Supplier<?> supplier = SUPPLIERS.stream().filter(it -> {
        ResolvableType providedType = ResolvableType.forType(it.getClass()).as(Supplier.class).getGeneric(0);
        if (requested.isAssignableFrom(providedType)) {
            return true;
        }
        return false;
    }).findFirst().orElseThrow(() -> new NoSuchElementException("Cannot find a factory for " + type));
    return (Supplier) supplier;
}
Also used : Supplier(java.util.function.Supplier) ResolvableType(io.lettuce.core.dynamic.support.ResolvableType)

Example 2 with ResolvableType

use of io.lettuce.core.dynamic.support.ResolvableType in project lettuce-core by lettuce-io.

the class CommandOutputResolverSupport method isAssignableFrom.

/**
 * Overridable hook to check whether {@code selector} can be assigned from the provider type {@code provider}.
 * <p>
 * This method descends the component type hierarchy and considers primitive/wrapper type conversion.
 *
 * @param selector must not be {@code null}.
 * @param provider must not be {@code null}.
 * @return {@code true} if selector can be assigned from its provider type.
 */
protected boolean isAssignableFrom(OutputSelector selector, OutputType provider) {
    ResolvableType selectorType = selector.getOutputType();
    ResolvableType resolvableType = provider.withCodec(selector.getRedisCodec());
    return selectorType.isAssignableFrom(resolvableType);
}
Also used : ResolvableType(io.lettuce.core.dynamic.support.ResolvableType)

Example 3 with ResolvableType

use of io.lettuce.core.dynamic.support.ResolvableType in project lettuce-core by lettuce-io.

the class OutputRegistry method getOutputComponentType.

/**
 * Retrieve {@link OutputType} for a {@link CommandOutput} type.
 *
 * @param commandOutputClass
 * @return
 */
static OutputType getOutputComponentType(Class<? extends CommandOutput> commandOutputClass) {
    ClassTypeInformation<? extends CommandOutput> classTypeInformation = ClassTypeInformation.from(commandOutputClass);
    TypeInformation<?> superTypeInformation = classTypeInformation.getSuperTypeInformation(CommandOutput.class);
    if (superTypeInformation == null) {
        return null;
    }
    List<TypeInformation<?>> typeArguments = superTypeInformation.getTypeArguments();
    return new OutputType(commandOutputClass, typeArguments.get(2), false) {

        @Override
        public ResolvableType withCodec(RedisCodec<?, ?> codec) {
            TypeInformation<?> typeInformation = ClassTypeInformation.from(codec.getClass());
            ResolvableType resolvableType = ResolvableType.forType(commandOutputClass, new CodecVariableTypeResolver(typeInformation));
            while (!resolvableType.getRawClass().equals(CommandOutput.class)) {
                resolvableType = resolvableType.getSuperType();
            }
            return resolvableType.getGeneric(2);
        }
    };
}
Also used : RedisCodec(io.lettuce.core.codec.RedisCodec) ResolvableType(io.lettuce.core.dynamic.support.ResolvableType) ClassTypeInformation(io.lettuce.core.dynamic.support.ClassTypeInformation) TypeInformation(io.lettuce.core.dynamic.support.TypeInformation)

Example 4 with ResolvableType

use of io.lettuce.core.dynamic.support.ResolvableType in project lettuce-core by lettuce-io.

the class OutputRegistryUnitTests method customizedValueOutput.

@Test
void customizedValueOutput() {
    OutputType outputComponentType = OutputRegistry.getOutputComponentType(KeyTypedOutput.class);
    ResolvableType resolvableType = outputComponentType.withCodec(ByteArrayCodec.INSTANCE);
    assertThat(resolvableType.isAssignableFrom(ResolvableType.forClass(byte[].class))).isTrue();
}
Also used : ResolvableType(io.lettuce.core.dynamic.support.ResolvableType) Test(org.junit.jupiter.api.Test)

Example 5 with ResolvableType

use of io.lettuce.core.dynamic.support.ResolvableType in project lettuce-core by lettuce-io.

the class OutputRegistryUnitTests method streamingTypeOfKeyListOuputWithCodecIsAssignableFromListOfString.

@Test
void streamingTypeOfKeyListOuputWithCodecIsAssignableFromListOfString() {
    OutputType outputComponentType = OutputRegistry.getStreamingType(ScoredValueListOutput.class);
    ResolvableType resolvableType = outputComponentType.withCodec(new StringCodec());
    assertThat(resolvableType.isAssignableFrom(ResolvableType.forClassWithGenerics(ScoredValue.class, String.class))).isTrue();
}
Also used : StringCodec(io.lettuce.core.codec.StringCodec) ResolvableType(io.lettuce.core.dynamic.support.ResolvableType) ScoredValue(io.lettuce.core.ScoredValue) Test(org.junit.jupiter.api.Test)

Aggregations

ResolvableType (io.lettuce.core.dynamic.support.ResolvableType)10 Test (org.junit.jupiter.api.Test)5 StringCodec (io.lettuce.core.codec.StringCodec)4 RedisCodec (io.lettuce.core.codec.RedisCodec)2 ClassTypeInformation (io.lettuce.core.dynamic.support.ClassTypeInformation)2 TypeInformation (io.lettuce.core.dynamic.support.TypeInformation)2 Supplier (java.util.function.Supplier)2 ClientOptions (io.lettuce.core.ClientOptions)1 RedisClient (io.lettuce.core.RedisClient)1 ScoredValue (io.lettuce.core.ScoredValue)1 StatefulConnection (io.lettuce.core.api.StatefulConnection)1 StatefulRedisConnection (io.lettuce.core.api.StatefulRedisConnection)1 RedisCommands (io.lettuce.core.api.sync.RedisCommands)1 RedisClusterClient (io.lettuce.core.cluster.RedisClusterClient)1 StatefulRedisClusterConnection (io.lettuce.core.cluster.api.StatefulRedisClusterConnection)1 StatefulRedisPubSubConnection (io.lettuce.core.pubsub.StatefulRedisPubSubConnection)1 ClientResources (io.lettuce.core.resource.ClientResources)1 DefaultRedisClient (io.lettuce.test.resource.DefaultRedisClient)1 DefaultRedisClusterClient (io.lettuce.test.resource.DefaultRedisClusterClient)1 TestClientResources (io.lettuce.test.resource.TestClientResources)1