From 8bd7915717ed837f16849dfe87f3d1bdb6152425 Mon Sep 17 00:00:00 2001 From: Yehuda Sadeh Date: Tue, 12 Feb 2013 14:44:24 -0800 Subject: [PATCH] rgw: generic decode_json for containers Signed-off-by: Yehuda Sadeh --- src/common/ceph_json.h | 37 ++++++++++++++++++++++++++++ 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 13e6a1b0d7748..53a2aefbf80a7 100644 --- a/src/common/ceph_json.h +++ b/src/common/ceph_json.h @@ -109,6 +109,9 @@ public: template static bool decode_json(const char *name, T& val, JSONObj *obj, bool mandatory = false); + template + static bool decode_json(const char *name, C& container, void (*cb)(C&, JSONObj *obj), JSONObj *obj, bool mandatory = false); + template static void decode_json(const char *name, T& val, T& default_val, JSONObj *obj); }; @@ -143,6 +146,17 @@ void decode_json_obj(list& l, JSONObj *obj) } } +template +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 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 +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 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 4cccadc5ac0d1..b9e52c4426002 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& 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& m, JSONObj *o) +{ + RGWAccessKey k; + k.decode_json(o, true); + m[k.subuser] = k; +} + +static void decode_subusers(map& 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 akeys_list; - JSONDecoder::decode_json("keys", akeys_list, obj); - - list::iterator iter; - for (iter = akeys_list.begin(); iter != akeys_list.end(); ++iter) { - RGWAccessKey& e = *iter; - access_keys[e.id] = e; - } - - list skeys_list; - list::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 susers_list; - list::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); } -- 2.39.5