From 32e77797c7eba8c735be59b6326427f1e7b53056 Mon Sep 17 00:00:00 2001 From: Yehuda Sadeh Date: Tue, 12 Dec 2017 10:10:15 -0800 Subject: [PATCH] ceph_json: refine Formattable api Signed-off-by: Yehuda Sadeh --- src/common/ceph_json.cc | 58 +++++++++++++++++++++------------- src/common/ceph_json.h | 42 ++++++++++++++++++------ src/rgw/rgw_sync_module_aws.cc | 6 ++-- src/rgw/rgw_sync_module_es.cc | 2 +- src/rgw/rgw_sync_module_log.cc | 2 +- 5 files changed, 73 insertions(+), 37 deletions(-) diff --git a/src/common/ceph_json.cc b/src/common/ceph_json.cc index 102de176875..13ddcf4f78a 100644 --- a/src/common/ceph_json.cc +++ b/src/common/ceph_json.cc @@ -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) diff --git a/src/common/ceph_json.h b/src/common/ceph_json.h index 3299d4792ce..4e203e01d31 100644 --- a/src/common/ceph_json.h +++ b/src/common/ceph_json.h @@ -474,9 +474,9 @@ struct JSONFormattable { FMT_ARRAY, FMT_OBJ, } type{FMT_NONE}; - string str; + std::string str; vector arr; - map obj; + map 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& 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) diff --git a/src/rgw/rgw_sync_module_aws.cc b/src/rgw/rgw_sync_module_aws.cc index 30d3aa74d95..cf611c91710 100644 --- a/src/rgw/rgw_sync_module_aws.cc +++ b/src/rgw/rgw_sync_module_aws.cc @@ -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); diff --git a/src/rgw/rgw_sync_module_es.cc b/src/rgw/rgw_sync_module_es.cc index cd219fffc2c..2ef7aab0425 100644 --- a/src/rgw/rgw_sync_module_es.cc +++ b/src/rgw/rgw_sync_module_es.cc @@ -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; } diff --git a/src/rgw/rgw_sync_module_log.cc b/src/rgw/rgw_sync_module_log.cc index 0378c040b6a..5e0f8d9e2ee 100644 --- a/src/rgw/rgw_sync_module_log.cc +++ b/src/rgw/rgw_sync_module_log.cc @@ -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; } -- 2.39.5