319 lines
11 KiB
Lua
319 lines
11 KiB
Lua
--[[--
|
||
JsonPath(Jayway 常用子集):$ .key ['key'] [n] [-n] [*] .* ..key [a,b] [s:e:step] [?(@.k op v)] [?(@.k)]。
|
||
|
||
@module moonsource_legadorule.jsonpath
|
||
--]]
|
||
|
||
local Json = require("moonsource_legadorule.json")
|
||
|
||
local JsonPath = {}
|
||
|
||
local null = Json.null
|
||
|
||
local function isArr(v) return Json.isArray(v) end
|
||
local function isObj(v) return Json.isObject(v) end
|
||
|
||
local function children(v)
|
||
local out = {}
|
||
if isArr(v) then
|
||
for k = 1, #v do out[k] = v[k] end
|
||
elseif isObj(v) then
|
||
local keys = {}
|
||
for k in pairs(v) do keys[#keys + 1] = k end
|
||
table.sort(keys)
|
||
for _, k in ipairs(keys) do out[#out + 1] = v[k] end
|
||
end
|
||
return out
|
||
end
|
||
|
||
local function descend(v, out)
|
||
out[#out + 1] = v
|
||
for _, c in ipairs(children(v)) do descend(c, out) end
|
||
end
|
||
|
||
local function str(s, i)
|
||
local q = s:sub(i, i)
|
||
local j, out = i + 1, {}
|
||
while j <= #s do
|
||
local c = s:sub(j, j)
|
||
if c == "\\" then
|
||
out[#out + 1] = s:sub(j + 1, j + 1)
|
||
j = j + 2
|
||
elseif c == q then
|
||
return table.concat(out), j + 1
|
||
else
|
||
out[#out + 1] = c
|
||
j = j + 1
|
||
end
|
||
end
|
||
error("jsonpath: unterminated string", 0)
|
||
end
|
||
|
||
-- 过滤表达式 @.a.b op literal
|
||
local function filterValue(s)
|
||
s = s:gsub("^%s+", ""):gsub("%s+$", "")
|
||
local q = s:sub(1, 1)
|
||
if q == "'" or q == '"' then return (str(s, 1)) end
|
||
if s == "true" then return true elseif s == "false" then return false elseif s == "null" then return null end
|
||
return tonumber(s) or s
|
||
end
|
||
|
||
local function walkAt(item, path)
|
||
local cur = item
|
||
for key in path:gmatch("[^%.]+") do
|
||
key = key:gsub("^%['(.-)'%]$", "%1")
|
||
if isObj(cur) then cur = cur[key]
|
||
elseif isArr(cur) and tonumber(key) then cur = cur[tonumber(key) + 1]
|
||
else return nil end
|
||
if cur == nil then return nil end
|
||
end
|
||
return cur
|
||
end
|
||
|
||
local function compileFilter(expr)
|
||
expr = expr:gsub("^%s*%(", ""):gsub("%)%s*$", "")
|
||
local function clause(c)
|
||
local path, op, rhs = c:match("^%s*@%.?([^%s=!<>~]*)%s*([=!<>~]+)%s*(.-)%s*$")
|
||
if not path then
|
||
local only = c:match("^%s*@%.?([^%s]*)%s*$")
|
||
if only then return function(item) return walkAt(item, only) ~= nil end end
|
||
local neg = c:match("^%s*!%s*@%.?([^%s]*)%s*$")
|
||
if neg then return function(item) return walkAt(item, neg) == nil end end
|
||
error("jsonpath: unsupported filter " .. c, 0)
|
||
end
|
||
local want = filterValue(rhs)
|
||
return function(item)
|
||
local v = path == "" and item or walkAt(item, path)
|
||
if v == nil then return false end
|
||
if op == "==" or op == "=" then return v == want or tostring(v) == tostring(want)
|
||
elseif op == "!=" then return tostring(v) ~= tostring(want)
|
||
elseif op == "=~" then
|
||
local pat = rhs:match("^/(.*)/[a-z]*$") or rhs
|
||
return require("moonsource_legadorule.regex").test(pat, tostring(v))
|
||
end
|
||
local a, b = tonumber(v), tonumber(want)
|
||
if not a or not b then return false end
|
||
if op == "<" then return a < b elseif op == ">" then return a > b
|
||
elseif op == "<=" then return a <= b else return a >= b end
|
||
end
|
||
end
|
||
local ors = {}
|
||
for part in (expr .. "||"):gmatch("(.-)||") do
|
||
local ands = {}
|
||
for c in (part .. "&&"):gmatch("(.-)&&") do ands[#ands + 1] = clause(c) end
|
||
ors[#ors + 1] = ands
|
||
end
|
||
return function(item)
|
||
for _, ands in ipairs(ors) do
|
||
local ok = true
|
||
for _, f in ipairs(ands) do if not f(item) then ok = false break end end
|
||
if ok then return true end
|
||
end
|
||
return false
|
||
end
|
||
end
|
||
|
||
--- 解析路径为步骤列表。
|
||
local function parse(path)
|
||
local steps, i = {}, 1
|
||
path = path:gsub("^%s+", ""):gsub("%s+$", "")
|
||
if path:sub(1, 1) == "$" then i = 2 end
|
||
local definite = true
|
||
while i <= #path do
|
||
local c = path:sub(i, i)
|
||
if c == "." then
|
||
if path:sub(i + 1, i + 1) == "." then
|
||
steps[#steps + 1] = { t = "deep" }
|
||
definite = false
|
||
i = i + 2
|
||
if path:sub(i, i) == "[" or path:sub(i, i) == "*" then
|
||
-- ..[ ] / ..* 交给后续步骤
|
||
else
|
||
local key = path:match("^[^%.%[]+", i)
|
||
steps[#steps + 1] = { t = "key", key = key }
|
||
i = i + #key
|
||
end
|
||
else
|
||
i = i + 1
|
||
if path:sub(i, i) == "*" then
|
||
steps[#steps + 1] = { t = "all" }
|
||
definite = false
|
||
i = i + 1
|
||
else
|
||
local key = path:match("^[^%.%[]+", i)
|
||
if not key then error("jsonpath: bad path", 0) end
|
||
steps[#steps + 1] = { t = "key", key = key }
|
||
i = i + #key
|
||
end
|
||
end
|
||
elseif c == "[" then
|
||
local j = i + 1
|
||
local body
|
||
local q = path:sub(j, j)
|
||
if q == "?" then
|
||
local depth, k = 0, j + 1
|
||
while k <= #path do
|
||
local ch = path:sub(k, k)
|
||
if ch == "(" then depth = depth + 1
|
||
elseif ch == ")" then
|
||
depth = depth - 1
|
||
if depth == 0 then break end
|
||
elseif ch == "'" or ch == '"' then
|
||
local _, nk = str(path, k)
|
||
k = nk - 1
|
||
end
|
||
k = k + 1
|
||
end
|
||
steps[#steps + 1] = { t = "filter", f = compileFilter(path:sub(j + 1, k)) }
|
||
definite = false
|
||
i = path:find("]", k, true) + 1
|
||
else
|
||
local depth, k = 0, j
|
||
while k <= #path do
|
||
local ch = path:sub(k, k)
|
||
if ch == "'" or ch == '"' then
|
||
local _, nk = str(path, k)
|
||
k = nk - 1
|
||
elseif ch == "]" then
|
||
break
|
||
end
|
||
k = k + 1
|
||
end
|
||
body = path:sub(j, k - 1)
|
||
i = k + 1
|
||
if body == "*" then
|
||
steps[#steps + 1] = { t = "all" }
|
||
definite = false
|
||
elseif body:find("^%s*['\"]") then
|
||
local keys, p = {}, 1
|
||
while p <= #body do
|
||
local ch = body:sub(p, p)
|
||
if ch == "'" or ch == '"' then
|
||
local s2
|
||
s2, p = str(body, p)
|
||
keys[#keys + 1] = s2
|
||
else
|
||
p = p + 1
|
||
end
|
||
end
|
||
if #keys == 1 then
|
||
steps[#steps + 1] = { t = "key", key = keys[1] }
|
||
else
|
||
steps[#steps + 1] = { t = "keys", keys = keys }
|
||
definite = false
|
||
end
|
||
elseif body:find(":") then
|
||
local a, b, s = body:match("^%s*(-?%d*)%s*:%s*(-?%d*)%s*:?%s*(-?%d*)%s*$")
|
||
steps[#steps + 1] = { t = "slice", a = tonumber(a), b = tonumber(b), step = tonumber(s) or 1 }
|
||
definite = false
|
||
elseif body:find(",") then
|
||
local idxs = {}
|
||
for n in body:gmatch("-?%d+") do idxs[#idxs + 1] = tonumber(n) end
|
||
steps[#steps + 1] = { t = "idxs", idxs = idxs }
|
||
definite = false
|
||
else
|
||
local n = tonumber(body)
|
||
if not n then error("jsonpath: bad index", 0) end
|
||
steps[#steps + 1] = { t = "idx", n = n }
|
||
end
|
||
end
|
||
else
|
||
error("jsonpath: unexpected '" .. c .. "'", 0)
|
||
end
|
||
end
|
||
return steps, definite
|
||
end
|
||
|
||
local function byIndex(v, n)
|
||
if not isArr(v) then return nil end
|
||
if n < 0 then n = #v + n end
|
||
return v[n + 1]
|
||
end
|
||
|
||
local function apply(step, v, out)
|
||
local t = step.t
|
||
if t == "key" then
|
||
if isObj(v) then
|
||
if v[step.key] ~= nil then out[#out + 1] = v[step.key] end
|
||
elseif isArr(v) and step.key == "length()" then
|
||
out[#out + 1] = #v
|
||
end
|
||
elseif t == "keys" then
|
||
if isObj(v) then
|
||
for _, k in ipairs(step.keys) do if v[k] ~= nil then out[#out + 1] = v[k] end end
|
||
end
|
||
elseif t == "idx" then
|
||
local x = byIndex(v, step.n)
|
||
if x ~= nil then out[#out + 1] = x end
|
||
elseif t == "idxs" then
|
||
for _, n in ipairs(step.idxs) do
|
||
local x = byIndex(v, n)
|
||
if x ~= nil then out[#out + 1] = x end
|
||
end
|
||
elseif t == "all" then
|
||
for _, c in ipairs(children(v)) do out[#out + 1] = c end
|
||
elseif t == "slice" and isArr(v) then
|
||
local n = #v
|
||
local a, b = step.a or 0, step.b or n
|
||
if a < 0 then a = n + a end
|
||
if b < 0 then b = n + b end
|
||
a, b = math.max(a, 0), math.min(b, n)
|
||
for k = a, b - 1, step.step > 0 and step.step or 1 do out[#out + 1] = v[k + 1] end
|
||
elseif t == "filter" then
|
||
for _, c in ipairs(children(v)) do
|
||
if step.f(c) then out[#out + 1] = c end
|
||
end
|
||
end
|
||
end
|
||
|
||
local cache = {}
|
||
|
||
--- 读取。返回 values(命中数组), definite(路径是否确定到单值)。路径错误抛字符串。
|
||
function JsonPath.read(root, path)
|
||
local c = cache[path]
|
||
if not c then
|
||
local steps, definite = parse(path)
|
||
c = { steps = steps, definite = definite }
|
||
cache[path] = c
|
||
end
|
||
local current = { root }
|
||
local i = 1
|
||
while i <= #c.steps do
|
||
local step = c.steps[i]
|
||
local nextList = {}
|
||
if step.t == "deep" then
|
||
local all = {}
|
||
for _, v in ipairs(current) do descend(v, all) end
|
||
local nxt = c.steps[i + 1]
|
||
i = i + 1
|
||
if nxt then
|
||
for _, v in ipairs(all) do apply(nxt, v, nextList) end
|
||
else
|
||
nextList = all
|
||
end
|
||
else
|
||
for _, v in ipairs(current) do apply(step, v, nextList) end
|
||
end
|
||
current = nextList
|
||
i = i + 1
|
||
end
|
||
return current, c.definite
|
||
end
|
||
|
||
--- 把 JSON 值转成 Legado 风格字符串:字符串原样,null 为空,对象 / 数组序列化。
|
||
function JsonPath.stringify(v)
|
||
if v == nil or v == null then return "" end
|
||
local t = type(v)
|
||
if t == "string" then return v end
|
||
if t == "boolean" then return tostring(v) end
|
||
if t == "number" then return Json.encode(v) end
|
||
if Json.isArray(v) then
|
||
local out = {}
|
||
for i, item in ipairs(v) do out[i] = JsonPath.stringify(item) end
|
||
return table.concat(out, "\n")
|
||
end
|
||
return Json.encode(v)
|
||
end
|
||
|
||
return JsonPath
|