- return "%(dst)s = %(src)s;" % args
-
- def initCDefault(self, var, isOptional):
- if self.refTable:
- return "%s = NULL;" % var
- elif self.type == 'string' and not isOptional:
- return "%s = \"\";" % var
- else:
- return {'integer': '%s = 0;',
- 'real': '%s = 0.0;',
- 'uuid': 'uuid_zero(&%s);',
- 'boolean': '%s = false;',
- 'string': '%s = NULL;'}[self.type] % var
-
- def cInitBaseType(self, indent, var):
- stmts = []
- stmts.append('ovsdb_base_type_init(&%s, OVSDB_TYPE_%s);' % (
- var, self.type.upper()),)
- if self.type == 'integer':
- if self.minInteger != None:
- stmts.append('%s.u.integer.min = %d;' % (var, self.minInteger))
- if self.maxInteger != None:
- stmts.append('%s.u.integer.max = %d;' % (var, self.maxInteger))
- elif self.type == 'real':
- if self.minReal != None:
- stmts.append('%s.u.real.min = %d;' % (var, self.minReal))
- if self.maxReal != None:
- stmts.append('%s.u.real.max = %d;' % (var, self.maxReal))
- elif self.type == 'string':
- if self.reMatch != None:
- if self.reComment != None:
- reComment = '"%s"' % escapeCString(self.reComment)
- else:
- reComment = NULL
- stmts.append('do_set_regex(&%s, "%s", %s);' % (
- var, escapeCString(self.reMatch), reComment))
- if self.minLength != None:
- stmts.append('%s.u.string.minLen = %d;' % (var, self.minLength))
- if self.maxLength != None:
- stmts.append('%s.u.string.maxLen = %d;' % (var, self.maxLength))
- return '\n'.join([indent + stmt for stmt in stmts])
-
-class Type:
- def __init__(self, key, value=None, min=1, max=1):
- self.key = key
- self.value = value
- self.min = min
- self.max = max
-
- @staticmethod
- def fromJson(json, description):
- if type(json) == unicode:
- return Type(BaseType(json))
- else:
- keyJson = mustGetMember(json, 'key', [dict, unicode], description)
- key = BaseType.fromJson(keyJson, 'key in %s' % description)
- keyRefTable = getMember(json, 'keyRefTable', [unicode], description)
- if keyRefTable:
- key.refTable = keyRefTable
-
- valueJson = getMember(json, 'value', [dict, unicode], description)
- if valueJson:
- value = BaseType.fromJson(valueJson,
- 'value in %s' % description)
- valueRefTable = getMember(json, 'valueRefTable', [unicode], description)
- if valueRefTable:
- value.refTable = valueRefTable
- else:
- value = None
-
- min = getMember(json, 'min', [int], description, 1)
- max = getMember(json, 'max', [int, unicode], description, 1)
- return Type(key, value, min, max)
-
- def isScalar(self):
- return self.min == 1 and self.max == 1 and not self.value
-
- def isOptional(self):
- return self.min == 0 and self.max == 1
-
- def isOptionalPointer(self):
- return (self.min == 0 and self.max == 1 and not self.value
- and (self.key.type == 'string' or self.key.refTable))
-
- def toEnglish(self):
- keyName = self.key.toEnglish()
- if self.value:
- valueName = self.value.toEnglish()
-
- if self.isScalar():
- return keyName
- elif self.isOptional():
- if self.value:
- return "optional %s-%s pair" % (keyName, valueName)
- else:
- return "optional %s" % keyName
+ 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))