GH-550 Add support for wrapping value into Message if necessary
Resolves #550
This commit is contained in:
@@ -761,6 +761,10 @@ public class SimpleFunctionRegistry implements FunctionRegistry, FunctionInspect
|
|||||||
if (rawType instanceof ParameterizedType) {
|
if (rawType instanceof ParameterizedType) {
|
||||||
rawType = ((ParameterizedType) rawType).getRawType();
|
rawType = ((ParameterizedType) rawType).getRawType();
|
||||||
}
|
}
|
||||||
|
if (value != null && !(value instanceof Message) && FunctionTypeUtils.isMessage(type)) {
|
||||||
|
value = new GenericMessage<>(value);
|
||||||
|
convertedValue = value;
|
||||||
|
}
|
||||||
if (value instanceof Message<?>) { // see AWS adapter with Optional payload
|
if (value instanceof Message<?>) { // see AWS adapter with Optional payload
|
||||||
if (messageNeedsConversion(rawType, (Message<?>) value)) {
|
if (messageNeedsConversion(rawType, (Message<?>) value)) {
|
||||||
convertedValue = FunctionTypeUtils.isTypeCollection(type)
|
convertedValue = FunctionTypeUtils.isTypeCollection(type)
|
||||||
|
|||||||
@@ -471,6 +471,40 @@ public class BeanFactoryAwareFunctionRegistryTests {
|
|||||||
registry.register(e);
|
registry.register(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||||
|
@Test
|
||||||
|
public void testValueWrappedInMessageIfNecessary() {
|
||||||
|
FunctionCatalog catalog = this.configureCatalog(PojoToMessageFunctionCompositionConfiguration.class);
|
||||||
|
Function f = catalog.lookup("uppercase|echo");
|
||||||
|
assertThat(f.apply("hello")).isEqualTo("HELLO");
|
||||||
|
f = catalog.lookup("toJson|uppercasePerson");
|
||||||
|
assertThat(f.apply("Bubbles")).isEqualTo("BUBBLES");
|
||||||
|
}
|
||||||
|
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
public static class PojoToMessageFunctionCompositionConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Function<String, String> uppercase() {
|
||||||
|
return v -> v.toUpperCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Function<Message<String>, String> echo() {
|
||||||
|
return v -> v.getPayload();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Function<String, String> toJson() {
|
||||||
|
return v -> "{\"id\":1, \"name\":\"" + v + "\"}";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Function<Message<Person>, String> uppercasePerson() {
|
||||||
|
return v -> v.getPayload().getName().toUpperCase();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@EnableAutoConfiguration
|
@EnableAutoConfiguration
|
||||||
public static class EmptyConfiguration {
|
public static class EmptyConfiguration {
|
||||||
|
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ public class SimpleFunctionRegistryTests {
|
|||||||
catalog.register(registration);
|
catalog.register(registration);
|
||||||
|
|
||||||
FunctionInvocationWrapper lookedUpFunction = catalog.lookup("hello");
|
FunctionInvocationWrapper lookedUpFunction = catalog.lookup("hello");
|
||||||
assertThat(lookedUpFunction).isNotNull(); // becouse we only have one and can look it up with any name
|
assertThat(lookedUpFunction).isNotNull(); // because we only have one and can look it up with any name
|
||||||
FunctionRegistration<TestFunction> registration2 = new FunctionRegistration<>(
|
FunctionRegistration<TestFunction> registration2 = new FunctionRegistration<>(
|
||||||
function, "foo2").type(FunctionType.of(TestFunction.class));
|
function, "foo2").type(FunctionType.of(TestFunction.class));
|
||||||
catalog.register(registration2);
|
catalog.register(registration2);
|
||||||
|
|||||||
Reference in New Issue
Block a user