GH-474 Add TypeResolver library and simplify type discovery

For complex cases where deep hierarchies are used there was still an issue with the fix in #473.
By adding TypeResolver library we essentially simplify our discovery process

Resolves #474
This commit is contained in:
Oleg Zhurakousky
2020-03-30 17:26:01 +02:00
parent 73c8f9c1a4
commit 1ec107abbd
5 changed files with 52 additions and 47 deletions

View File

@@ -41,6 +41,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.function.context.FunctionCatalog;
import org.springframework.cloud.function.context.catalog.BeanFactoryAwareFunctionRegistry.FunctionInvocationWrapper;
import org.springframework.cloud.function.context.catalog.FunctionTypeUtilsTests.ReactiveFunction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -356,7 +357,6 @@ public class BeanFactoryAwareFunctionRegistryTests {
assertThat(result.getPayload()).isEqualTo("\"b2xsZWg=\"".getBytes());
}
@SuppressWarnings("rawtypes")
@Test
public void testMultipleValuesInOutputHandling() throws Exception {
FunctionCatalog catalog = this.configureCatalog(CollectionOutConfiguration.class);
@@ -421,6 +421,29 @@ public class BeanFactoryAwareFunctionRegistryTests {
assertThat(dateResult.getHeaders().get("accept")).isNull();
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testWithComplexHierarchyAndTypeConversion() {
FunctionCatalog catalog = this.configureCatalog(ReactiveFunctionImpl.class);
Function<Object, Flux> f = catalog.lookup("");
assertThat(f.apply(new GenericMessage("23")).blockFirst()).isEqualTo(23);
assertThat(f.apply(Flux.just("25")).blockFirst()).isEqualTo(25);
assertThat(f.apply(Flux.just(25)).blockFirst()).isEqualTo(25);
}
public interface ReactiveFunction<S, T> extends Function<Flux<S>, Flux<T>> {
}
@Component
@EnableAutoConfiguration
public static class ReactiveFunctionImpl implements ReactiveFunction<String, Integer> {
@Override
public Flux<Integer> apply(Flux<String> inFlux) {
return inFlux.map(v -> Integer.parseInt(v));
}
}
@SuppressWarnings("unchecked")
@EnableAutoConfiguration
public static class CollectionOutConfiguration {

View File

@@ -132,33 +132,12 @@ public class FunctionTypeUtilsTests {
}
@Test
public void foo() {
public void testWithComplexHierarchy() {
FunctionType type = FunctionType.of(FunctionTypeUtils.discoverFunctionTypeFromClass(ReactiveFunctionImpl.class));
assertThat(String.class).isAssignableFrom(type.getInputType());
assertThat(Integer.class).isAssignableFrom(type.getOutputType());
}
// @Test
// public void testInputTypeByIndex() throws Exception {
// Type functionType = getReturnType("function");
// Type inputType = FunctionTypeUtils.getInputType(functionType, 0);
// assertThat(inputType.getTypeName()).isEqualTo(String.class.getName());
// inputType = FunctionTypeUtils.getInputType(functionType, 1);
// assertThat(inputType.getTypeName()).isEqualTo(Integer.class.getName());
//
// functionType = getReturnType("multiInputOutputPublisherFunction");
// inputType = FunctionTypeUtils.getInputType(functionType, 0);
// System.out.println("Reactive: " + FunctionTypeUtils.isReactive(inputType));
// System.out.println("Reactive: " + FunctionTypeUtils.getPublisherType(inputType));
// System.out.println("Reactive: " + FunctionTypeUtils.getImmediateGenericType(inputType, 0));
// System.out.println(inputType);
//
// functionType = getReturnType("typelessFunction");
// inputType = FunctionTypeUtils.getInputType(functionType, 0);
// System.out.println(inputType);
// }
private static Function<String, Integer> function() {
return null;
}
@@ -246,6 +225,5 @@ public class FunctionTypeUtilsTests {
public Flux<Integer> apply(Flux<String> inFlux) {
return inFlux.map(v -> Integer.parseInt(v));
}
}
}