Skip to content

Commit 02d66ee

Browse files
christophstroblmp911de
authored andcommitted
Add Streamable support to AOT repositories.
Closes #1620
1 parent d95e57a commit 02d66ee

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/repository/aot/CassandraCodeBlocks.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.data.javapoet.TypeNames;
4343
import org.springframework.data.repository.aot.generate.AotQueryMethodGenerationContext;
4444
import org.springframework.data.repository.aot.generate.MethodReturn;
45+
import org.springframework.data.util.Streamable;
4546
import org.springframework.javapoet.CodeBlock;
4647
import org.springframework.javapoet.CodeBlock.Builder;
4748
import org.springframework.javapoet.TypeName;
@@ -706,16 +707,22 @@ CodeBlock build() {
706707
conversionMethod = "convertOne";
707708
}
708709

710+
CodeBlock result = CodeBlock.of("$L($L, $T.class)", conversionMethod, execution.build(), actualReturnType);
711+
if (queryMethod.getReturnType().getType().equals(Streamable.class)) {
712+
result = CodeBlock.of("$T.of($L)", Streamable.class, result);
713+
}
714+
709715
builder.addStatement(returnBuilder //
710-
.optional("($T) $L($L, $T.class)", methodReturn.getTypeName(), conversionMethod, execution.build(),
711-
actualReturnType) //
716+
.optional("($T) $L", methodReturn.getTypeName(), result) //
712717
.build());
713718
} else {
714719

715720
CodeBlock executionBlock = execution.build();
716721

717722
if (query.isCount()) {
718723
returnBuilder.whenPrimitiveOrBoxed(Integer.class, "(int) $L", executionBlock);
724+
} else if (queryMethod.getReturnType().getType().equals(Streamable.class)) {
725+
executionBlock = CodeBlock.of("$T.of($L)", Streamable.class, executionBlock);
719726
}
720727

721728
builder.addStatement(returnBuilder.optional(executionBlock) //

spring-data-cassandra/src/test/java/org/springframework/data/cassandra/repository/aot/CassandraRepositoryContributorIntegrationTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@
4545
import org.springframework.data.cassandra.repository.support.AbstractSpringDataEmbeddedCassandraIntegrationTest;
4646
import org.springframework.data.cassandra.repository.support.IntegrationTestConfig;
4747
import org.springframework.data.domain.Limit;
48+
import org.springframework.data.domain.PageRequest;
4849
import org.springframework.data.domain.Pageable;
4950
import org.springframework.data.domain.ScoringFunction;
5051
import org.springframework.data.domain.Slice;
5152
import org.springframework.data.domain.Sort;
5253
import org.springframework.data.domain.Vector;
5354
import org.springframework.data.domain.Window;
55+
import org.springframework.data.util.Streamable;
5456
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
5557

5658
import com.datastax.oss.driver.api.core.CqlIdentifier;
@@ -196,6 +198,24 @@ void shouldApplySorting() {
196198
.containsSequence("Flynn (Walter Jr.)", "Skyler", "Walter");
197199
}
198200

201+
@Test // GH-1620
202+
void shouldConvertResultToStreamable() {
203+
204+
assertThat(fragment.streamByLastname("White", Sort.by("firstname")))
205+
.isInstanceOf(Streamable.class) //
206+
.extracting(Person::getFirstname) //
207+
.containsExactly("Flynn (Walter Jr.)", "Skyler", "Walter");
208+
}
209+
210+
@Test // GH-1620
211+
void shouldConvertResultToStreamableWhenPageableParameterIsUsed() {
212+
213+
assertThat(fragment.streamByLastname("White", PageRequest.of(0, 2, Sort.by("firstname"))))
214+
.isInstanceOf(Streamable.class) //
215+
.extracting(Person::getFirstname) //
216+
.containsExactly("Flynn (Walter Jr.)", "Skyler");
217+
}
218+
199219
@Test // GH-1566
200220
void shouldFindByFirstnameContains() {
201221

spring-data-cassandra/src/test/java/org/springframework/data/cassandra/repository/aot/PersonRepository.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
import com.datastax.oss.driver.api.core.DefaultConsistencyLevel;
4343
import com.datastax.oss.driver.api.core.cql.ResultSet;
44+
import org.springframework.data.util.Streamable;
4445

4546
/**
4647
* AOT repository interface for {@link Person} entities.
@@ -63,6 +64,10 @@ public interface PersonRepository extends CrudRepository<Person, String> {
6364

6465
List<Person> findByLastname(String lastname, Sort sort);
6566

67+
Streamable<Person> streamByLastname(String lastname, Sort sort);
68+
69+
Streamable<Person> streamByLastname(String lastname, Pageable pageable);
70+
6671
List<Person> findByLastnameOrderByFirstnameAsc(String lastname);
6772

6873
Person findByFirstnameStartsWith(String prefix);

0 commit comments

Comments
 (0)