summaryrefslogtreecommitdiff
path: root/lib/ovsdb-data.c
diff options
context:
space:
mode:
authorBen Pfaff <blp@nicira.com>2010-02-08 16:37:49 -0800
committerBen Pfaff <blp@nicira.com>2010-02-08 16:37:49 -0800
commitae8f13e29057e233712356b3c03f02a7ef4e1e93 (patch)
tree3450bd2f4b286cfc36a20fb201962780728af02a /lib/ovsdb-data.c
parentfbf925e45da7100e5018bd3ad49532e407666f77 (diff)
downloadopenvswitch-ae8f13e29057e233712356b3c03f02a7ef4e1e93.tar.gz
ovsdb: Make scalars and 1-element sets interchangeable.
It is natural to write "abc" in place of ["set",["abc"]] and vice versa. I cannot think of a reason not to support this, and it can make reading and writing OVSDB files and transactions easier, so support it.
Diffstat (limited to 'lib/ovsdb-data.c')
-rw-r--r--lib/ovsdb-data.c32
1 files changed, 17 insertions, 15 deletions
diff --git a/lib/ovsdb-data.c b/lib/ovsdb-data.c
index 47d1ea746..08d623a51 100644
--- a/lib/ovsdb-data.c
+++ b/lib/ovsdb-data.c
@@ -897,26 +897,17 @@ ovsdb_datum_from_json(struct ovsdb_datum *datum,
{
struct ovsdb_error *error;
- if (ovsdb_type_is_scalar(type)) {
- datum->n = 1;
- datum->keys = xmalloc(sizeof *datum->keys);
- datum->values = NULL;
-
- error = ovsdb_atom_from_json(&datum->keys[0], &type->key,
- json, symtab);
- if (error) {
- free(datum->keys);
- }
- return error;
- } else {
+ if (ovsdb_type_is_map(type)
+ || (json->type == JSON_ARRAY
+ && json->u.array.n > 0
+ && json->u.array.elems[0]->type == JSON_STRING
+ && !strcmp(json->u.array.elems[0]->u.string, "set"))) {
bool is_map = ovsdb_type_is_map(type);
const char *class = is_map ? "map" : "set";
const struct json *inner;
unsigned int i;
size_t n;
- assert(is_map || ovsdb_type_is_set(type));
-
error = unwrap_json(json, class, JSON_ARRAY, &inner);
if (error) {
return error;
@@ -974,6 +965,17 @@ ovsdb_datum_from_json(struct ovsdb_datum *datum,
error:
ovsdb_datum_destroy(datum, type);
return error;
+ } else {
+ datum->n = 1;
+ datum->keys = xmalloc(sizeof *datum->keys);
+ datum->values = NULL;
+
+ error = ovsdb_atom_from_json(&datum->keys[0], &type->key,
+ json, symtab);
+ if (error) {
+ free(datum->keys);
+ }
+ return error;
}
}
@@ -983,7 +985,7 @@ ovsdb_datum_to_json(const struct ovsdb_datum *datum,
{
/* These tests somewhat tolerate a 'datum' that does not exactly match
* 'type', in particular a datum with 'n' not in the allowed range. */
- if (datum->n == 1 && ovsdb_type_is_scalar(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) {
struct json **elems;