util: add toTxnResponse in Util
@@ -12,6 +12,7 @@ import com.coreos.jetcd.kv.CompactResponse;
import com.coreos.jetcd.kv.DeleteResponse;
import com.coreos.jetcd.kv.GetResponse;
import com.coreos.jetcd.kv.PutResponse;
+import com.coreos.jetcd.kv.TxnResponse;
import com.coreos.jetcd.lease.LeaseGrantResponse;
import com.coreos.jetcd.lease.LeaseKeepAliveResponse;
import com.coreos.jetcd.lease.LeaseRevokeResponse;
@@ -25,6 +26,8 @@ import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Function;
+import java.util.logging.Level;
+import java.util.logging.Logger;
import java.util.stream.Collectors;
/**
@@ -32,6 +35,8 @@ import java.util.stream.Collectors;
*/
final class Util {
+ private static final Logger logger = Logger.getLogger(Util.class.getName());
+
private Util() {
}
@@ -184,6 +189,38 @@ final class Util {
toEvents(response.getEventsList()));
}
+ /**
+ * convert API TxnResponse to client TxnResponse.
+ */
+ static TxnResponse toTxnResponse(com.coreos.jetcd.api.TxnResponse response) {
+ List<PutResponse> putResponses = new ArrayList<>();
+ List<GetResponse> getResponses = new ArrayList<>();
+ List<DeleteResponse> deleteResponses = new ArrayList<>();
+
+ response.getResponsesList().forEach((responseOp) -> {
+ switch (responseOp.getResponseCase()) {
+ case RESPONSE_PUT:
+ putResponses.add(toPutResponse(responseOp.getResponsePut()));
+ break;
+ case RESPONSE_RANGE:
+ getResponses.add(toGetResponse(responseOp.getResponseRange()));
+ break;
+ case RESPONSE_DELETE_RANGE:
+ deleteResponses.add(toDeleteResponse(responseOp.getResponseDeleteRange()));
+ break;
+ default:
+ logger.log(Level.WARNING, "unexpected type " + responseOp.getResponseCase());
+ }
+ });
+
+ return new TxnResponse(
+ toHeader(response.getHeader(), 0),
+ response.getSucceeded(),
+ putResponses,
+ getResponses,
+ deleteResponses);
+ }
+
/**
* convert ListenableFuture of Type S to CompletableFuture of Type T.
*/
