Skip to content

Commit

Permalink
Allowing Stream as return type from DynamoDB declarative service
Browse files Browse the repository at this point in the history
  • Loading branch information
musketyr committed Jan 9, 2025
1 parent f6835d5 commit 60d96c4
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
16 changes: 8 additions & 8 deletions docs/guide/src/docs/asciidoc/dynamodb.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ include::{root-dir}/subprojects/micronaut-amazon-awssdk-dynamodb/src/test/groovy
<5> Specify some range key criteria with the method's `rangeKey` argument (see https://agorapulse.github.io/micronaut-aws-sdk/api/com/agorapulse/micronaut/aws/dynamodb/builder/RangeConditionCollector.html[RangeConditionCollector] for full reference)
<6> You can limit which properties are returned from the query
<7> Only `rangeIndex` property will be populated in the entities returned
<8> The arguments have no special meaning but you can use them in the query. The method must return either `Publisher` or `List` of entities.
<8> The arguments have no special meaning but you can use them in the query. The method must return either `Publisher`, `Stream` or `Iterable` of entities.

[source,java,indent=0,options="nowrap",role="secondary"]
.Java
Expand All @@ -626,7 +626,7 @@ include::{root-dir}/subprojects/micronaut-amazon-awssdk-dynamodb/src/test/groovy
<4> Specify some range key criteria with the method's `rangeKey` argument (see https://agorapulse.github.io/micronaut-aws-sdk/api/com/agorapulse/micronaut/aws/dynamodb/builder/RangeConditionCollector.html[RangeConditionCollector] for full reference)
<5> Only `rangeIndex` property will be populated in the entities returned
<6> `@Query` annotation accepts a class which implements `Function<Map<String, Object>, DetachedQuery>`
<7> The arguments have no special meaning but you can use them in the query using `arguments` map. The method must return either `Publisher` or `List` of entities.
<7> The arguments have no special meaning but you can use them in the query using `arguments` map. The method must return either `Publisher`, `Stream` or `List` of entities.


[source,kotlin,indent=0,options="nowrap",role="secondary"]
Expand All @@ -647,7 +647,7 @@ include::{root-dir}/subprojects/micronaut-amazon-awssdk-dynamodb-kotlin/src/test
<4> Specify some range key criteria with the method's `rangeKey` argument (see https://agorapulse.github.io/micronaut-aws-sdk/api/com/agorapulse/micronaut/aws/dynamodb/builder/RangeConditionCollector.html[RangeConditionCollector] for full reference)
<5> Only `rangeIndex` property will be populated in the entities returned
<6> `@Query` annotation accepts a class which implements `Function<Map<String, Object>, DetachedQuery>`
<7> The arguments have no special meaning but you can use them in the query using `arguments` map. The method must return either `Publisher` or `List` of entities.
<7> The arguments have no special meaning but you can use them in the query using `arguments` map. The method must return either `Publisher`, `Stream` or `List` of entities.


[source,groovy,indent=0,options="nowrap",role="secondary"]
Expand All @@ -665,7 +665,7 @@ include::{root-dir}/subprojects/micronaut-aws-sdk-dynamodb/src/test/groovy/com/a
<5> Specify some range key criteria with the method's `rangeKey` argument (see https://agorapulse.github.io/micronaut-aws-sdk/api/com/agorapulse/micronaut/aws/dynamodb/builder/RangeConditionCollector.html[RangeConditionCollector] for full reference)
<6> You can limit which properties are returned from the query
<7> Only `rangeIndex` property will be populated in the entities returned
<8> The arguments have no special meaning but you can use them in the query. The method must return either `Publisher` or `List` of entities.
<8> The arguments have no special meaning but you can use them in the query. The method must return either `Publisher`, `Stream` or `List` of entities.

===== Scanning

