add support for function composition

This commit is contained in:
markfisher
2017-02-06 15:50:10 -05:00
parent 2075c649a5
commit 0a6dce951b
2 changed files with 17 additions and 2 deletions

View File

@@ -22,6 +22,7 @@ import java.util.function.Function;
import java.util.function.Supplier;
import org.springframework.cloud.function.registry.FunctionCatalog;
import org.springframework.util.StringUtils;
/**
* @author Dave Syer
@@ -49,9 +50,18 @@ public class InMemoryFunctionCatalog implements FunctionCatalog {
}
@Override
@SuppressWarnings("unchecked")
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T, R> Function<T, R> lookupFunction(String name) {
return (Function<T, R>) functions.get(name);
if (name.indexOf(',') == -1) {
return (Function<T, R>) functions.get(name);
}
String[] tokens = StringUtils.tokenizeToStringArray(name, ",");
Function function = null;
for (String token : tokens) {
Function next = lookupFunction(token);
function = (function == null) ? next : function.andThen(next);
}
return function;
}
@Override

View File

@@ -138,6 +138,11 @@ public class StreamConfiguration {
if (!StringUtils.hasText(functionName)) {
return ConditionOutcome.noMatch("no function name available");
}
if (functionName.indexOf(',') != -1) {
// for now we will just check the first, but later may support:
// supplier[,function]+ or [function,]+consumer
functionName = functionName.substring(0, functionName.indexOf(','));
}
Class<?> beanType = context.getBeanFactory().getType(functionName);
if (type.isAssignableFrom(beanType)) {
return ConditionOutcome.match(String.format("bean '%s' is a %s", functionName, type));