]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
ceph_json: refine Formattable api
authorYehuda Sadeh <yehuda@redhat.com>
Tue, 12 Dec 2017 18:10:15 +0000 (10:10 -0800)
committerYehuda Sadeh <yehuda@redhat.com>
Thu, 12 Apr 2018 22:38:36 +0000 (15:38 -0700)
Signed-off-by: Yehuda Sadeh <yehuda@redhat.com>
src/common/ceph_json.cc
src/common/ceph_json.h
src/rgw/rgw_sync_module_aws.cc
src/rgw/rgw_sync_module_es.cc
src/rgw/rgw_sync_module_log.cc

index 102de1768758f3b6c21c9ddf8a61c5da9c7525ac..13ddcf4f78af6020748f54aa19bfa899cff05e68 100644 (file)
@@ -540,41 +540,55 @@ bool JSONFormattable::find(const string& name, string *val) const
   if (i == obj.end()) {
     return false;
   }
-  *val = i->second;
+  *val = i->second.val();
   return true;
 }
 
-string JSONFormattable::get(const string& name, const string& def_val) const
-{
-  string s;
-  if (find(name, &s)) {
-    return s;
+int JSONFormattable::val_int() const {
+  return atoi(str.c_str());
+}
+
+bool JSONFormattable::val_bool() const {
+  return (boost::iequals(str, "true") ||
+          boost::iequals(str, "on") ||
+          boost::iequals(str, "yes") ||
+          boost::iequals(str, "1"));
+}
+
+string JSONFormattable::def(const string& def_val) const {
+  if (type == FMT_NONE) {
+    return def_val;
   }
+  return val();
+}
 
-  return def_val;
+int JSONFormattable::def(int def_val) const {
+  if (type == FMT_NONE) {
+    return def_val;
+  }
+  return val_int();
 }
 
-int JSONFormattable::get_int(const string& name, int def_val) const
-{
-  string s;
-  if (find(name, &s)) {
-    return atoi(s.c_str());
+bool JSONFormattable::def(bool def_val) const {
+  if (type == FMT_NONE) {
+    return def_val;
   }
+  return val_bool();
+}
 
-  return def_val;
+string JSONFormattable::get(const string& name, const string& def_val) const
+{
+  return (*this)[name].def(def_val);
 }
 
-bool JSONFormattable::get_bool(const string& name, bool def_val) const
+int JSONFormattable::get_int(const string& name, int def_val) const
 {
-  string s;
-  if (find(name, &s)) {
-    return (boost::iequals(s, "true") ||
-            boost::iequals(s, "on") ||
-            boost::iequals(s, "yes") ||
-            boost::iequals(s, "1"));
-  }
+  return (*this)[name].def(def_val);
+}
 
-  return def_val;
+bool JSONFormattable::get_bool(const string& name, bool def_val) const
+{
+  return (*this)[name].def(def_val);
 }
 
 void encode_json(const char *name, const JSONFormattable& v, Formatter *f)
index 3299d4792ce9145abeeabda5829b9577154b1aaf..4e203e01d313ad6fdc46b4413b8277744a4d39b9 100644 (file)
@@ -474,9 +474,9 @@ struct JSONFormattable {
     FMT_ARRAY,
     FMT_OBJ,
   } type{FMT_NONE};
-  string str;
+  std::string str;
   vector<JSONFormattable> arr;
-  map<string, JSONFormattable> obj;
+  map<std::string, JSONFormattable> obj;
 
   void decode_json(JSONObj *jo) {
     if (jo->is_array()) {
@@ -514,25 +514,47 @@ struct JSONFormattable {
     decode(obj, bl);
     DECODE_FINISH(bl);
   }
-  const string& val() const {
+  const std::string& val() const {
     return str;
   }
 
+  int val_int() const;
+  bool val_bool() const;
+
   const vector<JSONFormattable>& array() const {
     return arr;
   }
 
-  const JSONFormattable& operator[](const string& name) const;
+  const JSONFormattable& operator[](const std::string& name) const;
+  const JSONFormattable& operator[](const char * name) const {
+    return this->operator[](std::string(name));
+  }
 
-  operator string() const {
-    return val();
+  string operator ()(const char *def_val) const {
+    return def(string(def_val));
   }
 
-  bool find(const string& name, string *val) const;
+  string operator ()(const string& def_val) const {
+    return def(def_val);
+  }
+
+  int operator()(int def_val) const {
+    return def(def_val);
+  }
+
+  bool operator()(bool def_val) const {
+    return def(def_val);
+  }
+
+  std::string def(const std::string& def_val) const;
+  int def(int def_val) const;
+  bool def(bool def_val) const;
+
+  bool find(const std::string& name, std::string *val) const;
 
-  string get(const string& name, const string& def_val) const;
-  int get_int(const string& name, int def_val) const;
-  bool get_bool(const string& name, bool def_val) const;
+  std::string get(const std::string& name, const std::string& def_val) const;
+  int get_int(const std::string& name, int def_val) const;
+  bool get_bool(const std::string& name, bool def_val) const;
 };
 WRITE_CLASS_ENCODER(JSONFormattable)
 
index 30d3aa74d9526c98c089af95b7313025b8434d85..cf611c91710ffe72650e2491aaa1d993ec29552e 100644 (file)
@@ -969,10 +969,10 @@ static int conf_to_uint64(CephContext *cct, const JSONFormattable& config, const
 int RGWAWSSyncModule::create_instance(CephContext *cct, const JSONFormattable& config,  RGWSyncModuleInstanceRef *instance){
   AWSSyncConfig conf;
 
-  conf.s3_endpoint = config["s3_endpoint"];
+  conf.s3_endpoint = config["s3_endpoint"]("");
 
-  string access_key = config["access_key"];
-  string secret = config["secret"];
+  string access_key = config["access_key"]("");
+  string secret = config["secret"]("");
 
   conf.key = RGWAccessKey(access_key, secret);
 
index cd219fffc2c5a79514903cd6616d4e6b0997ae0b..2ef7aab0425e56385e178a705a308e22cdc61e7e 100644 (file)
@@ -609,7 +609,7 @@ RGWRESTMgr *RGWElasticSyncModuleInstance::get_rest_filter(int dialect, RGWRESTMg
 }
 
 int RGWElasticSyncModule::create_instance(CephContext *cct, const JSONFormattable& config, RGWSyncModuleInstanceRef *instance) {
-  string endpoint = config["endpoint"];
+  string endpoint = config["endpoint"]("");
   instance->reset(new RGWElasticSyncModuleInstance(cct, config));
   return 0;
 }
index 0378c040b6affea5b8ebff6e75c44e83d015c7a7..5e0f8d9e2ee0d3716b32efaf4aa793d9f81a2889 100644 (file)
@@ -64,7 +64,7 @@ public:
 };
 
 int RGWLogSyncModule::create_instance(CephContext *cct, const JSONFormattable& config, RGWSyncModuleInstanceRef *instance) {
-  string prefix = config["prefix"];
+  string prefix = config["prefix"]("");
   instance->reset(new RGWLogSyncModuleInstance(prefix));
   return 0;
 }