]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw: generic decode_json for containers
authorYehuda Sadeh <yehuda@inktank.com>
Tue, 12 Feb 2013 22:44:24 +0000 (14:44 -0800)
committerYehuda Sadeh <yehuda@inktank.com>
Fri, 22 Mar 2013 18:23:08 +0000 (11:23 -0700)
Signed-off-by: Yehuda Sadeh <yehuda@inktank.com>
src/common/ceph_json.h
src/rgw/rgw_json_enc.cc

index 13e6a1b0d77489c05c1d4e0e9453772035063ef3..53a2aefbf80a70c6b501ea7b9a04d7897547f387 100644 (file)
@@ -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)
 {
index 4cccadc5ac0d16e6c75d6593376dbc77a9f12a4e..b9e52c44260025369180fbc7bcaa40b230e0834e 100644 (file)
@@ -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);
 }