Skip to content
Snippets Groups Projects
Commit 015d998d authored by Jakob Olsson's avatar Jakob Olsson
Browse files

parse definitions as blob

parent b3eae44a
Branches
No related tags found
No related merge requests found
...@@ -84,8 +84,7 @@ bool schema_validator_validate_blob(struct blob_attr *msg, const char *object, c ...@@ -84,8 +84,7 @@ bool schema_validator_validate_blob(struct blob_attr *msg, const char *object, c
struct schema_method *s_method; struct schema_method *s_method;
json schema, obj; json schema, obj;
int rv = 1; int rv = 1;
char *str, *s; char *arg_str, *schema_str;
if (!initialized) if (!initialized)
return true; return true;
...@@ -101,28 +100,37 @@ bool schema_validator_validate_blob(struct blob_attr *msg, const char *object, c ...@@ -101,28 +100,37 @@ bool schema_validator_validate_blob(struct blob_attr *msg, const char *object, c
goto out; goto out;
if (type == SCHEMA_INPUT_CALL) if (type == SCHEMA_INPUT_CALL)
s = blobmsg_format_json(s_method->b_input, true); schema_str = blobmsg_format_json(s_method->b_input, true);
else if (type == SCHEMA_OUTPUT_CALL) else if (type == SCHEMA_OUTPUT_CALL)
s = blobmsg_format_json(s_method->b_output, true); schema_str = blobmsg_format_json(s_method->b_output, true);
else else
goto out; goto out;
if (!s) if (!schema_str)
goto out; goto out;
schema = json::parse(s); schema = json::parse(schema_str);
str = blobmsg_format_json(msg, true); arg_str = blobmsg_format_json(msg, true);
obj = json::parse(str); obj = json::parse(arg_str);
if (s_object->definitions) { if (s_object->definitions) {
json definitions = json::parse(s_object->definitions); char *def_str;
def_str = blobmsg_format_json(s_object->definitions, true);
if (!def_str)
goto out_definitions;
json definitions = json::parse(def_str);
schema += json::object_t::value_type("definitions", definitions); schema += json::object_t::value_type("definitions", definitions);
free(def_str);
} }
rv = json_object_validate_schema(obj, schema); rv = json_object_validate_schema(obj, schema);
free(str); out_definitions:
free(s); free(arg_str);
free(schema_str);
out: out:
return rv; return rv;
} }
...@@ -158,14 +166,24 @@ bool schema_validator_validate_jobj(struct json_object *j_object, const char *ob ...@@ -158,14 +166,24 @@ bool schema_validator_validate_jobj(struct json_object *j_object, const char *ob
sch = json::parse(s); sch = json::parse(s);
if (s_object->definitions) { if (s_object->definitions) {
json definitions = json::parse(s_object->definitions); char *def_str;
def_str = blobmsg_format_json(s_object->definitions, true);
if (!def_str)
goto out_definitions;
json definitions = json::parse(def_str);
sch += json::object_t::value_type("definitions", definitions); sch += json::object_t::value_type("definitions", definitions);
free(def_str);
} }
obj = json::parse(json_object_get_string(j_object)); obj = json::parse(json_object_get_string(j_object));
rv = json_object_validate_schema(obj, sch); rv = json_object_validate_schema(obj, sch);
out_definitions:
free(s);
out: out:
return rv; return rv;
} }
......
...@@ -124,7 +124,6 @@ schema_setup_file(const char *path) ...@@ -124,7 +124,6 @@ schema_setup_file(const char *path)
struct blob_attr *root, *object; struct blob_attr *root, *object;
int rem, rem2; int rem, rem2;
struct schema_object *schema_object; struct schema_object *schema_object;
char *definitions = NULL;
memset(&acl, 0, sizeof(acl)); memset(&acl, 0, sizeof(acl));
blob_buf_init(&acl, 0); blob_buf_init(&acl, 0);
...@@ -154,8 +153,12 @@ schema_setup_file(const char *path) ...@@ -154,8 +153,12 @@ schema_setup_file(const char *path)
goto out_flush; goto out_flush;
} }
if (!strncmp(blobmsg_name(root), "definitions", 12)) if (!strncmp(blobmsg_name(root), "definitions", 12)) {
definitions = blobmsg_format_json(root, true); int len = blob_raw_len(root);
schema_object->definitions = (struct blob_attr *) calloc(1, sizeof(struct blob_attr) + len);
memcpy(schema_object->definitions, root, len);
}
if (strncmp(blobmsg_name(root), "properties", 11)) if (strncmp(blobmsg_name(root), "properties", 11))
continue; continue;
...@@ -183,7 +186,6 @@ schema_setup_file(const char *path) ...@@ -183,7 +186,6 @@ schema_setup_file(const char *path)
if (!schema_object->avl.key) if (!schema_object->avl.key)
goto out_flush; goto out_flush;
schema_object->definitions = definitions;
avl_insert(&s_ctx.schema_objects, &schema_object->avl); avl_insert(&s_ctx.schema_objects, &schema_object->avl);
out: out:
...@@ -191,6 +193,7 @@ out: ...@@ -191,6 +193,7 @@ out:
return; return;
out_flush: out_flush:
schema_flush_object(schema_object); schema_flush_object(schema_object);
blob_buf_free(&acl);
} }
struct schema_method *schema_get_method_schema(const char *object, const char *method) struct schema_method *schema_get_method_schema(const char *object, const char *method)
...@@ -212,7 +215,7 @@ struct schema_method *schema_get_method_schema(const char *object, const char *m ...@@ -212,7 +215,7 @@ struct schema_method *schema_get_method_schema(const char *object, const char *m
return NULL; return NULL;
} }
const char *schema_get_definitions_schema(const char *object) struct blob_attr *schema_get_definitions_schema(const char *object)
{ {
struct schema_object *s_object; struct schema_object *s_object;
......
...@@ -25,7 +25,7 @@ struct schema_method { ...@@ -25,7 +25,7 @@ struct schema_method {
struct schema_object { struct schema_object {
char object_name[OBJECT_NAME_MAX_LEN]; char object_name[OBJECT_NAME_MAX_LEN];
char *definitions; struct blob_attr *definitions;
bool regex; bool regex;
regex_t regex_exp; regex_t regex_exp;
...@@ -35,7 +35,7 @@ struct schema_object { ...@@ -35,7 +35,7 @@ struct schema_object {
}; };
struct schema_method *schema_get_method_schema(const char *object, const char *method); struct schema_method *schema_get_method_schema(const char *object, const char *method);
const char *schema_get_definitions_schema(const char *object); struct blob_attr *schema_get_definitions_schema(const char *object);
struct schema_object *schema_get_object_schema(const char *object); struct schema_object *schema_get_object_schema(const char *object);
void schema_setup_json(); void schema_setup_json();
void schema_flush_objects(); void schema_flush_objects();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment