"length %u", s, n_chars, c->maxLen);
}
-#if HAVE_PCRE
- if (c->re) {
- int retval;
-
- retval = pcre_exec(c->re, NULL, s, strlen(s), 0,
- PCRE_ANCHORED | PCRE_NO_UTF8_CHECK, NULL, 0);
- if (retval == PCRE_ERROR_NOMATCH) {
- if (c->reComment) {
- return ovsdb_error("constraint violation",
- "\"%s\" is not a %s", s, c->reComment);
- } else {
- return ovsdb_error("constraint violation",
- "\"%s\" does not match regular expression "
- "/%s/", s, c->reMatch);
- }
- } else if (retval < 0) {
- /* PCRE doesn't have a function to translate an error code to a
- * description. Bizarre. See pcreapi(3) for error details. */
- return ovsdb_error("internal error", "PCRE returned error %d",
- retval);
- }
- }
-#endif /* HAVE_PCRE */
-
return NULL;
}
ovsdb_atom_check_constraints(const union ovsdb_atom *atom,
const struct ovsdb_base_type *base)
{
+ if (base->enum_
+ && ovsdb_datum_find_key(base->enum_, atom, base->type) == UINT_MAX) {
+ struct ovsdb_error *error;
+ struct ds actual = DS_EMPTY_INITIALIZER;
+ struct ds valid = DS_EMPTY_INITIALIZER;
+
+ ovsdb_atom_to_string(atom, base->type, &actual);
+ ovsdb_datum_to_string(base->enum_,
+ ovsdb_base_type_get_enum_type(base->type),
+ &valid);
+ error = ovsdb_error("constraint violation",
+ "%s is not one of the allowed values (%s)",
+ ds_cstr(&actual), ds_cstr(&valid));
+ ds_destroy(&actual);
+ ds_destroy(&valid);
+
+ return error;
+ }
+
switch (base->type) {
case OVSDB_TYPE_VOID:
NOT_REACHED();
}
struct ovsdb_datum_sort_cbdata {
- const struct ovsdb_type *type;
+ enum ovsdb_atomic_type key_type;
struct ovsdb_datum *datum;
};
return ovsdb_atom_compare_3way(&cbdata->datum->keys[a],
&cbdata->datum->keys[b],
- cbdata->type->key.type);
+ cbdata->key_type);
}
static void
struct ovsdb_datum_sort_cbdata *cbdata = cbdata_;
ovsdb_atom_swap(&cbdata->datum->keys[a], &cbdata->datum->keys[b]);
- if (cbdata->type->value.type != OVSDB_TYPE_VOID) {
+ if (cbdata->datum->values) {
ovsdb_atom_swap(&cbdata->datum->values[a], &cbdata->datum->values[b]);
}
}
struct ovsdb_error *
-ovsdb_datum_sort(struct ovsdb_datum *datum, const struct ovsdb_type *type)
+ovsdb_datum_sort(struct ovsdb_datum *datum, enum ovsdb_atomic_type key_type)
{
if (datum->n < 2) {
return NULL;
struct ovsdb_datum_sort_cbdata cbdata;
size_t i;
- cbdata.type = type;
+ cbdata.key_type = key_type;
cbdata.datum = datum;
sort(datum->n, ovsdb_datum_sort_compare_cb, ovsdb_datum_sort_swap_cb,
&cbdata);
for (i = 0; i < datum->n - 1; i++) {
if (ovsdb_atom_equals(&datum->keys[i], &datum->keys[i + 1],
- type->key.type)) {
- if (ovsdb_type_is_map(type)) {
+ key_type)) {
+ if (datum->values) {
return ovsdb_error(NULL, "map contains duplicate key");
} else {
return ovsdb_error(NULL, "set contains duplicate");
}
}
+void
+ovsdb_datum_sort_assert(struct ovsdb_datum *datum,
+ enum ovsdb_atomic_type key_type)
+{
+ struct ovsdb_error *error = ovsdb_datum_sort(datum, key_type);
+ if (error) {
+ NOT_REACHED();
+ }
+}
+
/* Checks that each of the atoms in 'datum' conforms to the constraints
* specified by its 'type'. Returns an error if a constraint is violated,
* otherwise a null pointer.
{
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;
datum->n++;
}
- error = ovsdb_datum_sort(datum, type);
+ error = ovsdb_datum_sort(datum, type->key.type);
if (error) {
goto error;
}
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;
}
}
{
/* 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;
goto error;
}
- dberror = ovsdb_datum_sort(datum, type);
+ dberror = ovsdb_datum_sort(datum, type->key.type);
if (dberror) {
ovsdb_error_destroy(dberror);
if (ovsdb_type_is_map(type)) {
if (n != a->n) {
struct ovsdb_error *error;
a->n = n;
- error = ovsdb_datum_sort(a, type);
+ error = ovsdb_datum_sort(a, type->key.type);
assert(!error);
}
}
}
}
if (changed) {
- struct ovsdb_error *error = ovsdb_datum_sort(a, a_type);
- assert(!error);
+ ovsdb_datum_sort_assert(a, a_type->key.type);
}
}
\f