From: Kefu Chai Date: Wed, 14 Feb 2018 15:26:28 +0000 (+0800) Subject: common/admin_socket: consolidate dump_cmd_to_json() and validate_cmd() X-Git-Tag: v13.0.2~254^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=21fdfc25a26cf131850dd65a0288cac61306b99d;p=ceph.git common/admin_socket: consolidate dump_cmd_to_json() and validate_cmd() Signed-off-by: Kefu Chai --- diff --git a/src/common/cmdparse.cc b/src/common/cmdparse.cc index f6c1a968fe6..473ad10cde3 100644 --- a/src/common/cmdparse.cc +++ b/src/common/cmdparse.cc @@ -41,6 +41,28 @@ std::string cmddesc_get_prefix(const std::string &cmddesc) return result.str(); } +using arg_desc_t = std::map; + +// Snarf up all the key=val,key=val pairs, put 'em in a dict. +template +arg_desc_t cmddesc_get_args(const String& cmddesc) +{ + arg_desc_t arg_desc; + for_each_substr(cmddesc, ",", [&](auto kv) { + // key=value; key by itself implies value is bool true + // name="name" means arg dict will be titled 'name' + auto equal = kv.find('='); + if (equal == kv.npos) { + // it should be the command + return; + } + auto key = kv.substr(0, equal); + auto val = kv.substr(equal + 1); + arg_desc[key] = val; + }); + return arg_desc; +} + /** * Read a command description list out of cmd, and dump it to f. * A signature description is a set of space-separated words; @@ -63,33 +85,13 @@ dump_cmd_to_json(Formatter *f, const string& cmd) f->dump_string("arg", word); continue; } - // Snarf up all the key=val,key=val pairs, put 'em in a dict. - // no '=val' implies '=True'. - std::stringstream argdesc(word); - std::string keyval; - std::mapdesckv; // accumulate descriptor keywords in desckv - - while (std::getline(argdesc, keyval, ',')) { - // key=value; key by itself implies value is bool true - // name="name" means arg dict will be titled 'name' - size_t pos = keyval.find('='); - std::string key, val; - if (pos != std::string::npos) { - key = keyval.substr(0, pos); - val = keyval.substr(pos+1); - } else { - key = keyval; - val = true; - } - desckv.insert(std::pair (key, val)); - } + auto desckv = cmddesc_get_args(word); // name the individual desc object based on the name key - f->open_object_section(desckv["name"].c_str()); + f->open_object_section(string(desckv["name"]).c_str()); // dump all the keys including name into the array - for (std::map::iterator it = desckv.begin(); - it != desckv.end(); ++it) { - f->dump_string(it->first.c_str(), it->second); + for (auto [key, value] : desckv) { + f->dump_string(string(key).c_str(), string(value)); } f->close_section(); // attribute object for individual desc } @@ -422,8 +424,6 @@ T str_to_num(const std::string& s) } } -using arg_desc_t = std::map; - template bool arg_in_range(T value, const arg_desc_t& desc, std::ostream& os) { auto range = desc.find("range"); @@ -523,17 +523,7 @@ bool validate_cmd(CephContext* cct, std::ostream& os) { return !find_first_in(desc, " ", [&](auto desc) { - arg_desc_t arg_desc; - for_each_substr(desc, ",", [&](auto kv) { - auto equal = kv.find('='); - if (equal == kv.npos) { - // it should be the command - return; - } - auto key = kv.substr(0, equal); - auto val = kv.substr(equal + 1); - arg_desc[key] = val; - }); + auto arg_desc = cmddesc_get_args(desc); if (arg_desc.empty()) { return false; }