Software /
code /
prosody
Annotate
teal-src/util/jsonpointer.tl @ 12630:781772c8b6d9
mod_mam: Store archives with sub-second precision timestamps
Changes sub-second part of example timestamp to .5 in order to avoid
floating point issues.
Some clients use timestamps when ordering messages which can lead to
messages having the same timestamp ending up in the wrong order.
It would be better to preserve the order messages are sent in, which is
the order they were stored in.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 14 Aug 2022 17:28:31 +0200 |
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 } |