Removed unused type converters, cleaned up tests

This commit is contained in:
Oleg Zhurakousky
2019-06-26 10:14:03 +03:00
parent d692cde856
commit 4c5ea1ae4b
4 changed files with 23 additions and 104 deletions

View File

@@ -18,6 +18,7 @@ import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MimeType;
import org.springframework.util.ObjectUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@@ -94,6 +95,9 @@ class FunctionTypeConversionHelper {
@SuppressWarnings("rawtypes")
Object convertOutputIfNecessary(Object output, MimeType... acceptedOutputTypes) {
if (ObjectUtils.isEmpty(acceptedOutputTypes)) {
return output;
}
List<Object> convertedResults = new ArrayList<Object>();
if (output instanceof Tuple2) {
convertedResults.add(this.doConvert(((Tuple2)output).getT1(), acceptedOutputTypes[0]));
@@ -118,9 +122,7 @@ class FunctionTypeConversionHelper {
convertedResults.add(this.doConvert(((Tuple8)output).getT8(), acceptedOutputTypes[7]));
}
if (!CollectionUtils.isEmpty(convertedResults)) {
output = Tuples.fromArray(convertedResults.toArray());
}
output = Tuples.fromArray(convertedResults.toArray());
return output;
}

View File

@@ -315,10 +315,7 @@ public class LazyFunctionRegistry implements FunctionRegistry, FunctionInspector
}
}
// ====
if (!ObjectUtils.isEmpty(this.acceptedOutputTypes)) {
result = this.functionTypeConversionHelper.convertOutputIfNecessary(result, this.acceptedOutputTypes);
}
result = this.functionTypeConversionHelper.convertOutputIfNecessary(result, this.acceptedOutputTypes);
return this.wrapOutputToReactiveIfNecessary(result);
}

View File

@@ -17,11 +17,9 @@
package org.springframework.cloud.function.context.config;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
@@ -66,9 +64,6 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalGenericConverter;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.type.StandardMethodMetadata;
import org.springframework.lang.Nullable;
@@ -77,9 +72,7 @@ import org.springframework.messaging.converter.CompositeMessageConverter;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.converter.StringMessageConverter;
import org.springframework.util.ClassUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
@@ -116,94 +109,9 @@ public class ContextFunctionCatalogAutoConfiguration {
messageConverters.add(new StringMessageConverter());
messageConverter = new CompositeMessageConverter(messageConverters);
}
if (conversionService != null) {
((ConfigurableConversionService)conversionService).addConverter(new MyConverter());
((ConfigurableConversionService)conversionService).addConverter(new ObjectToByteArrayConverter());
}
return new LazyFunctionRegistry(conversionService, messageConverter);
}
public static class MyConverter implements ConditionalGenericConverter {
@Override
public Set<ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(byte[].class, String.class));
}
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
return new String(((byte[])source), StandardCharsets.UTF_8);
}
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
if (ClassUtils.isAssignable(sourceType.getType(), byte[].class) && ClassUtils.isAssignable(targetType.getType(), String.class)) {
// maybe
return true;
}
return false;
}
}
public static class ObjectToByteArrayConverter implements ConditionalGenericConverter {
ObjectMapper mapper = new ObjectMapper();
@Override
public Set<ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(Object.class, byte[].class));
}
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
try {
byte[] result = mapper.writeValueAsBytes(source);
return result;
}
catch (Exception e) {
throw new IllegalStateException("Failwd to convert " + source + " to byte[]", e);
}
}
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
if (ClassUtils.isAssignable(Object.class, sourceType.getType()) && ClassUtils.isAssignable(byte[].class, targetType.getType())) {
// maybe
return true;
}
return false;
}
}
public static class Person {
private String name;
private int id;
public Person() {
}
public Person(String name, int id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String toString() {
return "Person: " + name + "/" + id;
}
}
@Bean(RoutingFunction.FUNCTION_NAME)
@ConditionalOnProperty(name = "spring.cloud.function.routing.enabled", havingValue = "true")
RoutingFunction gateway(FunctionCatalog functionCatalog, FunctionInspector functionInspector) {

View File

@@ -17,7 +17,6 @@
package org.springframework.cloud.function.context.catalog;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
@@ -40,6 +39,8 @@ import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.MimeTypeUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;
@@ -250,10 +251,21 @@ public class LazyFunctionRegistryMultiInOutTests {
MessageBuilder.withPayload(two.array()).setHeader(MessageHeaders.CONTENT_TYPE, "octet-stream/integer").build());
Tuple2<Flux<Message<byte[]>>, Mono<Message<byte[]>>> result = multiTuMulti.apply(Tuples.of(firstFlux, secondFlux, thirdFlux));
result.getT1().subscribe(v -> System.out.println("=> 1: " + v));
result.getT2().subscribe(v -> System.out.println("=> 2: " + v));
//Tuple2<Object, Object> d = multiTuMulti.apply(Tuples.of(firstFlux, secondFlux, thirdFlux));
ObjectMapper mapper = new ObjectMapper();
result.getT1().subscribe(v -> {
try {
System.out.println("=> 1: " + mapper.readValue(v.getPayload(), Person.class));
} catch (Exception e) {
e.printStackTrace();
}
});
result.getT2().subscribe(v -> {
try {
System.out.println("=> 2: " + mapper.readValue(v.getPayload(), Long.class));
} catch (Exception e) {
e.printStackTrace();
}
});
}