Use unique function if there is only one and name not provided

If the user doesn't ask for a specific function, but there is only
one possible choice, we can just use that.
This commit is contained in:
Dave Syer
2017-11-24 12:41:10 +09:00
parent 8459fb4e30
commit 1ee517ab01
2 changed files with 78 additions and 8 deletions

View File

@@ -245,8 +245,12 @@ public class ContextFunctionCatalogAutoConfiguration {
return lookup.get(name);
}
String[] stages = StringUtils.tokenizeToStringArray(name, ",");
Object function = lookup(stages[0],
!hasInput || stages.length == 1 ? lookup : this.functions);
Map<String, Object> source = !hasInput || stages.length <= 1 ? lookup
: this.functions;
if (stages.length == 0 && source.size() == 1) {
stages = new String[] { source.keySet().iterator().next() };
}
Object function = lookup(stages[0], source);
if (function == null) {
return null;
}
@@ -494,12 +498,13 @@ public class ContextFunctionCatalogAutoConfiguration {
int index = paramType.isOutput() ? 1 : 0;
if (source instanceof StandardMethodMetadata) {
// Standard @Bean metadata
Type beanType = ((StandardMethodMetadata) source)
.getIntrospectedMethod().getGenericReturnType();
Type beanType = ((StandardMethodMetadata) source).getIntrospectedMethod()
.getGenericReturnType();
if (beanType instanceof ParameterizedType) {
ParameterizedType type = (ParameterizedType) beanType;
param = extractType(type, paramType, index);
} else {
ParameterizedType type = (ParameterizedType) beanType;
param = extractType(type, paramType, index);
}
else {
param = findTypeFromBeanClass((Class<?>) beanType, paramType);
}
}

View File

@@ -16,7 +16,11 @@
package org.springframework.cloud.function.context;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.junit.Test;
@@ -44,7 +48,14 @@ public class BeanFactoryFunctionCatalogTests {
}
@Test
public void compose() {
public void lookupFunctionWithEmptyName() {
processor.register(new FunctionRegistration<>(new Foos()).names("foos"));
Function<Flux<Integer>, Flux<String>> foos = processor.lookupFunction("");
assertThat(foos.apply(Flux.just(2)).blockFirst()).isEqualTo("4");
}
@Test
public void composeFunction() {
processor.register(new FunctionRegistration<>(new Foos()).names("foos"));
processor.register(new FunctionRegistration<>(new Bars()).names("bars"));
Function<Flux<Integer>, Flux<String>> foos = processor
@@ -52,6 +63,60 @@ public class BeanFactoryFunctionCatalogTests {
assertThat(foos.apply(Flux.just(2)).blockFirst()).isEqualTo("Hello 4");
}
@Test
public void composeSupplier() {
processor.register(new FunctionRegistration<>(new Source()).names("numbers"));
processor.register(new FunctionRegistration<>(new Foos()).names("foos"));
Supplier<Flux<String>> foos = processor.lookupSupplier("numbers,foos");
assertThat(foos.get().blockFirst()).isEqualTo("6");
}
@Test
public void composeUniqueSupplier() {
processor.register(new FunctionRegistration<>(new Source()).names("numbers"));
Supplier<Flux<Integer>> foos = processor.lookupSupplier("");
assertThat(foos.get().blockFirst()).isEqualTo(3);
}
@Test
public void composeConsumer() {
processor.register(new FunctionRegistration<>(new Foos()).names("foos"));
Sink sink = new Sink();
processor.register(new FunctionRegistration<>(sink).names("sink"));
Consumer<Flux<Integer>> foos = processor.lookupConsumer("foos,sink");
foos.accept(Flux.just(2));
assertThat(sink.values).contains("4");
}
@Test
public void composeUniqueConsumer() {
Sink sink = new Sink();
processor.register(new FunctionRegistration<>(sink).names("sink"));
Consumer<Flux<String>> foos = processor.lookupConsumer("");
foos.accept(Flux.just("2"));
assertThat(sink.values).contains("2");
}
protected static class Source implements Supplier<Integer> {
@Override
public Integer get() {
return 3;
}
}
protected static class Sink implements Consumer<String> {
private List<String> values = new ArrayList<>();
@Override
public void accept(String value) {
values.add(value);
}
}
protected static class Foos implements Function<Integer, String> {
@Override