Software /
code /
prosody
Annotate
teal-src/util/jsonpointer.tl @ 12763:d26eefe98d09
util.dbuffer: Add efficient shortcuts for discard() in certain cases
If the buffer is already empty, nothing to do. If we're throwing away the
whole buffer, we can just empty it and avoid read_chunk() (which in turn
may collapse()). These shortcuts are much more efficient.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Tue, 11 Oct 2022 11:37:55 +0100 |
parent | 12496:87c3d45208ef |
rev | line source |
---|---|
12131
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 local enum ptr_error |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 "invalid-table" |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 "invalid-path" |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 end |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 local function unescape_token(escaped_token : string) : string |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 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
|
9 return unescaped |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 end |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 local function resolve_json_pointer(ref : table, path : string) : any, ptr_error |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 local ptr_len = #path+1 |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 for part, pos in path:gmatch("/([^/]*)()") do |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 local token = unescape_token(part) |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 if not ref is table then |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 return nil |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 end |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 local idx = next(ref) |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 local new_ref : any |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 if idx is string then |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 new_ref = ref[token] |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 elseif idx is integer then |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 local i = tonumber(token) |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 if token == "-" then i = #ref + 1 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 as integer == 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 new_ref is 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 ref is 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 { |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 resolve = resolve_json_pointer, |
b4c0efff8dd3
util.jsonpointer: Resolve JSON Pointers per RFC 6901
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 } |