Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List action #121

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 49 additions & 8 deletions models/rpc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 },
}
8 changes: 7 additions & 1 deletion models/test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,16 @@
<VALUE name="false" value="false"/>
</NODE>
<NODE name="groups" help="This is a leaf list of integers">
<NODE name="*" mode="rw" help="Member of group" range="1..9"/>
<NODE name="*" mode="rw" help="Member of group" range="1..999"/>
</NODE>
</NODE>
</NODE>
<NODE name="rules" help="This is a list of rules with numerical indexes">
<NODE name="*" help="The rule entry with key index">
<NODE name="index" mode="rw" help="integer" range="1..999"/>
<NODE name="name" mode="rw" help="This is the name of the rule"/>
</NODE>
</NODE>
</NODE>
<NODE name="state" help="State">
<NODE name="counter" mode="r" default="0" help="uint32" range="0..4294967295"/>
Expand Down
15 changes: 14 additions & 1 deletion models/test4:rpc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,20 @@
<NODE name="*" help="The user entry with key name">
<NODE name="name" mode="rw" help="This is the name of the user"/>
<NODE name="age" mode="rw" help="Age of the user" range="0..4294967295"/>
<NODE name="_" mode="rwx" help="Delete a user"/>
<NODE name="_" mode="rwx" help="get/add/delete users">
<NODE name="input" help="Input parameters">
<NODE name="name" mode="w" help="User name"/>
<NODE name="age" mode="w" help="User age" range="0..4294967295"/>
</NODE>
<NODE name="output" help="Input parameters">
<NODE name="users" help="This is a list of users">
<NODE name="*" help="The user entry with key name">
<NODE name="name" mode="r" help="User name"/>
<NODE name="age" mode="r" help="User age" range="0..4294967295"/>
</NODE>
</NODE>
</NODE>
</NODE>
<NODE name="set-age" mode="rwx" help="Set the users age">
<NODE name="input" help="Input parameters">
<NODE name="age" mode="w" help="New age" range="0..4294967295"/>
Expand Down
19 changes: 17 additions & 2 deletions schema.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand All @@ -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),
Expand Down
Loading