Expand All @@ -690,7 +690,7 @@ include::{root-dir}/subprojects/micronaut-amazon-awssdk-dynamodb/src/test/groovy
<2> Annotate an interface with `@Service` with the type of the entity as its `value`
<3> `@Scan` annotation accepts a closure which returns a scan builder (see https://agorapulse.github.io/micronaut-aws-sdk/api/com/agorapulse/micronaut/aws/dynamodb/builder/ScanBuilder.html[ScanBuilder] for full reference)
<4> Specify some filter criteria with the method's `foo` argument (see https://agorapulse.github.io/micronaut-aws-sdk/api/com/agorapulse/micronaut/aws/dynamodb/builder/RangeConditionCollector.html[RangeConditionCollector] for full reference)
<5> The arguments have no special meaning but you can use them in the scan definition. The method must return either `Publisher` or `List` of entities.
<5> The arguments have no special meaning but you can use them in the scan definition. The method must return either `Publisher`, `Stream` or `List` of entities.


[source,java,indent=0,options="nowrap",role="secondary"]
Expand All @@ -708,7 +708,7 @@ include::{root-dir}/subprojects/micronaut-amazon-awssdk-dynamodb/src/test/groovy
<2> Define class which implements `ScanFunction`
<3> Specify some filter criteria with the method's `foo` argument (see https://agorapulse.github.io/micronaut-aws-sdk/api/com/agorapulse/micronaut/aws/dynamodb/builder/RangeConditionCollector.html[RangeConditionCollector] for full reference)
<4> `@Scan` annotation accepts a class which implements `Function<Map<String, Object>, DetachedScan>`
<5> The arguments have no special meaning but you can use them in the scan definition. The method must return either `Publisher` or `List` of entities.
<5> The arguments have no special meaning but you can use them in the scan definition. The method must return either `Publisher`, `Stream` or `List` of entities.

[source,kotlin,indent=0,options="nowrap",role="secondary"]
.Kotlin
Expand All @@ -725,7 +725,7 @@ include::{root-dir}/subprojects/micronaut-amazon-awssdk-dynamodb-kotlin/src/test
<2> Define class which implements `ScanFunction`
<3> Specify some filter criteria with the method's `foo` argument (see https://agorapulse.github.io/micronaut-aws-sdk/api/com/agorapulse/micronaut/aws/dynamodb/builder/RangeConditionCollector.html[RangeConditionCollector] for full reference)
<4> `@Scan` annotation accepts a class which implements `Function<Map<String, Object>, DetachedScan>`
<5> The arguments have no special meaning but you can use them in the scan definition. The method must return either `Publisher` or `List` of entities.
<5> The arguments have no special meaning but you can use them in the scan definition. The method must return either `Publisher`, `Stream` or `List` of entities.

[source,groovy,indent=0,options="nowrap",role="secondary"]
.Groovy (AWS SDK 1.x)
Expand All @@ -742,7 +742,7 @@ include::{root-dir}/subprojects/micronaut-aws-sdk-dynamodb/src/test/groovy/com/a
<2> Annotate an interface with `@Service` with the type of the entity as its `value`
<3> `@Scan` annotation accepts a closure which returns a scan builder (see https://agorapulse.github.io/micronaut-aws-sdk/api/com/agorapulse/micronaut/aws/dynamodb/builder/ScanBuilder.html[ScanBuilder] for full reference)
<4> Specify some filter criteria with the method's `foo` argument (see https://agorapulse.github.io/micronaut-aws-sdk/api/com/agorapulse/micronaut/aws/dynamodb/builder/RangeConditionCollector.html[RangeConditionCollector] for full reference)
<5> The arguments have no special meaning but you can use them in the scan definition. The method must return either `Publisher` or `List` of entities.
<5> The arguments have no special meaning but you can use them in the scan definition. The method must return either `Publisher`, `Stream` or `List` of entities.


===== Updates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException;
import software.amazon.awssdk.services.dynamodb.model.Stream;

import java.util.Collections;
import java.util.Map;
Expand Down Expand Up @@ -232,6 +233,10 @@ private Object unwrapIfRequired(Publisher<?> publisher, Class<Object> type) {
});
}

if (Stream.class.isAssignableFrom(type)) {
return Flux.from(publisher).toStream();
}

if (type.isArray() || Iterable.class.isAssignableFrom(type)) {
return conversionService.convert(Flux.from(publisher).collectList().block(), type).orElse(Collections.emptyList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

/**
* Introduction for {@link Service} annotation.
Expand Down Expand Up @@ -173,6 +174,10 @@ private Object publisherOrIterable(Publisher<?> result, Class<?> type) {
return Publishers.convertPublisher(conversionService, result, type);
}

if (Stream.class.isAssignableFrom(type)) {
return Flux.from(result).toStream();
}

return Flux.from(result).collectList().blockOptional().orElse(Collections.emptyList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public void testJavaService() {


assertEquals(2, s.getAll("1", Arrays.asList("2", "1")).size());
assertEquals(2, s.getAllAsStream("1", Arrays.asList("2", "1")).count());
assertEquals(2, s.loadAll("1", Arrays.asList("2", "1")).size());
assertEquals(2, s.getAll("1", "2", "1").size());
assertEquals(0, s.loadAll("1", "3", "4").size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

// tag::all[]
// tag::header[]
Expand All @@ -38,6 +39,8 @@ public interface DynamoDBEntityService {

List<DynamoDBEntity> getAll(String hash, List<String> rangeKeys);

Stream<DynamoDBEntity> getAllAsStream(String hash, List<String> rangeKeys);

List<DynamoDBEntity> getAll(String hash, String... rangeKeys);

List<DynamoDBEntity> loadAll(String hash, List<String> rangeKeys);
Expand Down

0 comments on commit 60d96c4

Please sign in to comment.