DATACMNS-1165 - Added support for Streamable as repository return type.
We now allow users to use our own Streamable interface as query method return types to easily invoke ….stream() on everything that returns an Iterable using non-parallel streaming by default.
This commit is contained in:
@@ -257,6 +257,7 @@ public abstract class QueryExecutionConverters {
|
||||
}
|
||||
|
||||
conversionService.addConverter(new NullableWrapperToFutureConverter(conversionService));
|
||||
conversionService.addConverter(IterableToStreamableConverter.INSTANCE);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -697,6 +698,21 @@ public abstract class QueryExecutionConverters {
|
||||
}
|
||||
}
|
||||
|
||||
private static enum IterableToStreamableConverter implements Converter<Iterable<?>, Streamable<?>> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
|
||||
*/
|
||||
@Nonnull
|
||||
@Override
|
||||
public Streamable<?> convert(Iterable<?> source) {
|
||||
return Streamable.of(source);
|
||||
}
|
||||
}
|
||||
|
||||
@Value
|
||||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public static class WrapperType {
|
||||
|
||||
@@ -25,11 +25,13 @@ import rx.Observable;
|
||||
import rx.Single;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.reactivestreams.Publisher;
|
||||
@@ -37,6 +39,7 @@ import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.util.Streamable;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link QueryExecutionResultHandler}.
|
||||
@@ -348,6 +351,18 @@ public class QueryExecutionResultHandlerUnitTests {
|
||||
assertThat(result).isInstanceOfSatisfying(Option.class, it -> assertThat(it.get()).isEqualTo(value));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1165
|
||||
@SuppressWarnings("unchecked")
|
||||
public void convertsIterableIntoStreamable() {
|
||||
|
||||
Iterable<?> source = Arrays.asList(new Object());
|
||||
|
||||
Object result = handler.postProcessInvocationResult(source, TypeDescriptor.valueOf(Streamable.class));
|
||||
|
||||
assertThat(result).isInstanceOfSatisfying(Streamable.class,
|
||||
it -> assertThat(it.stream().collect(Collectors.toList())).isEqualTo(source));
|
||||
}
|
||||
|
||||
private static TypeDescriptor getTypeDescriptorFor(String methodName) throws Exception {
|
||||
|
||||
Method method = Sample.class.getMethod(methodName);
|
||||
|
||||
Reference in New Issue
Block a user