-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.lua
90 lines (76 loc) · 2.18 KB
/
test.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
local function print_r(root)
local cache = { [root] = "." }
local function _dump(t, space, name)
local temp = {}
for k, v in pairs(t) do
local key = tostring(k)
if cache[v] then
table.insert(temp, "+" .. key .. " {" .. cache[v] .. "}")
elseif type(v) == "table" then
local new_key = name .. "." .. key
cache[v] = new_key
table.insert(temp,
"+" .. key .. _dump(v, space .. (next(t, k) and "|" or " ") .. string.rep(" ", #key), new_key))
else
table.insert(temp, "+" .. key .. " [" .. tostring(v) .. "]")
end
end
return table.concat(temp, "\n" .. space)
end
print(_dump(root, "", ""))
end
local luaxml = require "luaxml"
local lx1 = luaxml.new()
lx1.xt = {
["@meta"] = {},
["root@1"] = {
["key1@1"] = {
["@val"] = "value1",
["@next"] = "key2@1",
},
["key2@1"] = {
["@val"] = 123,
["@attr"] = { type = "string" },
["@next"] = "key3@1",
},
["key3@1"] = {
["@val"] = 31,
["@next"] = "key3@2",
},
["key3@2"] = {
["@val"] = 32,
["@next"] = "key4@1",
},
["key4@1"] = {
["key41@1"] = {
["@val"] = 123,
["@next"] = "key42@1"
},
["key42@1"] = {
["@val"] = 123,
},
["@head"] = "key41@1",
["@attr"] = { type = "map" },
},
["@head"] = "key1@1",
},
["@head"] = "root@1",
}
print(lx1)
print(assert(lx1:get("/root/key1") == "value1"))
lx1:set("/root/key1", 456)
print(assert(lx1:get("/root/key1") == 456))
print(lx1:get("/root/key3[1]")) -- 31
lx1["/root/key3[1]"] = 789
print(lx1:get("/root/key3[1]")) -- 789
print(lx1:get("/root/key5")) -- nil
-- lx1:set("/root/key3[3]", 1024)
lx1["/root/key3[3]"] = 1024
-- iterate attrs
assert(lx1["/root/key4/@type"] == "map")
local key4attrs = lx1:get_attrs("/root/key4")
for k, v in pairs(key4attrs) do
print(k, v)
end
print(lx1)
print(lx1["/root/key1"])