ovsdb-data: Simplify converting an OVSDB datum to JSON by reordering logic.
authorBen Pfaff <blp@nicira.com>
Mon, 22 Aug 2011 21:52:21 +0000 (14:52 -0700)
committerBen Pfaff <blp@nicira.com>
Wed, 24 Aug 2011 18:57:42 +0000 (11:57 -0700)
Putting the "map" case first avoids duplicate tests.

Suggested-by: Reid Price <reid@nicira.com>
lib/ovsdb-data.c
python/ovs/db/data.py

index 54ce72cf719c03baf3b3303df5f685f9fee8087c..1781811917d2ee04df5f190da7622df3073a8e91 100644 (file)
@@ -1278,30 +1278,30 @@ struct json *
 ovsdb_datum_to_json(const struct ovsdb_datum *datum,
                     const struct ovsdb_type *type)
 {
-    if (datum->n == 1 && !ovsdb_type_is_map(type)) {
-        return ovsdb_atom_to_json(&datum->keys[0], type->key.type);
-    } else if (type->value.type == OVSDB_TYPE_VOID) {
+    if (ovsdb_type_is_map(type)) {
         struct json **elems;
         size_t i;
 
         elems = xmalloc(datum->n * sizeof *elems);
         for (i = 0; i < datum->n; i++) {
-            elems[i] = ovsdb_atom_to_json(&datum->keys[i], type->key.type);
+            elems[i] = json_array_create_2(
+                ovsdb_atom_to_json(&datum->keys[i], type->key.type),
+                ovsdb_atom_to_json(&datum->values[i], type->value.type));
         }
 
-        return wrap_json("set", json_array_create(elems, datum->n));
+        return wrap_json("map", json_array_create(elems, datum->n));
+    } else if (datum->n == 1) {
+        return ovsdb_atom_to_json(&datum->keys[0], type->key.type);
     } else {
         struct json **elems;
         size_t i;
 
         elems = xmalloc(datum->n * sizeof *elems);
         for (i = 0; i < datum->n; i++) {
-            elems[i] = json_array_create_2(
-                ovsdb_atom_to_json(&datum->keys[i], type->key.type),
-                ovsdb_atom_to_json(&datum->values[i], type->value.type));
+            elems[i] = ovsdb_atom_to_json(&datum->keys[i], type->key.type);
         }
 
-        return wrap_json("map", json_array_create(elems, datum->n));
+        return wrap_json("set", json_array_create(elems, datum->n));
     }
 }
 
index b68d4e32f8b4c8534979a0643b5c7b106351b9a2..9530fb9cdfc5b873d00a2de0ed9e0caa636ab216 100644 (file)
@@ -325,14 +325,14 @@ class Datum(object):
             return Datum(type_, {keyAtom: None})
 
     def to_json(self):
-        if len(self.values) == 1 and not self.type.is_map():
+        if self.type.is_map():
+            return ["map", [[k.to_json(), v.to_json()]
+                            for k, v in sorted(self.values.items())]]
+        elif len(self.values) == 1:
             key = self.values.keys()[0]
             return key.to_json()
-        elif not self.type.is_map():
-            return ["set", [k.to_json() for k in sorted(self.values.keys())]]
         else:
-            return ["map", [[k.to_json(), v.to_json()]
-                            for k, v in sorted(self.values.items())]]
+            return ["set", [k.to_json() for k in sorted(self.values.keys())]]
 
     def to_string(self):
         head = tail = None