]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
common/cmdparse: allow vector<double>
authorSage Weil <sage@redhat.com>
Thu, 13 Jul 2017 21:07:18 +0000 (17:07 -0400)
committerSage Weil <sage@redhat.com>
Fri, 21 Jul 2017 17:50:51 +0000 (13:50 -0400)
Signed-off-by: Sage Weil <sage@redhat.com>
src/common/cmdparse.cc
src/common/cmdparse.h

index 5a8e78e08f22ca3e2a40a16d51d1a8a2568a9b67..592b889b4597f83ab3decc9a162f75da1aba5c47 100644 (file)
@@ -185,6 +185,15 @@ void cmdmap_dump(const cmdmap_t &cmdmap, Formatter *f)
       }
       f->close_section();
     }
+
+    void operator()(const std::vector<double> &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<string> cmd, map<string, cmd_vartype> *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<json_spirit::mValue>& 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<string> cmd, map<string, cmd_vartype> *mapp, stringstrea
              outv.push_back(sv.get_int64());
            }
            (*mapp)[it->first] = std::move(outv);
+         } else if (spvals.front().type() == json_spirit::real_type) {
+           vector<double> 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;
index 35afc24ab17a1b5d36e9131503ddbc33c0eae1b5..41495f5551a283c3628c86b608fdf3e13216931c 100644 (file)
@@ -21,7 +21,8 @@ typedef boost::variant<std::string,
                       int64_t,
                       double,
                       std::vector<std::string>,
-                      std::vector<int64_t>>  cmd_vartype;
+                      std::vector<int64_t>,
+                      std::vector<double>>  cmd_vartype;
 typedef std::map<std::string, cmd_vartype> cmdmap_t;
 
 std::string cmddesc_get_prefix(const std::string &cmddesc);