-
Notifications
You must be signed in to change notification settings - Fork 0
/
dot_jq
51 lines (45 loc) · 2.44 KB
/
dot_jq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def decode_dynamodb:
def _sprop($key): select(keys == [$key])[$key]; # single property objects only
((objects | { value: _sprop("S") }) # string (from string)
// (objects | { value: _sprop("B") }) # blob (from string)
// (objects | { value: _sprop("N") | tonumber }) # number (from string)
// (objects | { value: _sprop("BOOL") }) # boolean (from boolean)
// (objects | { value: _sprop("M") | map_values(decode_ddb) }) # map (from object)
// (objects | { value: _sprop("L") | map(decode_ddb) }) # list (from encoded array)
// (objects | { value: _sprop("SS") }) # string set (from string array)
// (objects | { value: _sprop("NS") | map(tonumber) }) # number set (from string array)
// (objects | { value: _sprop("BS") }) # blob set (from string array)
// (objects | { value: map_values(decode_ddb) }) # all other non-conforming objects
// (arrays | { value: map(decode_ddb) }) # all other non-conforming arrays
// { value: . }).value # everything else
;
def encode_dynamodb:
if type == "string" then {"S": . } elif
type == "number" then {"N": "\(.)"} elif
type == "boolean" then {"BOOL": . } elif
type == "null" then {"NULL": true } elif
type == "object" then {"M": . | map_values(encode_dynamodb) } elif
type == "array" then {"L": . | map(encode_dynamodb) } else
. end;
def to_dynamodb_batch($t):
{"\($t)": . | map({"PutRequest": {"Item": . | map_values(encode_dynamodb) }})};
def nextRandomNumber($state):
( (214013 * $state) + 2531011) % 2147483648 # mod 2^31
| { newState: .,
randomNumber: (. / 65536 | floor) };
def nextRandomNumbers($state; $count):
[foreach range($count) as $x (nextRandomNumber($state); nextRandomNumber(.newState); .)]
| { newState: .[-1].newState,
randomNumbers: map(.randomNumber) };
def hexByte:
[. / 256 % 16, . % 16]
| map(if . < 10 then . + 48 else . + 87 end) # ASCII: 0...9: 48-57, a...f: 97-102
| implode;
def nextRandomUUID($state):
nextRandomNumbers($state; 16)
| .newState as $newState
| .randomNumbers
| map(hexByte)
| "\(.[0:4] | join(""))-\(.[4:6] | join(""))-\(.[6:8] | join(""))-\(.[8:10] | join(""))-\(.[10:] | join(""))"
| { newState: $newState,
randomUUID: . };