Software /
code /
prosody
Annotate
util/jsonpointer.lua @ 12498:c3e47a5dd30d
util.async: Add Teal description file
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 08 May 2022 15:21:35 +0200 |
parent | 12496:87c3d45208ef |
child | 12499:03e307952816 |
rev | line source |
---|---|
12131
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 local function unescape_token(escaped_token) |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 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
|
3 return unescaped |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 end |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 local function resolve_json_pointer(ref, path) |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 local ptr_len = #path + 1 |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 for part, pos in path:gmatch("/([^/]*)()") do |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 local token = unescape_token(part) |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 if not (type(ref) == "table") then |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 return nil |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 end |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 local idx = next(ref) |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 local new_ref |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 if type(idx) == "string" then |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 new_ref = ref[token] |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 elseif math.type(idx) == "integer" then |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 local i = tonumber(token) |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 if token == "-" then |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 i = #ref + 1 |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 end |
12496
87c3d45208ef
util.jsonpointer: Fix off-by-one in array resolution
Kim Alvefur <zash@zash.se>
parents:
12131
diff
changeset
|
23 new_ref = ref[i + 1] |
12131
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 else |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 return nil, "invalid-table" |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 end |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 if pos == ptr_len then |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 return new_ref |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 elseif type(new_ref) == "table" then |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 ref = new_ref |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 elseif not (type(ref) == "table") then |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 return nil, "invalid-path" |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 end |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 |
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 return ref |
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 return { resolve = resolve_json_pointer } |