From: Kefu Chai Date: Sat, 9 Jan 2016 03:20:59 +0000 (+0800) Subject: common/cmdparse: handle array of integer also X-Git-Tag: v11.0.0~530^2~3 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=6e475df7d7c2d6e530c6feb02fc34557209aa35a;p=ceph.git common/cmdparse: handle array of integer also we need to handle cmd arg of [int..]. for example, the osdid in "ceph osd pg-temp .." could be represented using an array of integers instead of an array of strings. Signed-off-by: Kefu Chai --- diff --git a/src/common/cmdparse.cc b/src/common/cmdparse.cc index 6c1aae83db5a..7c4ad58998d5 100644 --- a/src/common/cmdparse.cc +++ b/src/common/cmdparse.cc @@ -155,16 +155,35 @@ cmdmap_from_json(vector cmd, map *mapp, stringstrea case json_spirit::array_type: { // array is a vector of values. Unpack it to a vector - // of strings, the only type we handle. - vector spvals = it->second.get_array(); - vector outv; - for (vector::iterator sv = spvals.begin(); - sv != spvals.end(); ++sv) { - if (sv->type() != json_spirit::str_type) - throw(runtime_error("Can't handle arrays of non-strings")); - outv.push_back(sv->get_str()); + // of strings or int64_t, the only types we handle. + const vector& spvals = it->second.get_array(); + if (spvals.empty()) { + // if an empty array is acceptable, the caller should always check for + // vector if the expected value of "vector" in the + // cmdmap is missing. + (*mapp)[it->first] = std::move(vector()); + } else if (spvals.front().type() == json_spirit::str_type) { + vector outv; + for (const auto& sv : spvals) { + if (sv.type() != json_spirit::str_type) { + throw(runtime_error("Can't handle arrays of multiple types")); + } + outv.push_back(sv.get_str()); + } + (*mapp)[it->first] = std::move(outv); + } else if (spvals.front().type() == json_spirit::int_type) { + vector outv; + for (const auto& sv : spvals) { + if (spvals.front().type() != json_spirit::int_type) { + throw(runtime_error("Can't handle arrays of multiple types")); + } + outv.push_back(sv.get_int64()); + } + (*mapp)[it->first] = std::move(outv); + } else { + throw(runtime_error("Can't handle arrays of types other than " + "int or string")); } - (*mapp)[it->first] = outv; } break; case json_spirit::str_type: diff --git a/src/common/cmdparse.h b/src/common/cmdparse.h index 97c082037f76..0d022af71cbd 100644 --- a/src/common/cmdparse.h +++ b/src/common/cmdparse.h @@ -17,7 +17,12 @@ class CephContext; /* this is handy; can't believe it's not standard */ #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*a)) -typedef boost::variant > cmd_vartype; +typedef boost::variant, + std::vector> cmd_vartype; typedef std::map cmdmap_t; void dump_cmd_to_json(ceph::Formatter *f, const std::string& cmd);