]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
common/config: move parse_value() into Option
authorSage Weil <sage@redhat.com>
Mon, 13 Nov 2017 15:25:07 +0000 (09:25 -0600)
committerSage Weil <sage@redhat.com>
Tue, 6 Mar 2018 20:44:09 +0000 (14:44 -0600)
It is more reuable there.

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

index 0c37194f052429c65853689c5b27cffc4198ef62..b6394e1a09877d61cd50f9afb37ac3e44b7fda53 100644 (file)
@@ -1061,69 +1061,12 @@ int md_config_t::set_val_impl(const std::string &raw_val, const Option &opt,
 {
   assert(lock.is_locked());
 
-  std::string val = raw_val;
-
-  int r = opt.pre_validate(&val, error_message);
-  if (r != 0) {
-    return r;
-  }
-
   Option::value_t new_value;
-  if (opt.type == Option::TYPE_INT) {
-    int64_t f = strict_si_cast<int64_t>(val.c_str(), error_message);
-    if (!error_message->empty()) {
-      return -EINVAL;
-    }
-    new_value = f;
-  } else if (opt.type == Option::TYPE_UINT) {
-    uint64_t f = strict_si_cast<uint64_t>(val.c_str(), error_message);
-    if (!error_message->empty()) {
-      return -EINVAL;
-    }
-    new_value = f;
-  } else if (opt.type == Option::TYPE_STR) {
-    new_value = val;
-  } else if (opt.type == Option::TYPE_FLOAT) {
-    double f = strict_strtod(val.c_str(), error_message);
-    if (!error_message->empty()) {
-      return -EINVAL;
-    } else {
-      new_value = f;
-    }
-  } else if (opt.type == Option::TYPE_BOOL) {
-    if (strcasecmp(val.c_str(), "false") == 0) {
-      new_value = false;
-    } else if (strcasecmp(val.c_str(), "true") == 0) {
-      new_value = true;
-    } else {
-      int b = strict_strtol(val.c_str(), 10, error_message);
-      if (!error_message->empty()) {
-       return -EINVAL;
-      }
-      new_value = !!b;
-    }
-  } else if (opt.type == Option::TYPE_ADDR) {
-    entity_addr_t addr;
-    if (!addr.parse(val.c_str())){
-      return -EINVAL;
-    }
-    new_value = addr;
-  } else if (opt.type == Option::TYPE_UUID) {
-    uuid_d uuid;
-    if (!uuid.parse(val.c_str())) {
-      return -EINVAL;
-    }
-    new_value = uuid;
-  } else {
-    ceph_abort();
-  }
-
-  r = opt.validate(new_value, error_message);
-  if (r != 0) {
+  int r = opt.parse_value(raw_val, &new_value, error_message);
+  if (r < 0) {
     return r;
   }
 
-
   // Apply the value to its entry in the `values` map
   values[opt.name] = new_value;
 
index 11fb97a369fe654c78bfabc786c331c84dbfbd73..f9ead98f7c9ed0f125189b9a129fb1c76a23cc21 100644 (file)
@@ -88,6 +88,75 @@ int Option::validate(const Option::value_t &new_value, std::string *err) const
   return 0;
 }
 
+int Option::parse_value(
+  const std::string& raw_val,
+  value_t *out,
+  std::string *error_message) const
+{
+  std::string val = raw_val;
+
+  int r = pre_validate(&val, error_message);
+  if (r != 0) {
+    return r;
+  }
+
+  if (type == Option::TYPE_INT) {
+    int64_t f = strict_si_cast<int64_t>(val.c_str(), error_message);
+    if (!error_message->empty()) {
+      return -EINVAL;
+    }
+    *out = f;
+  } else if (type == Option::TYPE_UINT) {
+    uint64_t f = strict_si_cast<uint64_t>(val.c_str(), error_message);
+    if (!error_message->empty()) {
+      return -EINVAL;
+    }
+    *out = f;
+  } else if (type == Option::TYPE_STR) {
+    *out = val;
+  } else if (type == Option::TYPE_FLOAT) {
+    double f = strict_strtod(val.c_str(), error_message);
+    if (!error_message->empty()) {
+      return -EINVAL;
+    } else {
+      *out = f;
+    }
+  } else if (type == Option::TYPE_BOOL) {
+    if (strcasecmp(val.c_str(), "false") == 0) {
+      *out = false;
+    } else if (strcasecmp(val.c_str(), "true") == 0) {
+      *out = true;
+    } else {
+      int b = strict_strtol(val.c_str(), 10, error_message);
+      if (!error_message->empty()) {
+       return -EINVAL;
+      }
+      *out = !!b;
+    }
+  } else if (type == Option::TYPE_ADDR) {
+    entity_addr_t addr;
+    if (!addr.parse(val.c_str())){
+      return -EINVAL;
+    }
+    *out = addr;
+  } else if (type == Option::TYPE_UUID) {
+    uuid_d uuid;
+    if (!uuid.parse(val.c_str())) {
+      return -EINVAL;
+    }
+    *out = uuid;
+  } else {
+    ceph_abort();
+  }
+
+  r = validate(*out, error_message);
+  if (r != 0) {
+    return r;
+  }
+
+  return 0;
+}
+
 void Option::dump(Formatter *f) const
 {
   f->open_object_section("option");
index 28648a5bc97044d93295c80101798dcaf96bf2dd..a224d00c173efc89bb5544bdeebe90281f93bc0c 100644 (file)
@@ -182,6 +182,12 @@ struct Option {
     return *this;
   }
 
+  /// parse and validate a string input
+  int parse_value(
+    const std::string& raw_val,
+    value_t *out,
+    std::string *error_message) const;
+
   template<typename T>
   Option& set_default(const T& v) {
     return set_value(value, v);