From 77109d06e440b916dddb8df515db963a550af965 Mon Sep 17 00:00:00 2001 From: arches Date: Fri, 8 Jul 2016 10:00:15 -0400 Subject: [PATCH] Updated access of V3Response --- src/Response.cpp | 26 ++++++++--------- v3/include/V3Response.hpp | 14 +++++++-- v3/src/AsyncCompareAndDeleteAction.cpp | 11 ++++---- v3/src/AsyncCompareAndSwapAction.cpp | 12 ++++---- v3/src/AsyncSetAction.cpp | 9 +++--- v3/src/AsyncTxnResponse.cpp | 14 ++++----- v3/src/AsyncUpdateAction.cpp | 10 +++---- v3/src/AsyncWatchAction.cpp | 4 +-- v3/src/V3Response.cpp | 39 ++++++++++++++++++++++++-- 9 files changed, 89 insertions(+), 50 deletions(-) diff --git a/src/Response.cpp b/src/Response.cpp index 0b0079a..ff71374 100644 --- a/src/Response.cpp +++ b/src/Response.cpp @@ -5,29 +5,25 @@ etcd::Response::Response(const etcdv3::V3Response& reply) { - _index = reply.index; - _action = reply.action; - _error_code = reply.error_code; - _error_message = reply.error_message; - int size = reply.values.size(); - //with prefix means that we expect that - //values could have at least one result(e.g. ls, rmdir) - if(size) + _index = reply.get_index(); + _action = reply.get_action(); + _error_code = reply.get_error_code(); + _error_message = reply.get_error_message(); + if(reply.has_values()) { - for(int index = 0; index < size; index++) + auto val = reply.get_values(); + for(unsigned int index = 0; index < val.size(); index++) { - _values.push_back(Value(reply.values[index])); - _keys.push_back(reply.values[index].key()); + _values.push_back(Value(val[index])); + _keys.push_back(val[index].key()); } } - //values where we expect that - // at most one result.(e.g. set, add, modify, rm, watch) else { - _value = Value(reply.value); + _value = Value(reply.get_value()); } - _prev_value = Value(reply.prev_value); + _prev_value = Value(reply.get_prev_value()); } diff --git a/v3/include/V3Response.hpp b/v3/include/V3Response.hpp index 45e8863..5b864dd 100644 --- a/v3/include/V3Response.hpp +++ b/v3/include/V3Response.hpp @@ -8,13 +8,21 @@ namespace etcdv3 { class V3Response { - public: + public: V3Response(): error_code(0), index(0), isPrefix(false) {}; void set_error_code(int code); + int get_error_code() const; + std::string const & get_error_message() const; void set_error_message(std::string msg); void set_action(std::string action); - std::vector get_kv_values(); - std::vector get_prev_kv_values(); + int get_index() const; + std::string const & get_action() const; + std::vector const & get_values() const; + std::vector const & get_prev_values() const; + mvccpb::KeyValue const & get_value() const; + mvccpb::KeyValue const & get_prev_value() const; + bool has_values() const; + protected: int error_code; int index; bool isPrefix; diff --git a/v3/src/AsyncCompareAndDeleteAction.cpp b/v3/src/AsyncCompareAndDeleteAction.cpp index f1ef4fb..b626380 100644 --- a/v3/src/AsyncCompareAndDeleteAction.cpp +++ b/v3/src/AsyncCompareAndDeleteAction.cpp @@ -36,19 +36,18 @@ etcdv3::AsyncTxnResponse etcdv3::AsyncCompareAndDeleteAction::ParseResponse() AsyncTxnResponse txn_resp; if(!status.ok()) { - txn_resp.error_code = status.error_code(); - txn_resp.error_message = status.error_message(); + txn_resp.set_error_code(status.error_code()); + txn_resp.set_error_message(status.error_message()); } else { txn_resp.ParseResponse(parameters.key, parameters.withPrefix, reply); - txn_resp.prev_values = txn_resp.values; - txn_resp.action = etcdv3::COMPAREDELETE_ACTION; + txn_resp.set_action(etcdv3::COMPAREDELETE_ACTION); if(!reply.succeeded()) { - txn_resp.error_code=101; - txn_resp.error_message="Compare failed"; + txn_resp.set_error_code(101); + txn_resp.set_error_message("Compare failed"); } } diff --git a/v3/src/AsyncCompareAndSwapAction.cpp b/v3/src/AsyncCompareAndSwapAction.cpp index 9e3098c..5f3900b 100644 --- a/v3/src/AsyncCompareAndSwapAction.cpp +++ b/v3/src/AsyncCompareAndSwapAction.cpp @@ -37,20 +37,20 @@ etcdv3::AsyncTxnResponse etcdv3::AsyncCompareAndSwapAction::ParseResponse() if(!status.ok()) { - txn_resp.error_code = status.error_code(); - txn_resp.error_message = status.error_message(); + txn_resp.set_error_code(status.error_code()); + txn_resp.set_error_message(status.error_message()); } else { txn_resp.ParseResponse(parameters.key, parameters.withPrefix, reply); - txn_resp.action = etcdv3::COMPARESWAP_ACTION; + txn_resp.set_action(etcdv3::COMPARESWAP_ACTION); //if there is an error code returned by parseResponse, we must //not overwrite it. - if(!reply.succeeded() && !txn_resp.error_code) + if(!reply.succeeded() && !txn_resp.get_error_code()) { - txn_resp.error_code=101; - txn_resp.error_message="Compare failed"; + txn_resp.set_error_code(101); + txn_resp.set_error_message("Compare failed"); } } diff --git a/v3/src/AsyncSetAction.cpp b/v3/src/AsyncSetAction.cpp index 8690ec7..5b71caf 100644 --- a/v3/src/AsyncSetAction.cpp +++ b/v3/src/AsyncSetAction.cpp @@ -45,12 +45,13 @@ etcdv3::AsyncTxnResponse etcdv3::AsyncSetAction::ParseResponse() else { txn_resp.ParseResponse(parameters.key, parameters.withPrefix, reply); - txn_resp.set_action(isCreate? etcdv3::CREATE_ACTION:etcdv3::SET_ACTION); + std::string action = isCreate? etcdv3::CREATE_ACTION:etcdv3::SET_ACTION; + txn_resp.set_action(action); - if(!reply.succeeded() && txn_resp.action == etcdv3::CREATE_ACTION) + if(!reply.succeeded() && action == etcdv3::CREATE_ACTION) { - txn_resp.error_code=105; - txn_resp.error_message="Key already exists"; + txn_resp.set_error_code(105); + txn_resp.set_error_message("Key already exists"); } } return txn_resp; diff --git a/v3/src/AsyncTxnResponse.cpp b/v3/src/AsyncTxnResponse.cpp index 861260c..00d375e 100644 --- a/v3/src/AsyncTxnResponse.cpp +++ b/v3/src/AsyncTxnResponse.cpp @@ -16,11 +16,11 @@ void etcdv3::AsyncTxnResponse::ParseResponse(std::string const& key, bool prefix AsyncRangeResponse response; response.ParseResponse(*(resp.mutable_response_range()),prefix); - error_code = response.error_code; - error_message = response.error_message; + error_code = response.get_error_code(); + error_message = response.get_error_message(); - values = response.values; - value = response.value; + values = response.get_values(); + value = response.get_value(); } else if(ResponseOp::ResponseCase::kResponsePut == resp.response_case()) { @@ -35,10 +35,10 @@ void etcdv3::AsyncTxnResponse::ParseResponse(std::string const& key, bool prefix AsyncDeleteRangeResponse response; response.ParseResponse(key,prefix,*(resp.mutable_response_delete_range())); - prev_value = response.prev_value; + prev_value = response.get_prev_value(); - values = response.values; - value = response.value; + values = response.get_values(); + value = response.get_value(); } } } diff --git a/v3/src/AsyncUpdateAction.cpp b/v3/src/AsyncUpdateAction.cpp index c37b6bb..123f9fa 100644 --- a/v3/src/AsyncUpdateAction.cpp +++ b/v3/src/AsyncUpdateAction.cpp @@ -29,20 +29,20 @@ etcdv3::AsyncTxnResponse etcdv3::AsyncUpdateAction::ParseResponse() if(!status.ok()) { - txn_resp.error_code = status.error_code(); - txn_resp.error_message = status.error_message(); + txn_resp.set_error_code(status.error_code()); + txn_resp.set_error_message(status.error_message()); } else { if(reply.succeeded()) { txn_resp.ParseResponse(parameters.key, parameters.withPrefix, reply); - txn_resp.action = etcdv3::UPDATE_ACTION; + txn_resp.set_action(etcdv3::UPDATE_ACTION); } else { - txn_resp.error_code = 100; - txn_resp.error_message = "Key not found"; + txn_resp.set_error_code(100); + txn_resp.set_error_message("Key not found"); } } diff --git a/v3/src/AsyncWatchAction.cpp b/v3/src/AsyncWatchAction.cpp index 69cb53d..88bc9af 100644 --- a/v3/src/AsyncWatchAction.cpp +++ b/v3/src/AsyncWatchAction.cpp @@ -96,8 +96,8 @@ etcdv3::AsyncWatchResponse etcdv3::AsyncWatchAction::ParseResponse() AsyncWatchResponse watch_resp; if(!status.ok()) { - watch_resp.error_code = status.error_code(); - watch_resp.error_message = status.error_message(); + watch_resp.set_error_code(status.error_code()); + watch_resp.set_error_message(status.error_message()); } else { diff --git a/v3/src/V3Response.cpp b/v3/src/V3Response.cpp index 38f92a1..822ce48 100644 --- a/v3/src/V3Response.cpp +++ b/v3/src/V3Response.cpp @@ -11,17 +11,52 @@ void etcdv3::V3Response::set_error_message(std::string msg) error_message = msg; } +int etcdv3::V3Response::get_index() const +{ + return index; +} + +std::string const & etcdv3::V3Response::get_action() const +{ + return action; +} + +int etcdv3::V3Response::get_error_code() const +{ + return error_code; +} + +std::string const & etcdv3::V3Response::get_error_message() const +{ + return error_message; +} + void etcdv3::V3Response::set_action(std::string action) { this->action = action; } -std::vector etcdv3::V3Response::get_kv_values() +std::vector const & etcdv3::V3Response::get_values() const { return values; } -std::vector etcdv3::V3Response::get_prev_kv_values() +std::vector const & etcdv3::V3Response::get_prev_values() const { return prev_values; } + +mvccpb::KeyValue const & etcdv3::V3Response::get_value() const +{ + return value; +} + +mvccpb::KeyValue const & etcdv3::V3Response::get_prev_value() const +{ + return prev_value; +} + +bool etcdv3::V3Response::has_values() const +{ + return values.size() > 0; +}