Minor polishing to accomodate edge cases in s-c-stream
This commit is contained in:
@@ -123,6 +123,7 @@ public class BeanFactoryAwareFunctionRegistry
|
|||||||
|
|
||||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||||
@Override
|
@Override
|
||||||
|
//TODO do we really need to do that given we no longer do the same for other gunctions?
|
||||||
public void afterSingletonsInstantiated() {
|
public void afterSingletonsInstantiated() {
|
||||||
Map<String, FunctionRegistration> beansOfType = this.applicationContext
|
Map<String, FunctionRegistration> beansOfType = this.applicationContext
|
||||||
.getBeansOfType(FunctionRegistration.class);
|
.getBeansOfType(FunctionRegistration.class);
|
||||||
@@ -190,7 +191,9 @@ public class BeanFactoryAwareFunctionRegistry
|
|||||||
else {
|
else {
|
||||||
if (StringUtils.isEmpty(definition)) {
|
if (StringUtils.isEmpty(definition)) {
|
||||||
String[] functionNames = this.applicationContext.getBeanNamesForType(Function.class);
|
String[] functionNames = this.applicationContext.getBeanNamesForType(Function.class);
|
||||||
Assert.notEmpty(functionNames, "Can't find any functions in BeanFactory");
|
if (ObjectUtils.isEmpty(functionNames)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
Assert.isTrue(functionNames.length == 1, "Found more then one function in BeanFactory");
|
Assert.isTrue(functionNames.length == 1, "Found more then one function in BeanFactory");
|
||||||
definition = functionNames[0];
|
definition = functionNames[0];
|
||||||
}
|
}
|
||||||
@@ -349,12 +352,6 @@ public class BeanFactoryAwareFunctionRegistry
|
|||||||
return target;
|
return target;
|
||||||
}
|
}
|
||||||
|
|
||||||
// public boolean isMultipleOutput() {
|
|
||||||
//
|
|
||||||
// Type type = FunctionTypeUtils.getInputType(functionType, 0);
|
|
||||||
// return FunctionTypeUtils.isFlux(type);
|
|
||||||
// }
|
|
||||||
|
|
||||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||||
private Object invokeFunction(Object input) {
|
private Object invokeFunction(Object input) {
|
||||||
if (target instanceof FunctionInvocationWrapper || target instanceof Function) {
|
if (target instanceof FunctionInvocationWrapper || target instanceof Function) {
|
||||||
@@ -482,7 +479,7 @@ public class BeanFactoryAwareFunctionRegistry
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Publisher<?> convertOutputPublisherIfNecessary(Publisher<?> publisher, String... acceptedOutputMimeTypes) {
|
private Publisher<?> convertOutputPublisherIfNecessary(Publisher<?> publisher, String... acceptedOutputMimeTypes) {
|
||||||
System.out.println("Converting output publisher");
|
logger.info("Applying type conversion on output Publisher");
|
||||||
Publisher<?> result = publisher instanceof Mono
|
Publisher<?> result = publisher instanceof Mono
|
||||||
? Mono.from(publisher).map(value -> this.convertOutputValueIfNecessary(value, acceptedOutputMimeTypes))
|
? Mono.from(publisher).map(value -> this.convertOutputValueIfNecessary(value, acceptedOutputMimeTypes))
|
||||||
: Flux.from(publisher).map(value -> this.convertOutputValueIfNecessary(value, acceptedOutputMimeTypes));
|
: Flux.from(publisher).map(value -> this.convertOutputValueIfNecessary(value, acceptedOutputMimeTypes));
|
||||||
@@ -490,7 +487,7 @@ public class BeanFactoryAwareFunctionRegistry
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Publisher<?> convertInputPublisherIfNecessary(Publisher<?> publisher, Type type) {
|
private Publisher<?> convertInputPublisherIfNecessary(Publisher<?> publisher, Type type) {
|
||||||
System.out.println("Converting publisher");
|
logger.info("Applying type conversion on input Publisher");
|
||||||
Publisher<?> result = publisher instanceof Mono
|
Publisher<?> result = publisher instanceof Mono
|
||||||
? Mono.from(publisher).map(value -> this.convertInputValueIfNecessary(value, type))
|
? Mono.from(publisher).map(value -> this.convertInputValueIfNecessary(value, type))
|
||||||
: Flux.from(publisher).map(value -> this.convertInputValueIfNecessary(value, type));
|
: Flux.from(publisher).map(value -> this.convertInputValueIfNecessary(value, type));
|
||||||
@@ -498,7 +495,7 @@ public class BeanFactoryAwareFunctionRegistry
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Object convertInputValueIfNecessary(Object value, Type type) {
|
private Object convertInputValueIfNecessary(Object value, Type type) {
|
||||||
System.out.println("Converting value");
|
logger.info("Applying type conversion on actual value ");
|
||||||
Object convertedValue = value;
|
Object convertedValue = value;
|
||||||
if (FunctionTypeUtils.isMultipleArgumentsHolder(value)) {
|
if (FunctionTypeUtils.isMultipleArgumentsHolder(value)) {
|
||||||
int inputCount = FunctionTypeUtils.getInputCount(functionType);
|
int inputCount = FunctionTypeUtils.getInputCount(functionType);
|
||||||
@@ -516,12 +513,12 @@ public class BeanFactoryAwareFunctionRegistry
|
|||||||
else {
|
else {
|
||||||
// this needs revisiting as the type is not always Class (think really complex types)
|
// this needs revisiting as the type is not always Class (think really complex types)
|
||||||
Type rawType = FunctionTypeUtils.unwrapActualTypeByIndex(type, 0);
|
Type rawType = FunctionTypeUtils.unwrapActualTypeByIndex(type, 0);
|
||||||
if (/*!(rawType instanceof Class<?>) && */rawType instanceof ParameterizedType) {
|
if (rawType instanceof ParameterizedType) {
|
||||||
rawType = ((ParameterizedType) rawType).getRawType();
|
rawType = ((ParameterizedType) rawType).getRawType();
|
||||||
}
|
}
|
||||||
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 = messageConverter.fromMessage((Message<?>) value, (Class<?>) rawType, type);
|
convertedValue = messageConverter.fromMessage((Message<?>) value, (Class<?>) rawType);
|
||||||
if (FunctionTypeUtils.isMessage(type)) {
|
if (FunctionTypeUtils.isMessage(type)) {
|
||||||
convertedValue = MessageBuilder.withPayload(convertedValue).copyHeaders(((Message<?>) value).getHeaders()).build();
|
convertedValue = MessageBuilder.withPayload(convertedValue).copyHeaders(((Message<?>) value).getHeaders()).build();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,16 +35,20 @@ import org.springframework.util.ObjectUtils;
|
|||||||
* @author Oleg Zhurakousky
|
* @author Oleg Zhurakousky
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
final class FunctionTypeUtils {
|
public final class FunctionTypeUtils {
|
||||||
|
|
||||||
private FunctionTypeUtils() {
|
private FunctionTypeUtils() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public static Type getFunctionType(Object function, FunctionInspector inspector) {
|
||||||
* Will extract the relevant type (e.g., for type conversion purposes). Useful to extract
|
FunctionRegistration<?> registration = inspector.getRegistration(function);
|
||||||
* types wrapped in Publisher and/or Message.
|
if (registration != null) {
|
||||||
*/
|
return registration.getType().getType();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public static Type unwrapActualTypeByIndex(Type type, int index) {
|
public static Type unwrapActualTypeByIndex(Type type, int index) {
|
||||||
if (isMessage(type) || isPublisher(type)) {
|
if (isMessage(type) || isPublisher(type)) {
|
||||||
if (isPublisher(type)) {
|
if (isPublisher(type)) {
|
||||||
@@ -144,10 +148,9 @@ final class FunctionTypeUtils {
|
|||||||
|
|
||||||
|
|
||||||
public static boolean isReactive(Type type) {
|
public static boolean isReactive(Type type) {
|
||||||
if (type instanceof ParameterizedType) {
|
Class<?> rawType = type instanceof ParameterizedType
|
||||||
return Publisher.class.isAssignableFrom(((Class<?>) ((ParameterizedType) type).getRawType()));
|
? (Class<?>) ((ParameterizedType) type).getRawType() : (type instanceof Class<?> ? (Class<?>) type : Object.class);
|
||||||
}
|
return Publisher.class.isAssignableFrom(rawType);
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isConsumer(Type type) {
|
public static boolean isConsumer(Type type) {
|
||||||
|
|||||||
@@ -90,15 +90,10 @@ public class ContextFunctionCatalogAutoConfiguration {
|
|||||||
|
|
||||||
static final String PREFERRED_MAPPER_PROPERTY = "spring.http.converters.preferred-json-mapper";
|
static final String PREFERRED_MAPPER_PROPERTY = "spring.http.converters.preferred-json-mapper";
|
||||||
|
|
||||||
// @Bean
|
|
||||||
public FunctionRegistry functionCatalog() {
|
|
||||||
return new BeanFactoryFunctionCatalog();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public FunctionRegistry functionCatalog(@Nullable ConversionService conversionService, @Nullable CompositeMessageConverter messageConverter,
|
public FunctionRegistry functionCatalog(@Nullable CompositeMessageConverter messageConverter,
|
||||||
Map<String, MessageConverter> additionalConverters) {
|
Map<String, MessageConverter> additionalConverters) {
|
||||||
conversionService = conversionService == null ? new DefaultConversionService() : conversionService;
|
ConversionService conversionService = new DefaultConversionService();
|
||||||
if (messageConverter == null) {
|
if (messageConverter == null) {
|
||||||
List<MessageConverter> messageConverters = new ArrayList<>();
|
List<MessageConverter> messageConverters = new ArrayList<>();
|
||||||
messageConverters.addAll(additionalConverters.values());
|
messageConverters.addAll(additionalConverters.values());
|
||||||
@@ -107,6 +102,7 @@ public class ContextFunctionCatalogAutoConfiguration {
|
|||||||
messageConverters.add(new StringMessageConverter());
|
messageConverters.add(new StringMessageConverter());
|
||||||
messageConverter = new CompositeMessageConverter(messageConverters);
|
messageConverter = new CompositeMessageConverter(messageConverters);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new BeanFactoryAwareFunctionRegistry(conversionService, messageConverter);
|
return new BeanFactoryAwareFunctionRegistry(conversionService, messageConverter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user