diff --git a/models/rpc.lua b/models/rpc.lua index d3158e6..a7af48b 100644 --- a/models/rpc.lua +++ b/models/rpc.lua @@ -59,16 +59,56 @@ local function get_reset_history() return { ["last-reset"] = lasttime, ["history"] = history } end -local function set_age(input, path) - local user = path:match('/t4:test/state/users/([^/]+)') - local age = input["age"] - apteryx.set("/t4:test/state/users/" .. user .. "/age", age) +local function createget_users(input, _, method) + if method == "GET" then + local users = {} + for _, info in pairs(apteryx.get_tree('/t4:test/state/users')) do + users[#users + 1] = { + ["name"] = info["name"], + ["age"] = info["age"], + } + end + return { + ["users"] = users, + } + elseif method == "POST" then + if input["name"] == nil or input["age"] == nil then + return false + end + apteryx.set_tree ("/t4:test/state/users/" .. input["name"], { + ["name"] = input["name"], + ["age"] = input["age"], + }) + end return true end -local function delete_user(input, path) +local function modifydelete_user(input, path, method) local user = path:match('/t4:test/state/users/([^/]+)') - apteryx.prune("/t4:test/state/users/" .. user) + if user == nil then + return false + end + if method == "PUT" then + if input["age"] == nil or apteryx.get("/t4:test/state/users/" .. user .. '/name') ~= user then + return false + end + apteryx.set_tree("/t4:test/state/users/" .. user, { + ["age"] = input["age"], + }) + return true + elseif method == "DELETE" then + apteryx.prune("/t4:test/state/users/" .. user) + return true + end + return false +end + +local function set_user_age(input, path) + local user = path:match('/t4:test/state/users/([^/]+)') + if user == nil then + return false + end + apteryx.set("/t4:test/state/users/" .. user .. "/age", input["age"]) return true end @@ -79,6 +119,7 @@ return { { path="/t4:test/state/reset", methods={"GET", "POST"}, handler=reset_state }, { path="/t4:test/state/get-last-reset-time", methods={"GET", "POST"}, handler=get_reset_time }, { path="/t4:test/state/get-reset-history", methods={"GET", "POST"}, handler=get_reset_history }, - { path="/t4:test/state/users/*/set-age", methods={"POST"}, handler=set_age }, - { path="/t4:test/state/users/*", methods={"DELETE"}, handler=delete_user }, + { path="/t4:test/state/users", methods={"GET", "POST"}, handler=createget_users }, + { path="/t4:test/state/users/*", methods={"PUT", "DELETE"}, handler=modifydelete_user }, + { path="/t4:test/state/users/*/set-age", methods={"POST"}, handler=set_user_age }, } diff --git a/models/test.xml b/models/test.xml index 0b532e7..62b9cd6 100644 --- a/models/test.xml +++ b/models/test.xml @@ -43,10 +43,16 @@ - + + + + + + + diff --git a/models/test4:rpc.xml b/models/test4:rpc.xml index 401bb20..63c5502 100644 --- a/models/test4:rpc.xml +++ b/models/test4:rpc.xml @@ -39,7 +39,20 @@ - + + + + + + + + + + + + + + diff --git a/schema.c b/schema.c index 65035b2..2a19f1a 100644 --- a/schema.c +++ b/schema.c @@ -3392,6 +3392,13 @@ sch_traverse_tree (sch_instance * instance, sch_node * schema, GNode * node, int return rc; } +static int _sch_strcmp_ll (const char *stra, const char *strb) +{ + int a = g_ascii_strtoll (stra, NULL, 10); + int b = g_ascii_strtoll (strb, NULL, 10); + return a - b; +} + static json_t * _sch_gnode_to_json (sch_instance * instance, sch_node * schema, xmlNs *ns, GNode * node, int flags, int depth) { @@ -3488,8 +3495,12 @@ _sch_gnode_to_json (sch_instance * instance, sch_node * schema, xmlNs *ns, GNode if (sch_is_leaf_list (schema) && (flags & SCH_F_JSON_ARRAYS)) { + sch_node *kschema = sch_node_child_first (schema); + if (kschema && xmlHasProp ((xmlNode *)kschema, (const xmlChar *)"range")) + apteryx_sort_children (node, _sch_strcmp_ll); + else + apteryx_sort_children (node, g_strcmp0); data = json_array (); - apteryx_sort_children (node, g_strcmp0); DEBUG (flags, "%*s%s[", depth * 2, " ", APTERYX_NAME (node)); for (GNode * child = node->children; child; child = child->next) @@ -3515,8 +3526,12 @@ _sch_gnode_to_json (sch_instance * instance, sch_node * schema, xmlNs *ns, GNode } else if (sch_is_list (schema) && (flags & SCH_F_JSON_ARRAYS)) { + sch_node *kschema = sch_node_child_first (sch_node_child_first(schema)); + if (kschema && xmlHasProp ((xmlNode *)kschema, (const xmlChar *)"range")) + apteryx_sort_children (node, _sch_strcmp_ll); + else + apteryx_sort_children (node, g_strcmp0); data = json_array (); - apteryx_sort_children (node, g_strcmp0); for (GNode * child = node->children; child; child = child->next) { DEBUG (flags, "%*s%s[%s]\n", depth * 2, " ", APTERYX_NAME (node),