Software /
code /
prosody
Annotate
util/jsonpointer.lua @ 13085:0e17cb78264f
util.jsonschema: Implement 'luaPattern' - Lua variant of 'pattern'
Like 'pattern' but uses Lua patterns instead of Regular Expressions,
since only a subset of regex are also valid Lua patterns.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 22 Apr 2023 12:48:51 +0200 |
parent | 12781:22066b02887f |
rev | line source |
---|---|
12781
22066b02887f
util.startup: Provide a common Lua 5.3+ math.type() for Lua 5.2
Kim Alvefur <zash@zash.se>
parents:
12499
diff
changeset
|
1 local m_type = math.type; |
12499
03e307952816
util.jsonpointer: Fix Lua <5.3 compat
Kim Alvefur <zash@zash.se>
parents:
12496
diff
changeset
|
2 |
12131
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 local function unescape_token(escaped_token) |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 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
|
5 return unescaped |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 end |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 local function resolve_json_pointer(ref, path) |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 local ptr_len = #path + 1 |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 for part, pos in path:gmatch("/([^/]*)()") do |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 local token = unescape_token(part) |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 if not (type(ref) == "table") then |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 return nil |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 end |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 local idx = next(ref) |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 local new_ref |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 if type(idx) == "string" then |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 new_ref = ref[token] |
12499
03e307952816
util.jsonpointer: Fix Lua <5.3 compat
Kim Alvefur <zash@zash.se>
parents:
12496
diff
changeset
|
20 elseif m_type(idx) == "integer" then |
12131
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 local i = tonumber(token) |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 if token == "-" then |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 i = #ref + 1 |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 end |
12496
87c3d45208ef
util.jsonpointer: Fix off-by-one in array resolution
Kim Alvefur <zash@zash.se>
parents:
12131
diff
changeset
|
25 new_ref = ref[i + 1] |
12131
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 else |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 return nil, "invalid-table" |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 end |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 if pos == ptr_len then |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 return new_ref |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 elseif type(new_ref) == "table" then |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 ref = new_ref |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 elseif not (type(ref) == "table") then |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 return nil, "invalid-path" |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 end |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 |
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 return ref |
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 |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 return { resolve = resolve_json_pointer } |