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

refactor to store blobs in memory

parent 759510c2
Branches
No related tags found
No related merge requests found
...@@ -158,12 +158,13 @@ out: ...@@ -158,12 +158,13 @@ out:
return rv; return rv;
} }
/*
bool schema_validator_validate(struct json_object *j_object, char *object, char *method, enum schema_call_t type) bool schema_validator_validate_jobj(struct json_object *j_object, char *object, char *method, enum schema_call_t type)
{ {
struct json_object *definitions, *schema;
struct schema_object *s_object; struct schema_object *s_object;
struct schema_method *s_method; struct schema_method *s_method;
json obj, sch;
char *s;
int rv = 1; int rv = 1;
s_object = schema_get_object_schema(object); s_object = schema_get_object_schema(object);
...@@ -177,76 +178,29 @@ bool schema_validator_validate(struct json_object *j_object, char *object, char ...@@ -177,76 +178,29 @@ bool schema_validator_validate(struct json_object *j_object, char *object, char
goto out; goto out;
if (type == SCHEMA_INPUT_CALL) if (type == SCHEMA_INPUT_CALL)
schema = json_tokener_parse(s_method->input); s = blobmsg_format_json(s_method->b_input, true);
else if (type == SCHEMA_OUTPUT_CALL) else if (type == SCHEMA_OUTPUT_CALL)
schema = json_tokener_parse(s_method->output); s = blobmsg_format_json(s_method->b_output, true);
else else
goto out; goto out;
if (!schema) if (!s)
goto out;
sch = json::parse(s);
if (!sch)
goto out; goto out;
if (s_object->definitions) { if (s_object->definitions) {
definitions = json_tokener_parse(s_object->definitions); json definitions = json::parse(s_object->definitions);
if (definitions) sch += json::object_t::value_type("definitions", definitions);
json_object_object_add(schema, "definitions", definitions);
} }
std::cout << "jobject" << json_object_get_string(j_object) << std::endl; obj = json::parse(json_object_get_string(j_object));
std::cout << "schema" << json_object_get_string(schema) << std::endl;
rv = json_object_validate_schema(j_object, schema); rv = json_object_validate_schema(obj, sch);
json_object_put(schema);
out: out:
return rv; return rv;
} }
bool json_object_validate_schema_inject_definitions(struct json_object *j_object, struct json_object *definitions, struct json_object *j_schema)
{
json_object_object_add(j_object, "definitions", definitions);
return json_object_validate_schema(j_object, j_schema);
}*/
bool json_object_validate_schema(json obj, json sch)
{
std::cout << "hello" << std::endl;
/*const char *sch_str, *obj_str;
obj_str = json_object_get_string(j_object);
sch_str = json_object_get_string(j_schema);
std::cout << "obj_str " << obj_str << std::endl;
std::cout << "sch_str " << sch_str << std::endl;
json sch = json::parse(sch_str);
json obj = json::parse(obj_str);*/
/* json-parse the schema */
json_validator validator; // create validator
try {
validator.set_root_schema(sch); // insert root-schema
} catch (const std::exception &e) {
std::cerr << "Validation of schema failed, here is why: " << e.what() << "\n";
return 0;
}
/* json-parse the people - with custom error handler */
class custom_error_handler : public nlohmann::json_schema::basic_error_handler
{
void error(const nlohmann::json::json_pointer &ptr, const json &instance, const std::string &message) override
{
nlohmann::json_schema::basic_error_handler::error(ptr, instance, message);
std::cerr << "ERROR: '" << ptr << "' - '" << instance << "': " << message << "\n";
}
};
custom_error_handler err;
validator.validate(obj, err); // validate the document
if (err)
return 0;
return 1;
}
...@@ -21,10 +21,11 @@ static void schema_flush_method(struct schema_method *s_method) ...@@ -21,10 +21,11 @@ static void schema_flush_method(struct schema_method *s_method)
{ {
fprintf(stderr, "cleaning method %s\n", (char *)s_method->avl.key); fprintf(stderr, "cleaning method %s\n", (char *)s_method->avl.key);
if (s_method->input) if (s_method->b_input)
free(s_method->input); free(s_method->b_input);
if (s_method->output) if (s_method->b_output)
free(s_method->output); free(s_method->b_output);
if (s_method->avl.key) if (s_method->avl.key)
free((void *) s_method->avl.key); free((void *) s_method->avl.key);
...@@ -97,12 +98,20 @@ static void schema_parse_object(struct schema_object *schema_object, struct blob ...@@ -97,12 +98,20 @@ static void schema_parse_object(struct schema_object *schema_object, struct blob
blobmsg_for_each_attr(method, properties, rem2) { blobmsg_for_each_attr(method, properties, rem2) {
//fprintf(stderr, "%s %d name = %s\n", __func__, __LINE__, blobmsg_name(method)); //fprintf(stderr, "%s %d name = %s\n", __func__, __LINE__, blobmsg_name(method));
if (!strncmp(blobmsg_name(method), "input", 6)) { if (!strncmp(blobmsg_name(method), "input", 6)) {
s_method->input = blobmsg_format_json(method, true); int len = blob_raw_len(method);
//fprintf(stderr, "input = %s\n", s_method->input);
s_method->b_input = (struct blob_attr *) calloc(1, sizeof(struct blob_attr) + len);
memcpy(s_method->b_input, method, len);
fprintf(stderr, "b_input = %s\n", blobmsg_format_json(s_method->b_input, true));
} }
else if (!strncmp(blobmsg_name(method), "output", 7)) { else if (!strncmp(blobmsg_name(method), "output", 7)) {
s_method->output = blobmsg_format_json(method, true); int len = blob_raw_len(method);
//fprintf(stderr, "output = %s\n", s_method->output);
s_method->b_output = (struct blob_attr *) calloc(1, sizeof(struct blob_attr) + len);
memcpy(s_method->b_output, method, len);
fprintf(stderr, "b_output = %s\n", blobmsg_format_json(s_method->b_output, true));
} }
} }
} }
......
...@@ -16,11 +16,8 @@ struct schema_context { ...@@ -16,11 +16,8 @@ struct schema_context {
struct schema_method { struct schema_method {
char method_name[METHOD_NAME_MAX_LEN]; char method_name[METHOD_NAME_MAX_LEN];
//struct blob_attr input; struct blob_attr *b_input;
//struct blob_attr output; struct blob_attr *b_output;
char *input;
char *output;
struct avl_node avl; struct avl_node avl;
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment