Annotate

util/jsonpointer.lua @ 12757:edbc888b1e05 0.12

util.jsonschema: Ignore some further test cases for URI references Full-URI references are not implemented
author Kim Alvefur <zash@zash.se>
date Sun, 09 Oct 2022 15:24:26 +0200
parent 12499:03e307952816
child 12781:22066b02887f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12499
03e307952816 util.jsonpointer: Fix Lua <5.3 compat
Kim Alvefur <zash@zash.se>
parents: 12496
diff changeset
1 local m_type = math.type or function (n)
03e307952816 util.jsonpointer: Fix Lua <5.3 compat
Kim Alvefur <zash@zash.se>
parents: 12496
diff changeset
2 return n % 1 == 0 and n <= 9007199254740992 and n >= -9007199254740992 and "integer" or "float";
03e307952816 util.jsonpointer: Fix Lua <5.3 compat
Kim Alvefur <zash@zash.se>
parents: 12496
diff changeset
3 end;
03e307952816 util.jsonpointer: Fix Lua <5.3 compat
Kim Alvefur <zash@zash.se>
parents: 12496
diff changeset
4
12131
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 local function unescape_token(escaped_token)
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local unescaped = escaped_token:gsub("~1", "/"):gsub("~0", "~")
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 return unescaped
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 end
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 local function resolve_json_pointer(ref, path)
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local ptr_len = #path + 1
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 for part, pos in path:gmatch("/([^/]*)()") do
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local token = unescape_token(part)
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 if not (type(ref) == "table") then
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 return nil
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 end
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 local idx = next(ref)
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local new_ref
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 if type(idx) == "string" then
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 new_ref = ref[token]
12499
03e307952816 util.jsonpointer: Fix Lua <5.3 compat
Kim Alvefur <zash@zash.se>
parents: 12496
diff changeset
22 elseif m_type(idx) == "integer" then
12131
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 local i = tonumber(token)
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 if token == "-" then
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 i = #ref + 1
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 end
12496
87c3d45208ef util.jsonpointer: Fix off-by-one in array resolution
Kim Alvefur <zash@zash.se>
parents: 12131
diff changeset
27 new_ref = ref[i + 1]
12131
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 else
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 return nil, "invalid-table"
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 end
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 if pos == ptr_len then
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 return new_ref
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 elseif type(new_ref) == "table" then
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 ref = new_ref
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 elseif not (type(ref) == "table") then
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 return nil, "invalid-path"
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 end
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 end
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 return ref
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 end
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43
b4c0efff8dd3 util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 return { resolve = resolve_json_pointer }