From 65068e24023299ef6e547b2ebbb44f5839c49bb5 Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Thu, 13 Jul 2017 17:07:18 -0400 Subject: [PATCH] common/cmdparse: allow vector Signed-off-by: Sage Weil --- src/common/cmdparse.cc | 22 ++++++++++++++++++++-- src/common/cmdparse.h | 3 ++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/common/cmdparse.cc b/src/common/cmdparse.cc index 5a8e78e08f22c..592b889b4597f 100644 --- a/src/common/cmdparse.cc +++ b/src/common/cmdparse.cc @@ -185,6 +185,15 @@ void cmdmap_dump(const cmdmap_t &cmdmap, Formatter *f) } f->close_section(); } + + void operator()(const std::vector &operand) const + { + f->open_array_section(key.c_str()); + for (const auto i : operand) { + f->dump_float("item", i); + } + f->close_section(); + } }; //f->open_object_section("cmdmap"); @@ -240,7 +249,7 @@ 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 or int64_t, the only types we handle. + // of strings, doubles, 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 @@ -265,9 +274,18 @@ cmdmap_from_json(vector cmd, map *mapp, stringstrea outv.push_back(sv.get_int64()); } (*mapp)[it->first] = std::move(outv); + } else if (spvals.front().type() == json_spirit::real_type) { + vector outv; + for (const auto& sv : spvals) { + if (spvals.front().type() != json_spirit::real_type) { + throw(runtime_error("Can't handle arrays of multiple types")); + } + outv.push_back(sv.get_real()); + } + (*mapp)[it->first] = std::move(outv); } else { throw(runtime_error("Can't handle arrays of types other than " - "int or string")); + "int, string, or double")); } } break; diff --git a/src/common/cmdparse.h b/src/common/cmdparse.h index 35afc24ab17a1..41495f5551a28 100644 --- a/src/common/cmdparse.h +++ b/src/common/cmdparse.h @@ -21,7 +21,8 @@ typedef boost::variant, - std::vector> cmd_vartype; + std::vector, + std::vector> cmd_vartype; typedef std::map cmdmap_t; std::string cmddesc_get_prefix(const std::string &cmddesc); -- 2.39.5