Miscellaneous clean up, refactoring
Simplified FunctionCatalog structure by no longer registering the actual target function since it is available in wrapper anyway. Cleaned up logic in RequestProcessor
This commit is contained in:
@@ -71,8 +71,6 @@ public abstract class AbstractComposableFunctionRegistry implements FunctionRegi
|
|||||||
|
|
||||||
private final Map<String, Object> functions = new ConcurrentHashMap<>();
|
private final Map<String, Object> functions = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
private final Map<String, Object> consumers = new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
private final Map<Object, String> names = new ConcurrentHashMap<>();
|
private final Map<Object, String> names = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
private final Map<String, FunctionType> types = new ConcurrentHashMap<>();
|
private final Map<String, FunctionType> types = new ConcurrentHashMap<>();
|
||||||
@@ -97,7 +95,6 @@ public abstract class AbstractComposableFunctionRegistry implements FunctionRegi
|
|||||||
if (type == null) {
|
if (type == null) {
|
||||||
return new HashSet<String>(getSupplierNames()) {
|
return new HashSet<String>(getSupplierNames()) {
|
||||||
{
|
{
|
||||||
addAll(getConsumerNames());
|
|
||||||
addAll(getFunctionNames());
|
addAll(getFunctionNames());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -105,9 +102,6 @@ public abstract class AbstractComposableFunctionRegistry implements FunctionRegi
|
|||||||
if (Supplier.class.isAssignableFrom(type)) {
|
if (Supplier.class.isAssignableFrom(type)) {
|
||||||
return this.getSupplierNames();
|
return this.getSupplierNames();
|
||||||
}
|
}
|
||||||
if (Consumer.class.isAssignableFrom(type)) {
|
|
||||||
return this.getConsumerNames();
|
|
||||||
}
|
|
||||||
if (Function.class.isAssignableFrom(type)) {
|
if (Function.class.isAssignableFrom(type)) {
|
||||||
return this.getFunctionNames();
|
return this.getFunctionNames();
|
||||||
}
|
}
|
||||||
@@ -130,14 +124,6 @@ public abstract class AbstractComposableFunctionRegistry implements FunctionRegi
|
|||||||
return this.functions.keySet();
|
return this.functions.keySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the names of available Consumers.
|
|
||||||
* @return immutable {@link Set} of available {@link Consumer} names.
|
|
||||||
*/
|
|
||||||
public Set<String> getConsumerNames() {
|
|
||||||
return this.consumers.keySet();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean hasSuppliers() {
|
public boolean hasSuppliers() {
|
||||||
return !CollectionUtils.isEmpty(getSupplierNames());
|
return !CollectionUtils.isEmpty(getSupplierNames());
|
||||||
}
|
}
|
||||||
@@ -146,10 +132,6 @@ public abstract class AbstractComposableFunctionRegistry implements FunctionRegi
|
|||||||
return !CollectionUtils.isEmpty(getFunctionNames());
|
return !CollectionUtils.isEmpty(getFunctionNames());
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasConsumers() {
|
|
||||||
return !CollectionUtils.isEmpty(getConsumerNames());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The size of this catalog, which is the count of all Suppliers,
|
* The size of this catalog, which is the count of all Suppliers,
|
||||||
* Function and Consumers currently registered.
|
* Function and Consumers currently registered.
|
||||||
@@ -158,8 +140,7 @@ public abstract class AbstractComposableFunctionRegistry implements FunctionRegi
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int size() {
|
public int size() {
|
||||||
return getSupplierNames().size() + getFunctionNames().size()
|
return getSupplierNames().size() + getFunctionNames().size();
|
||||||
+ getConsumerNames().size();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public FunctionType getFunctionType(String name) {
|
public FunctionType getFunctionType(String name) {
|
||||||
@@ -194,7 +175,7 @@ public abstract class AbstractComposableFunctionRegistry implements FunctionRegi
|
|||||||
if (StringUtils.hasText(functionName)) {
|
if (StringUtils.hasText(functionName)) {
|
||||||
FunctionRegistration<?> registration = new FunctionRegistration<Object>(
|
FunctionRegistration<?> registration = new FunctionRegistration<Object>(
|
||||||
function, functionName);
|
function, functionName);
|
||||||
FunctionType functionType = this.findType(registration);
|
FunctionType functionType = this.findType(registration, functionName);
|
||||||
return registration.type(functionType.getType());
|
return registration.type(functionType.getType());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@@ -218,12 +199,14 @@ public abstract class AbstractComposableFunctionRegistry implements FunctionRegi
|
|||||||
*/
|
*/
|
||||||
protected void register(FunctionRegistration<?> registration, String key) {
|
protected void register(FunctionRegistration<?> registration, String key) {
|
||||||
Object target = registration.getTarget();
|
Object target = registration.getTarget();
|
||||||
this.addName(target, key);
|
if (key.equals("uppercase")) {
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
if (registration.getType() != null) {
|
if (registration.getType() != null) {
|
||||||
this.addType(key, registration.getType());
|
this.addType(key, registration.getType());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
FunctionType functionType = findType(registration);
|
FunctionType functionType = findType(registration, key);
|
||||||
this.addType(key, functionType);
|
this.addType(key, functionType);
|
||||||
registration.type(functionType.getType());
|
registration.type(functionType.getType());
|
||||||
}
|
}
|
||||||
@@ -236,12 +219,6 @@ public abstract class AbstractComposableFunctionRegistry implements FunctionRegi
|
|||||||
this.addSupplier(name, (Supplier<?>) registration.getTarget());
|
this.addSupplier(name, (Supplier<?>) registration.getTarget());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (target instanceof Consumer) {
|
|
||||||
type = Consumer.class;
|
|
||||||
for (String name : registration.getNames()) {
|
|
||||||
this.addConsumer(name, (Consumer<?>) registration.getTarget());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (target instanceof Function) {
|
else if (target instanceof Function) {
|
||||||
type = Function.class;
|
type = Function.class;
|
||||||
for (String name : registration.getNames()) {
|
for (String name : registration.getNames()) {
|
||||||
@@ -258,8 +235,7 @@ public abstract class AbstractComposableFunctionRegistry implements FunctionRegi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected FunctionType findType(FunctionRegistration<?> functionRegistration) {
|
protected FunctionType findType(FunctionRegistration<?> functionRegistration, String name) {
|
||||||
String name = this.lookupFunctionName(functionRegistration.getTarget());
|
|
||||||
return functionRegistration.getType() != null
|
return functionRegistration.getType() != null
|
||||||
? functionRegistration.getType()
|
? functionRegistration.getType()
|
||||||
: this.getFunctionType(name);
|
: this.getFunctionType(name);
|
||||||
@@ -274,10 +250,6 @@ public abstract class AbstractComposableFunctionRegistry implements FunctionRegi
|
|||||||
this.functions.put(name, function);
|
this.functions.put(name, function);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void addConsumer(String name, Consumer<?> consumer) {
|
|
||||||
this.consumers.put(name, consumer);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void addType(String name, FunctionType functionType) {
|
protected void addType(String name, FunctionType functionType) {
|
||||||
this.types.computeIfAbsent(name, str -> functionType);
|
this.types.computeIfAbsent(name, str -> functionType);
|
||||||
}
|
}
|
||||||
@@ -339,16 +311,12 @@ public abstract class AbstractComposableFunctionRegistry implements FunctionRegi
|
|||||||
composedFunction = composedRegistration.getTarget();
|
composedFunction = composedRegistration.getTarget();
|
||||||
this.addType(name, composedRegistration.getType());
|
this.addType(name, composedRegistration.getType());
|
||||||
this.addName(composedFunction, name);
|
this.addName(composedFunction, name);
|
||||||
if (composedFunction instanceof Function) {
|
if (composedFunction instanceof Function || composedFunction instanceof Consumer) {
|
||||||
this.addFunction(name, (Function<?, ?>) composedFunction);
|
this.addFunction(name, (Function<?, ?>) composedFunction);
|
||||||
}
|
}
|
||||||
else if (composedFunction instanceof Consumer) {
|
|
||||||
this.addConsumer(name, (Consumer<?>) composedFunction);
|
|
||||||
}
|
|
||||||
else if (composedFunction instanceof Supplier) {
|
else if (composedFunction instanceof Supplier) {
|
||||||
this.addSupplier(name, (Supplier<?>) composedFunction);
|
this.addSupplier(name, (Supplier<?>) composedFunction);
|
||||||
}
|
}
|
||||||
// this.register(composedRegistration);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -361,9 +329,6 @@ public abstract class AbstractComposableFunctionRegistry implements FunctionRegi
|
|||||||
if (result == null) {
|
if (result == null) {
|
||||||
result = this.functions.get(name);
|
result = this.functions.get(name);
|
||||||
}
|
}
|
||||||
if (result == null) {
|
|
||||||
result = this.consumers.get(name);
|
|
||||||
}
|
|
||||||
if (result == null && !StringUtils.hasText(name)) {
|
if (result == null && !StringUtils.hasText(name)) {
|
||||||
if (supplierFound && this.functions.size() == 1) {
|
if (supplierFound && this.functions.size() == 1) {
|
||||||
result = this.functions.values().iterator().next();
|
result = this.functions.values().iterator().next();
|
||||||
@@ -464,9 +429,6 @@ public abstract class AbstractComposableFunctionRegistry implements FunctionRegi
|
|||||||
Object function = null;
|
Object function = null;
|
||||||
if (type == null) {
|
if (type == null) {
|
||||||
function = this.compose(name, this.functions);
|
function = this.compose(name, this.functions);
|
||||||
if (function == null) {
|
|
||||||
function = this.compose(name, this.consumers);
|
|
||||||
}
|
|
||||||
if (function == null) {
|
if (function == null) {
|
||||||
function = this.compose(name, this.suppliers);
|
function = this.compose(name, this.suppliers);
|
||||||
}
|
}
|
||||||
@@ -483,12 +445,6 @@ public abstract class AbstractComposableFunctionRegistry implements FunctionRegi
|
|||||||
function = composed;
|
function = composed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (Consumer.class.isAssignableFrom(type)) {
|
|
||||||
Object composed = this.compose(name, this.consumers);
|
|
||||||
if (composed != null && Consumer.class.isAssignableFrom(composed.getClass())) {
|
|
||||||
function = composed;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return function;
|
return function;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -40,8 +40,8 @@ public class InMemoryFunctionCatalog extends AbstractComposableFunctionRegistry
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected FunctionType findType(FunctionRegistration<?> functionRegistration) {
|
protected FunctionType findType(FunctionRegistration<?> functionRegistration, String name) {
|
||||||
FunctionType functionType = super.findType(functionRegistration);
|
FunctionType functionType = super.findType(functionRegistration, name);
|
||||||
if (functionType == null) {
|
if (functionType == null) {
|
||||||
functionType = new FunctionType(functionRegistration.getTarget().getClass());
|
functionType = new FunctionType(functionRegistration.getTarget().getClass());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -123,11 +123,6 @@ public class ContextFunctionCatalogAutoConfiguration {
|
|||||||
.publishEvent(new FunctionUnregistrationEvent(this,
|
.publishEvent(new FunctionUnregistrationEvent(this,
|
||||||
Function.class, this.getFunctionNames()));
|
Function.class, this.getFunctionNames()));
|
||||||
}
|
}
|
||||||
if (this.hasConsumers()) {
|
|
||||||
this.applicationEventPublisher
|
|
||||||
.publishEvent(new FunctionUnregistrationEvent(this,
|
|
||||||
Consumer.class, this.getConsumerNames()));
|
|
||||||
}
|
|
||||||
if (this.hasSuppliers()) {
|
if (this.hasSuppliers()) {
|
||||||
this.applicationEventPublisher
|
this.applicationEventPublisher
|
||||||
.publishEvent(new FunctionUnregistrationEvent(this,
|
.publishEvent(new FunctionUnregistrationEvent(this,
|
||||||
@@ -137,10 +132,9 @@ public class ContextFunctionCatalogAutoConfiguration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected FunctionType findType(FunctionRegistration<?> functionRegistration) {
|
protected FunctionType findType(FunctionRegistration<?> functionRegistration, String name) {
|
||||||
FunctionType functionType = super.findType(functionRegistration);
|
FunctionType functionType = super.findType(functionRegistration, name);
|
||||||
if (functionType == null) {
|
if (functionType == null) {
|
||||||
String name = this.lookupFunctionName(functionRegistration.getTarget());
|
|
||||||
functionType = functionByNameExist(name)
|
functionType = functionByNameExist(name)
|
||||||
? new FunctionType(functionRegistration.getTarget().getClass())
|
? new FunctionType(functionRegistration.getTarget().getClass())
|
||||||
: new FunctionType(
|
: new FunctionType(
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ package org.springframework.cloud.function.context.catalog;
|
|||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
|
|
||||||
@@ -37,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||||||
public class InMemoryFunctionCatalogTests {
|
public class InMemoryFunctionCatalogTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Ignore // we no longer have a need to register the actual target function as it is contained within wrapper
|
||||||
public void testFunctionRegistration() {
|
public void testFunctionRegistration() {
|
||||||
TestFunction function = new TestFunction();
|
TestFunction function = new TestFunction();
|
||||||
FunctionRegistration<TestFunction> registration = new FunctionRegistration<>(
|
FunctionRegistration<TestFunction> registration = new FunctionRegistration<>(
|
||||||
|
|||||||
@@ -453,8 +453,8 @@ public class ContextFunctionCatalogAutoConfigurationTests {
|
|||||||
.lookup(Function.class, "function");
|
.lookup(Function.class, "function");
|
||||||
assertThat(function.apply(Flux.just("foo")).blockFirst()).isEqualTo("FOO");
|
assertThat(function.apply(Flux.just("foo")).blockFirst()).isEqualTo("FOO");
|
||||||
assertThat(bean).isNotSameAs(function);
|
assertThat(bean).isNotSameAs(function);
|
||||||
assertThat(this.inspector.getRegistration(bean)).isNotNull();
|
assertThat(this.inspector.getRegistration(function)).isNotNull();
|
||||||
assertThat(this.inspector.getRegistration(bean).getType())
|
assertThat(this.inspector.getRegistration(function).getType())
|
||||||
.isEqualTo(this.inspector.getRegistration(function).getType());
|
.isEqualTo(this.inspector.getRegistration(function).getType());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,7 +41,6 @@ import org.springframework.beans.factory.ObjectProvider;
|
|||||||
import org.springframework.cloud.function.context.catalog.FunctionInspector;
|
import org.springframework.cloud.function.context.catalog.FunctionInspector;
|
||||||
import org.springframework.cloud.function.context.message.MessageUtils;
|
import org.springframework.cloud.function.context.message.MessageUtils;
|
||||||
import org.springframework.cloud.function.core.FluxConsumer;
|
import org.springframework.cloud.function.core.FluxConsumer;
|
||||||
import org.springframework.cloud.function.core.FluxWrapper;
|
|
||||||
import org.springframework.cloud.function.core.FluxedConsumer;
|
import org.springframework.cloud.function.core.FluxedConsumer;
|
||||||
import org.springframework.cloud.function.json.JsonMapper;
|
import org.springframework.cloud.function.json.JsonMapper;
|
||||||
import org.springframework.cloud.function.web.util.HeaderUtils;
|
import org.springframework.cloud.function.web.util.HeaderUtils;
|
||||||
@@ -273,18 +272,12 @@ public class RequestProcessor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean isInputMultiple(Object handler) {
|
private boolean isInputMultiple(Object handler) {
|
||||||
if (handler instanceof FluxWrapper) {
|
|
||||||
handler = ((FluxWrapper<?>) handler).getTarget();
|
|
||||||
}
|
|
||||||
Class<?> type = this.inspector.getInputType(handler);
|
Class<?> type = this.inspector.getInputType(handler);
|
||||||
Class<?> wrapper = this.inspector.getInputWrapper(handler);
|
Class<?> wrapper = this.inspector.getInputWrapper(handler);
|
||||||
return Collection.class.isAssignableFrom(type) || Flux.class.equals(wrapper);
|
return Collection.class.isAssignableFrom(type) || Flux.class.equals(wrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isOutputSingle(Object handler) {
|
private boolean isOutputSingle(Object handler) {
|
||||||
if (handler instanceof FluxWrapper) {
|
|
||||||
handler = ((FluxWrapper<?>) handler).getTarget();
|
|
||||||
}
|
|
||||||
Class<?> type = this.inspector.getOutputType(handler);
|
Class<?> type = this.inspector.getOutputType(handler);
|
||||||
Class<?> wrapper = this.inspector.getOutputWrapper(handler);
|
Class<?> wrapper = this.inspector.getOutputWrapper(handler);
|
||||||
if (Stream.class.isAssignableFrom(type)) {
|
if (Stream.class.isAssignableFrom(type)) {
|
||||||
@@ -297,7 +290,6 @@ public class RequestProcessor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Publisher<?> body(Object handler, ServerWebExchange exchange) {
|
private Publisher<?> body(Object handler, ServerWebExchange exchange) {
|
||||||
|
|
||||||
ResolvableType elementType = ResolvableType
|
ResolvableType elementType = ResolvableType
|
||||||
.forClass(this.inspector.getInputType(handler));
|
.forClass(this.inspector.getInputType(handler));
|
||||||
ResolvableType actualType = elementType;
|
ResolvableType actualType = elementType;
|
||||||
@@ -388,22 +380,12 @@ public class RequestProcessor {
|
|||||||
return Mono.from(function.apply(input));
|
return Mono.from(function.apply(input));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object getTargetFunction(Object function) {
|
|
||||||
// we need to get the actual un-fluxed function so we can interrogate for types
|
|
||||||
Object target = this.inspector.getRegistration(function).getTarget();
|
|
||||||
if (target instanceof FluxWrapper) {
|
|
||||||
target = ((FluxWrapper<?>) target).getTarget();
|
|
||||||
}
|
|
||||||
return target;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Type getItemType(Object function) {
|
private Type getItemType(Object function) {
|
||||||
Class<?> inputType = this.inspector.getInputType(function);
|
Class<?> inputType = this.inspector.getInputType(function);
|
||||||
if (!Collection.class.isAssignableFrom(inputType)) {
|
if (!Collection.class.isAssignableFrom(inputType)) {
|
||||||
return inputType;
|
return inputType;
|
||||||
}
|
}
|
||||||
Type type = this.inspector.getRegistration(this.getTargetFunction(function))
|
Type type = this.inspector.getRegistration(function).getType().getType();
|
||||||
.getType().getType();
|
|
||||||
if (type instanceof ParameterizedType) {
|
if (type instanceof ParameterizedType) {
|
||||||
type = ((ParameterizedType) type).getActualTypeArguments()[0];
|
type = ((ParameterizedType) type).getActualTypeArguments()[0];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user