]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/options: fix stringification
authorSage Weil <sage@redhat.com>
Wed, 17 Jan 2018 20:34:27 +0000 (14:34 -0600)
committerSage Weil <sage@redhat.com>
Tue, 6 Mar 2018 20:44:49 +0000 (14:44 -0600)
There is a generic operator<< for boost::variant--we can't overload it
for Option::value_t.  Make a to_str() method instead.

Add a parse_option() output arg for the normalized value for convenience.

Signed-off-by: Sage Weil <sage@redhat.com>
src/common/config.cc
src/common/options.cc
src/common/options.h
src/mon/ConfigMonitor.cc

index 7f8005617640141a792ad2f99ff101585c7d4d78..5693dd5d32a2965b00c9485997ba4f9ebaf64477 100644 (file)
@@ -92,17 +92,7 @@ static int conf_stringify(const Option::value_t& v, string *out)
   if (boost::get<boost::blank>(&v)) {
     return -ENOENT;
   }
-  if (const bool *flag = boost::get<const bool>(&v)) {
-    *out = *flag ? "true" : "false";
-    return 0;
-  }
-  ostringstream oss;
-  if (const double *dp = boost::get<const double>(&v)) {
-    oss << std::fixed << *dp;
-  } else {
-    oss << v;
-  }
-  *out = oss.str();
+  *out = Option::to_str(v);
   return 0;
 }
 
@@ -903,7 +893,7 @@ void md_config_t::get_defaults_bl(bufferlist *bl)
       if (j != values.end()) {
        auto k = j->second.find(CONF_DEFAULT);
        if (k != j->second.end()) {
-         encode(stringify(k->second), bl);
+         encode(Option::to_str(k->second), bl);
          continue;
        }
       }
@@ -933,7 +923,7 @@ void md_config_t::get_config_bl(bufferlist *bl)
       encode((uint32_t)i.second.size(), bl);
       for (auto& j : i.second) {
        encode(j.first, bl);
-       encode(stringify(j.second), bl);
+       encode(Option::to_str(j.second), bl);
       }
     }
     // make sure overridden items appear, and include the default value
index 6e837638f449cfa6c6222a6498eb407142f7e6eb..88fb0689521f28a4556285395f629c12bb9d44d3 100644 (file)
@@ -91,7 +91,8 @@ int Option::validate(const Option::value_t &new_value, std::string *err) const
 int Option::parse_value(
   const std::string& raw_val,
   value_t *out,
-  std::string *error_message) const
+  std::string *error_message,
+  std::string *normalized_value) const
 {
   std::string val = raw_val;
 
@@ -131,7 +132,7 @@ int Option::parse_value(
       if (!error_message->empty()) {
        return -EINVAL;
       }
-      *out = !!b;
+      *out = (bool)!!b;
     }
   } else if (type == Option::TYPE_ADDR) {
     entity_addr_t addr;
@@ -154,6 +155,9 @@ int Option::parse_value(
     return r;
   }
 
+  if (normalized_value) {
+    *normalized_value = to_str(*out);
+  }
   return 0;
 }
 
@@ -203,35 +207,20 @@ void Option::dump(Formatter *f) const
   f->dump_bool("can_update_at_runtime", can_update_at_runtime());
 }
 
-ostream& operator<<(ostream& out, const Option::value_t& v)
+std::string Option::to_str(const Option::value_t& v)
 {
   if (boost::get<boost::blank>(&v)) {
-    return out;
+    return string();
   }
   if (const bool *flag = boost::get<const bool>(&v)) {
-    return out << (*flag ? "true" : "false");
+    return *flag ? "true" : "false";
   }
   if (const double *dp = boost::get<const double>(&v)) {
     ostringstream oss;
     oss << std::fixed << *dp;
-    return out << oss.str();
-  }
-  if (const uint64_t *i = boost::get<const uint64_t>(&v)) {
-    return out << *i;
-  }
-  if (const int64_t *i = boost::get<const int64_t>(&v)) {
-    return out << *i;
-  }
-  if (const std::string *i = boost::get<const std::string>(&v)) {
-    return out << *i;
-  }
-  if (const uuid_d *i = boost::get<const uuid_d>(&v)) {
-    return out << *i;
-  }
-  if (const entity_addr_t *i = boost::get<const entity_addr_t>(&v)) {
-    return out << *i;
+    return oss.str();
   }
-  ceph_abort();
+  return stringify(v);
 }
 
 void Option::print(ostream *out) const
index 27a8a3df6eec96b7519cbc01dbf0accef2c3ae98..80b050fa6e707c07f9f4898b1f08405fe303be3b 100644 (file)
@@ -86,7 +86,7 @@ struct Option {
   value_t value;
   value_t daemon_value;
 
-  friend ostream& operator<<(ostream&, const value_t& v);
+  static std::string to_str(const value_t& v);
 
   // Items like mon, osd, rgw, rbd, ceph-fuse.  This is advisory metadata
   // for presentation layers (like web dashboards, or generated docs), so that
@@ -195,7 +195,8 @@ struct Option {
   int parse_value(
     const std::string& raw_val,
     value_t *out,
-    std::string *error_message) const;
+    std::string *error_message,
+    std::string *normalized_value=nullptr) const;
 
   template<typename T>
   Option& set_default(const T& v) {
index cf412e1985ab6793ffeaa4c0e52216a30d0be767..55025d35ae1b00a4d722f2821a98364e276f1329 100644 (file)
@@ -358,13 +358,12 @@ bool ConfigMonitor::prepare_command(MonOpRequestRef op)
       }
 
       Option::value_t real_value;
-      string str;
-      err = opt->parse_value(value, &real_value, &str);
+      string errstr;
+      err = opt->parse_value(value, &real_value, &errstr, &value);
       if (err < 0) {
-       ss << "error parsing value: " << str;
+       ss << "error parsing value: " << errstr;
        goto reply;
       }
-      value = stringify(real_value);
     }
 
     string section;