-class Datum:
- def __init__(self, type, values):
- self.type = type
- self.values = values
-
- @staticmethod
- def fromJson(type_, json):
- if not type_.value:
- if len(json) == 2 and json[0] == "set":
- values = []
- for atomJson in json[1]:
- values += [Atom.fromJson(type_.key, atomJson)]
- else:
- values = [Atom.fromJson(type_.key, json)]
- else:
- if len(json) != 2 or json[0] != "map":
- raise Error("%s is not valid JSON for a map" % json)
- values = []
- for pairJson in json[1]:
- values += [(Atom.fromJson(type_.key, pairJson[0]),
- Atom.fromJson(type_.value, pairJson[1]))]
- return Datum(type_, values)
-
- def cInitDatum(self, var):
- if len(self.values) == 0:
- return ["ovsdb_datum_init_empty(%s);" % var]
-
- s = ["%s->n = %d;" % (var, len(self.values))]
- s += ["%s->keys = xmalloc(%d * sizeof *%s->keys);"
- % (var, len(self.values), var)]
-
- for i in range(len(self.values)):
- key = self.values[i]
- if self.type.value:
- key = key[0]
- s += key.cInitAtom("%s->keys[%d]" % (var, i))
-
- if self.type.value:
- s += ["%s->values = xmalloc(%d * sizeof *%s->values);"
- % (var, len(self.values), var)]
- for i in range(len(self.values)):
- value = self.values[i][1]
- s += key.cInitAtom("%s->values[%d]" % (var, i))
- else:
- s += ["%s->values = NULL;" % var]
-
- if len(self.values) > 1:
- s += ["ovsdb_datum_sort_assert(%s, OVSDB_TYPE_%s);"
- % (var, self.type.key.upper())]
-
- return s
-