Updated access of V3Response

This commit is contained in:
arches 2016-07-08 10:00:15 -04:00
parent 6dfbe791a0
commit 77109d06e4
9 changed files with 89 additions and 50 deletions

View File

@ -5,29 +5,25 @@
etcd::Response::Response(const etcdv3::V3Response& reply) etcd::Response::Response(const etcdv3::V3Response& reply)
{ {
_index = reply.index; _index = reply.get_index();
_action = reply.action; _action = reply.get_action();
_error_code = reply.error_code; _error_code = reply.get_error_code();
_error_message = reply.error_message; _error_message = reply.get_error_message();
int size = reply.values.size(); if(reply.has_values())
//with prefix means that we expect that
//values could have at least one result(e.g. ls, rmdir)
if(size)
{ {
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])); _values.push_back(Value(val[index]));
_keys.push_back(reply.values[index].key()); _keys.push_back(val[index].key());
} }
} }
//values where we expect that
// at most one result.(e.g. set, add, modify, rm, watch)
else else
{ {
_value = Value(reply.value); _value = Value(reply.get_value());
} }
_prev_value = Value(reply.prev_value); _prev_value = Value(reply.get_prev_value());
} }

View File

@ -11,10 +11,18 @@ namespace etcdv3
public: public:
V3Response(): error_code(0), index(0), isPrefix(false) {}; V3Response(): error_code(0), index(0), isPrefix(false) {};
void set_error_code(int code); 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_error_message(std::string msg);
void set_action(std::string action); void set_action(std::string action);
std::vector<mvccpb::KeyValue> get_kv_values(); int get_index() const;
std::vector<mvccpb::KeyValue> get_prev_kv_values(); std::string const & get_action() const;
std::vector<mvccpb::KeyValue> const & get_values() const;
std::vector<mvccpb::KeyValue> 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 error_code;
int index; int index;
bool isPrefix; bool isPrefix;

View File

@ -36,19 +36,18 @@ etcdv3::AsyncTxnResponse etcdv3::AsyncCompareAndDeleteAction::ParseResponse()
AsyncTxnResponse txn_resp; AsyncTxnResponse txn_resp;
if(!status.ok()) if(!status.ok())
{ {
txn_resp.error_code = status.error_code(); txn_resp.set_error_code(status.error_code());
txn_resp.error_message = status.error_message(); txn_resp.set_error_message(status.error_message());
} }
else else
{ {
txn_resp.ParseResponse(parameters.key, parameters.withPrefix, reply); txn_resp.ParseResponse(parameters.key, parameters.withPrefix, reply);
txn_resp.prev_values = txn_resp.values; txn_resp.set_action(etcdv3::COMPAREDELETE_ACTION);
txn_resp.action = etcdv3::COMPAREDELETE_ACTION;
if(!reply.succeeded()) if(!reply.succeeded())
{ {
txn_resp.error_code=101; txn_resp.set_error_code(101);
txn_resp.error_message="Compare failed"; txn_resp.set_error_message("Compare failed");
} }
} }

View File

@ -37,20 +37,20 @@ etcdv3::AsyncTxnResponse etcdv3::AsyncCompareAndSwapAction::ParseResponse()
if(!status.ok()) if(!status.ok())
{ {
txn_resp.error_code = status.error_code(); txn_resp.set_error_code(status.error_code());
txn_resp.error_message = status.error_message(); txn_resp.set_error_message(status.error_message());
} }
else else
{ {
txn_resp.ParseResponse(parameters.key, parameters.withPrefix, reply); 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 //if there is an error code returned by parseResponse, we must
//not overwrite it. //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.set_error_code(101);
txn_resp.error_message="Compare failed"; txn_resp.set_error_message("Compare failed");
} }
} }

View File

@ -45,12 +45,13 @@ etcdv3::AsyncTxnResponse etcdv3::AsyncSetAction::ParseResponse()
else else
{ {
txn_resp.ParseResponse(parameters.key, parameters.withPrefix, reply); 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.set_error_code(105);
txn_resp.error_message="Key already exists"; txn_resp.set_error_message("Key already exists");
} }
} }
return txn_resp; return txn_resp;

View File

@ -16,11 +16,11 @@ void etcdv3::AsyncTxnResponse::ParseResponse(std::string const& key, bool prefix
AsyncRangeResponse response; AsyncRangeResponse response;
response.ParseResponse(*(resp.mutable_response_range()),prefix); response.ParseResponse(*(resp.mutable_response_range()),prefix);
error_code = response.error_code; error_code = response.get_error_code();
error_message = response.error_message; error_message = response.get_error_message();
values = response.values; values = response.get_values();
value = response.value; value = response.get_value();
} }
else if(ResponseOp::ResponseCase::kResponsePut == resp.response_case()) else if(ResponseOp::ResponseCase::kResponsePut == resp.response_case())
{ {
@ -35,10 +35,10 @@ void etcdv3::AsyncTxnResponse::ParseResponse(std::string const& key, bool prefix
AsyncDeleteRangeResponse response; AsyncDeleteRangeResponse response;
response.ParseResponse(key,prefix,*(resp.mutable_response_delete_range())); response.ParseResponse(key,prefix,*(resp.mutable_response_delete_range()));
prev_value = response.prev_value; prev_value = response.get_prev_value();
values = response.values; values = response.get_values();
value = response.value; value = response.get_value();
} }
} }
} }

View File

@ -29,20 +29,20 @@ etcdv3::AsyncTxnResponse etcdv3::AsyncUpdateAction::ParseResponse()
if(!status.ok()) if(!status.ok())
{ {
txn_resp.error_code = status.error_code(); txn_resp.set_error_code(status.error_code());
txn_resp.error_message = status.error_message(); txn_resp.set_error_message(status.error_message());
} }
else else
{ {
if(reply.succeeded()) if(reply.succeeded())
{ {
txn_resp.ParseResponse(parameters.key, parameters.withPrefix, reply); txn_resp.ParseResponse(parameters.key, parameters.withPrefix, reply);
txn_resp.action = etcdv3::UPDATE_ACTION; txn_resp.set_action(etcdv3::UPDATE_ACTION);
} }
else else
{ {
txn_resp.error_code = 100; txn_resp.set_error_code(100);
txn_resp.error_message = "Key not found"; txn_resp.set_error_message("Key not found");
} }
} }

View File

@ -96,8 +96,8 @@ etcdv3::AsyncWatchResponse etcdv3::AsyncWatchAction::ParseResponse()
AsyncWatchResponse watch_resp; AsyncWatchResponse watch_resp;
if(!status.ok()) if(!status.ok())
{ {
watch_resp.error_code = status.error_code(); watch_resp.set_error_code(status.error_code());
watch_resp.error_message = status.error_message(); watch_resp.set_error_message(status.error_message());
} }
else else
{ {

View File

@ -11,17 +11,52 @@ void etcdv3::V3Response::set_error_message(std::string msg)
error_message = 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) void etcdv3::V3Response::set_action(std::string action)
{ {
this->action = action; this->action = action;
} }
std::vector<mvccpb::KeyValue> etcdv3::V3Response::get_kv_values() std::vector<mvccpb::KeyValue> const & etcdv3::V3Response::get_values() const
{ {
return values; return values;
} }
std::vector<mvccpb::KeyValue> etcdv3::V3Response::get_prev_kv_values() std::vector<mvccpb::KeyValue> const & etcdv3::V3Response::get_prev_values() const
{ {
return prev_values; 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;
}