Skip to content

Commit

Permalink
refactor: simplify tuple key mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
booniepepper committed Nov 18, 2023
1 parent 3cf7942 commit 1db23c9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
7 changes: 4 additions & 3 deletions src/main/java/dev/openfga/sdk/api/client/ClientTupleKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import dev.openfga.sdk.api.model.TupleKey;
import dev.openfga.sdk.api.model.TupleKeys;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class ClientTupleKey {
Expand Down Expand Up @@ -66,12 +67,12 @@ public TupleKey asTupleKey() {
return new TupleKey().user(user).relation(relation)._object(_object);
}

public static TupleKeys asTupleKeys(List<ClientTupleKey> clientTupleKeys) {
public static Optional<TupleKeys> asTupleKeys(List<ClientTupleKey> clientTupleKeys) {
if (clientTupleKeys == null || clientTupleKeys.size() == 0) {
return new TupleKeys();
return Optional.empty();
}

return new TupleKeys().tupleKeys(asListOfTupleKey(clientTupleKeys));
return Optional.of(new TupleKeys().tupleKeys(asListOfTupleKey(clientTupleKeys)));
}

public static ContextualTupleKeys asContextualTupleKeys(List<ClientTupleKey> clientTupleKeys) {
Expand Down
21 changes: 8 additions & 13 deletions src/main/java/dev/openfga/sdk/api/client/OpenFgaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,17 +288,8 @@ private CompletableFuture<ClientWriteResponse> writeOneTransaction(
String storeId, ClientWriteRequest request, ClientWriteOptions options) {
WriteRequest body = new WriteRequest();

if (request != null) {
TupleKeys writes = ClientTupleKey.asTupleKeys(request.getWrites());
if (!writes.getTupleKeys().isEmpty()) {
body.writes(writes);
}

TupleKeys deletes = ClientTupleKey.asTupleKeys(request.getDeletes());
if (!deletes.getTupleKeys().isEmpty()) {
body.deletes(deletes);
}
}
ClientTupleKey.asTupleKeys(request.getWrites()).ifPresent(body::writes);
ClientTupleKey.asTupleKeys(request.getDeletes()).ifPresent(body::deletes);

if (options != null && !isNullOrWhitespace(options.getAuthorizationModelId())) {
body.authorizationModelId(options.getAuthorizationModelId());
Expand Down Expand Up @@ -358,7 +349,9 @@ public CompletableFuture<ClientWriteResponse> writeTuples(List<ClientTupleKey> t
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();

var request = new WriteRequest().writes(ClientTupleKey.asTupleKeys(tupleKeys));
var request = new WriteRequest();
ClientTupleKey.asTupleKeys(tupleKeys).ifPresent(request::writes);

String authorizationModelId = configuration.getAuthorizationModelId();
if (!isNullOrWhitespace(authorizationModelId)) {
request.authorizationModelId(authorizationModelId);
Expand All @@ -377,7 +370,9 @@ public CompletableFuture<ClientWriteResponse> deleteTuples(List<ClientTupleKey>
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();

var request = new WriteRequest().deletes(ClientTupleKey.asTupleKeys(tupleKeys));
var request = new WriteRequest();
ClientTupleKey.asTupleKeys(tupleKeys).ifPresent(request::deletes);

String authorizationModelId = configuration.getAuthorizationModelId();
if (!isNullOrWhitespace(authorizationModelId)) {
request.authorizationModelId(authorizationModelId);
Expand Down

0 comments on commit 1db23c9

Please sign in to comment.