Refactor FunctionCatalog implementation

This commit is contained in:
Oleg Zhurakousky
2020-09-17 14:02:51 +02:00
parent 978a474c81
commit 72f05fc591
34 changed files with 1597 additions and 1643 deletions

View File

@@ -57,11 +57,11 @@ final class FunctionRSocketUtils {
registerRSocketForwardingFunctionIfNecessary(functionDefinition, functionCatalog, applicationContext);
FunctionProperties functionProperties = applicationContext.getBean(FunctionProperties.class);
String acceptContentType = functionProperties.getAccept();
String acceptContentType = functionProperties.getExpectedContentType();
if (!StringUtils.hasText(acceptContentType)) {
FunctionInvocationWrapper function = functionCatalog.lookup(functionDefinition);
Type functionType = function.getFunctionType();
Type outputType = FunctionTypeUtils.getOutputType(functionType, 0);
//Type functionType = function.getFunctionType();
Type outputType = function.getOutputType();
if (outputType instanceof Class && String.class.isAssignableFrom((Class<?>) outputType)) {
acceptContentType = "text/plain";
}

View File

@@ -16,7 +16,6 @@
package org.springframework.cloud.function.rsocket;
import java.lang.reflect.Type;
import java.util.function.Function;
import io.rsocket.frame.FrameType;
@@ -74,7 +73,7 @@ class RSocketListenerFunction implements Function<Message<Flux<byte[]>>, Publish
Flux<?> dataFlux =
messageToProcess.getPayload()
.map((payload) -> MessageBuilder.createMessage(payload, messageToProcess.getHeaders()));
if (isFunctionInputReactive(this.targetFunction.getFunctionType())) {
if (FunctionTypeUtils.isPublisher(this.targetFunction.getInputType())) {
dataFlux = dataFlux.transform((Function) this.targetFunction);
}
else {
@@ -92,12 +91,12 @@ class RSocketListenerFunction implements Function<Message<Flux<byte[]>>, Publish
Flux<?> dataFlux =
messageToProcess.getPayload()
.map((payload) -> MessageBuilder.createMessage(payload, messageToProcess.getHeaders()));
if (isFunctionInputReactive(this.targetFunction.getFunctionType())) {
if (this.targetFunction.getInputType() != null && FunctionTypeUtils.isPublisher(this.targetFunction.getInputType())) {
dataFlux = dataFlux.transform((Function) this.targetFunction);
}
else {
dataFlux = dataFlux.flatMap((data) -> {
Object result = this.targetFunction.apply(data);
Object result = this.targetFunction.isSupplier() ? this.targetFunction.apply(null) : this.targetFunction.apply(data);
return result instanceof Publisher<?>
? (Publisher<Message<byte[]>>) result
: Mono.just((Message<byte[]>) result);
@@ -105,10 +104,4 @@ class RSocketListenerFunction implements Function<Message<Flux<byte[]>>, Publish
}
return dataFlux.cast(Message.class).map(Message::getPayload);
}
private static boolean isFunctionInputReactive(Type functionType) {
Type inputType = FunctionTypeUtils.getInputType(functionType, 0);
return FunctionTypeUtils.isPublisher(inputType);
}
}

View File

@@ -73,7 +73,7 @@ public class RSocketAutoConfigurationTests {
}
@Test
public void testImperativeFunctionAsRequestReplyWithDefinitionExplicitAccept() {
public void testImperativeFunctionAsRequestReplyWithDefinitionExplicitExpectedOutputCt() {
int port = SocketUtils.findAvailableTcpPort();
try (
ConfigurableApplicationContext applicationContext =
@@ -81,7 +81,7 @@ public class RSocketAutoConfigurationTests {
.web(WebApplicationType.NONE)
.run("--logging.level.org.springframework.cloud.function=DEBUG",
"--spring.cloud.function.definition=uppercase",
"--spring.cloud.function.accept=application/json",
"--spring.cloud.function.expected-content-type=application/json",
"--spring.rsocket.server.port=" + port);
) {
RSocketRequester.Builder rsocketRequesterBuilder =