From: Ben Pfaff Date: Mon, 22 Aug 2011 21:52:21 +0000 (-0700) Subject: ovsdb-data: Simplify converting an OVSDB datum to JSON by reordering logic. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6a6f8d167357a1f1751cdd54b9491c531a371ae1;p=openvswitch ovsdb-data: Simplify converting an OVSDB datum to JSON by reordering logic. Putting the "map" case first avoids duplicate tests. Suggested-by: Reid Price --- diff --git a/lib/ovsdb-data.c b/lib/ovsdb-data.c index 54ce72cf..17818119 100644 --- a/lib/ovsdb-data.c +++ b/lib/ovsdb-data.c @@ -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)); } } diff --git a/python/ovs/db/data.py b/python/ovs/db/data.py index b68d4e32..9530fb9c 100644 --- a/python/ovs/db/data.py +++ b/python/ovs/db/data.py @@ -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