Skip to content

Commit

Permalink
New overload in invokeBinding + allow null typeref. (#1158)
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Souza <asouza.pro@gmail.com>
  • Loading branch information
artursouza authored Dec 3, 2024
1 parent 7798360 commit 7805ea9
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
8 changes: 8 additions & 0 deletions sdk/src/main/java/io/dapr/client/AbstractDaprClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,14 @@ public <T> Mono<T> invokeBinding(
return this.invokeBinding(bindingName, operation, data, metadata, TypeRef.get(clazz));
}

/**
* {@inheritDoc}
*/
@Override
public Mono<Void> invokeBinding(InvokeBindingRequest request) {
return this.invokeBinding(request, TypeRef.VOID);
}

/**
* {@inheritDoc}
*/
Expand Down
8 changes: 8 additions & 0 deletions sdk/src/main/java/io/dapr/client/DaprClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,14 @@ <T> Mono<T> invokeBinding(String bindingName, String operation, Object data, Map
<T> Mono<T> invokeBinding(String bindingName, String operation, Object data, Map<String, String> metadata,
Class<T> clazz);

/**
* Invokes a Binding operation.
*
* @param request The binding invocation request.
* @return a Mono with void.
*/
Mono<Void> invokeBinding(InvokeBindingRequest request);

/**
* Invokes a Binding operation.
*
Expand Down
36 changes: 29 additions & 7 deletions sdk/src/main/java/io/dapr/client/DaprClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,10 @@ private <T> Subscription<T> buildSubscription(

try {
CloudEvent<T> cloudEvent = new CloudEvent<>();
var object =
DaprClientImpl.this.objectSerializer.deserialize(message.getData().toByteArray(), type);
T object = null;
if (type != null) {
object = DaprClientImpl.this.objectSerializer.deserialize(message.getData().toByteArray(), type);
}
cloudEvent.setData(object);
cloudEvent.setDatacontenttype(message.getDataContentType());
cloudEvent.setId(message.getId());
Expand Down Expand Up @@ -528,6 +530,10 @@ public <T> Mono<T> invokeMethod(InvokeMethodRequest invokeMethodRequest, TypeRef

private <T> Mono<T> getMonoForHttpResponse(TypeRef<T> type, DaprHttp.Response r) {
try {
if (type == null) {
return Mono.empty();
}

T object = objectSerializer.deserialize(r.getBody(), type);
if (object == null) {
return Mono.empty();
Expand Down Expand Up @@ -585,6 +591,9 @@ public <T> Mono<T> invokeBinding(InvokeBindingRequest request, TypeRef<T> type)
}

try {
if (type == null) {
return Mono.empty();
}
return Mono.justOrEmpty(objectSerializer.deserialize(it.getData().toByteArray(), type));
} catch (IOException e) {
throw DaprException.propagate(e);
Expand Down Expand Up @@ -706,13 +715,18 @@ private <T> State<T> buildStateKeyValue(
return new State<>(key, error);
}

ByteString payload = item.getData();
byte[] data = payload == null ? null : payload.toByteArray();
T value = stateSerializer.deserialize(data, type);
String etag = item.getEtag();
if (etag.equals("")) {
etag = null;
}

T value = null;
if (type != null) {
ByteString payload = item.getData();
byte[] data = payload == null ? null : payload.toByteArray();
value = stateSerializer.deserialize(data, type);
}

return new State<>(key, value, etag, item.getMetadataMap(), null);
}

Expand All @@ -723,7 +737,11 @@ private <T> State<T> buildStateKeyValue(
TypeRef<T> type) throws IOException {
ByteString payload = response.getData();
byte[] data = payload == null ? null : payload.toByteArray();
T value = stateSerializer.deserialize(data, type);
T value = null;
if (type != null) {
value = stateSerializer.deserialize(data, type);
}

String etag = response.getEtag();
if (etag.equals("")) {
etag = null;
Expand Down Expand Up @@ -1108,7 +1126,11 @@ private <T> QueryStateItem<T> buildQueryStateKeyValue(
}
ByteString payload = item.getData();
byte[] data = payload == null ? null : payload.toByteArray();
T value = stateSerializer.deserialize(data, type);
T value = null;
if (type != null) {
value = stateSerializer.deserialize(data, type);
}

String etag = item.getEtag();
if (etag.equals("")) {
etag = null;
Expand Down
20 changes: 19 additions & 1 deletion sdk/src/test/java/io/dapr/client/DaprClientGrpcTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.dapr.client.domain.ExecuteStateTransactionRequest;
import io.dapr.client.domain.GetBulkStateRequest;
import io.dapr.client.domain.GetStateRequest;
import io.dapr.client.domain.InvokeBindingRequest;
import io.dapr.client.domain.PublishEventRequest;
import io.dapr.client.domain.RuleMetadata;
import io.dapr.client.domain.State;
Expand Down Expand Up @@ -327,7 +328,7 @@ public void invokeBindingCallbackExceptionThrownTest() {
@Test
public void invokeBindingTest() throws IOException {
DaprProtos.InvokeBindingResponse.Builder responseBuilder =
DaprProtos.InvokeBindingResponse.newBuilder().setData(serialize("OK"));
DaprProtos.InvokeBindingResponse.newBuilder().setData(serialize("OK"));
doAnswer((Answer<Void>) invocation -> {
StreamObserver<DaprProtos.InvokeBindingResponse> observer = (StreamObserver<DaprProtos.InvokeBindingResponse>) invocation.getArguments()[1];
observer.onNext(responseBuilder.build());
Expand All @@ -339,6 +340,23 @@ public void invokeBindingTest() throws IOException {
result.block();
}

@Test
public void invokeBindingVoidReturnTest() throws IOException {
DaprProtos.InvokeBindingResponse.Builder responseBuilder =
DaprProtos.InvokeBindingResponse.newBuilder().setData(serialize("OK"));
doAnswer((Answer<Void>) invocation -> {
StreamObserver<DaprProtos.InvokeBindingResponse> observer = (StreamObserver<DaprProtos.InvokeBindingResponse>) invocation.getArguments()[1];
observer.onNext(responseBuilder.build());
observer.onCompleted();
return null;
}).when(daprStub).invokeBinding(any(DaprProtos.InvokeBindingRequest.class), any());

var request = new InvokeBindingRequest("BindingName", "MyOperation");
request.setData("request");
Mono<Void> result = client.invokeBinding(request, null);
result.block();
}

@Test
public void invokeBindingByteArrayTest() {
DaprProtos.InvokeBindingResponse.Builder responseBuilder =
Expand Down

0 comments on commit 7805ea9

Please sign in to comment.