diff options
author | Yehuda Sadeh <yehuda@inktank.com> | 2013-02-12 14:44:24 -0800 |
---|---|---|
committer | Yehuda Sadeh <yehuda@inktank.com> | 2013-02-12 14:44:24 -0800 |
commit | 627fc44cb4883099dca44dab31508d6dc7224ec0 (patch) | |
tree | dce83df98e86cbe083f3cfc81f0850191463b8a6 | |
parent | 063bfff89c129430e14ef93da4fc255f5d7ae4c4 (diff) | |
download | ceph-627fc44cb4883099dca44dab31508d6dc7224ec0.tar.gz |
rgw: generic decode_json for containers
Signed-off-by: Yehuda Sadeh <yehuda@inktank.com>
-rw-r--r-- | src/common/ceph_json.h | 37 | ||||
-rw-r--r-- | src/rgw/rgw_json_enc.cc | 54 |
2 files changed, 59 insertions, 32 deletions
diff --git a/src/common/ceph_json.h b/src/common/ceph_json.h index 13e6a1b0d77..53a2aefbf80 100644 --- a/src/common/ceph_json.h +++ b/src/common/ceph_json.h @@ -109,6 +109,9 @@ public: template<class T> static bool decode_json(const char *name, T& val, JSONObj *obj, bool mandatory = false); + template<class C> + static bool decode_json(const char *name, C& container, void (*cb)(C&, JSONObj *obj), JSONObj *obj, bool mandatory = false); + template<class T> static void decode_json(const char *name, T& val, T& default_val, JSONObj *obj); }; @@ -143,6 +146,17 @@ void decode_json_obj(list<T>& l, JSONObj *obj) } } +template<class C> +void decode_json_obj(C& container, void (*cb)(C&, JSONObj *obj), JSONObj *obj) +{ + JSONObjIter iter = obj->find_first(); + + for (; !iter.end(); ++iter) { + JSONObj *o = *iter; + cb(container, o); + } +} + template<class T> bool JSONDecoder::decode_json(const char *name, T& val, JSONObj *obj, bool mandatory) { @@ -166,6 +180,29 @@ bool JSONDecoder::decode_json(const char *name, T& val, JSONObj *obj, bool manda return true; } +template<class C> +bool JSONDecoder::decode_json(const char *name, C& container, void (*cb)(C&, JSONObj *), JSONObj *obj, bool mandatory) +{ + JSONObjIter iter = obj->find_first(name); + if (iter.end()) { + if (mandatory) { + string s = "missing mandatory field " + string(name); + throw err(s); + } + return false; + } + + try { + decode_json_obj(container, cb, *iter); + } catch (err& e) { + string s = string(name) + ": "; + s.append(e.message); + throw err(s); + } + + return true; +} + template<class T> void JSONDecoder::decode_json(const char *name, T& val, T& default_val, JSONObj *obj) { diff --git a/src/rgw/rgw_json_enc.cc b/src/rgw/rgw_json_enc.cc index 4cccadc5ac0..b9e52c44260 100644 --- a/src/rgw/rgw_json_enc.cc +++ b/src/rgw/rgw_json_enc.cc @@ -343,13 +343,26 @@ void RGWUserInfo::dump(Formatter *f) const } -struct SwiftKeyEntry { - RGWAccessKey key; +static void decode_access_keys(map<string, RGWAccessKey>& m, JSONObj *o) +{ + RGWAccessKey k; + k.decode_json(o); + m[k.id] = k; +} - void decode_json(JSONObj *obj) { - key.decode_json(obj, true); - } -}; +static void decode_swift_keys(map<string, RGWAccessKey>& m, JSONObj *o) +{ + RGWAccessKey k; + k.decode_json(o, true); + m[k.subuser] = k; +} + +static void decode_subusers(map<string, RGWSubUser>& m, JSONObj *o) +{ + RGWSubUser u; + u.decode_json(o); + m[u.name] = u; +} void RGWUserInfo::decode_json(JSONObj *obj) { @@ -362,32 +375,9 @@ void RGWUserInfo::decode_json(JSONObj *obj) JSONDecoder::decode_json("max_buckets", max_buckets, obj); JSONDecoder::decode_json("auid", auid, obj); - list<RGWAccessKey> akeys_list; - JSONDecoder::decode_json("keys", akeys_list, obj); - - list<RGWAccessKey>::iterator iter; - for (iter = akeys_list.begin(); iter != akeys_list.end(); ++iter) { - RGWAccessKey& e = *iter; - access_keys[e.id] = e; - } - - list<SwiftKeyEntry> skeys_list; - list<SwiftKeyEntry>::iterator skiter; - JSONDecoder::decode_json("swift_keys", skeys_list, obj); - - for (skiter = skeys_list.begin(); skiter != skeys_list.end(); ++skiter) { - SwiftKeyEntry& e = *skiter; - swift_keys[e.key.subuser] = e.key; - } - - list<RGWSubUser> susers_list; - list<RGWSubUser>::iterator siter; - JSONDecoder::decode_json("subusers", susers_list, obj); - - for (siter = susers_list.begin(); siter != susers_list.end(); ++siter) { - RGWSubUser& e = *siter; - subusers[e.name] = e; - } + JSONDecoder::decode_json("keys", access_keys, decode_access_keys, obj); + JSONDecoder::decode_json("swift_keys", swift_keys, decode_swift_keys, obj); + JSONDecoder::decode_json("subusers", subusers, decode_subusers, obj); JSONDecoder::decode_json("caps", caps, obj); } |