-
Notifications
You must be signed in to change notification settings - Fork 7
/
aggregate.libsonnet
104 lines (91 loc) · 2.02 KB
/
aggregate.libsonnet
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
local d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet';
{
local this = self,
'#': d.pkg(
name='aggregate',
url='github.com/jsonnet-libs/xtd/aggregate.libsonnet',
help=|||
`aggregate` implements helper functions to aggregate arrays of objects into objects with arrays.
Example:
```jsonnet
local apps = [
{
appid: 'id1',
name: 'yo',
id: i,
}
for i in std.range(0, 10)
];
aggregate.byKeys(apps, ['appid', 'name']);
```
Output:
```json
{
"id1": {
"yo": [
{
"appid": "id1",
"id": 0,
"name": "yo"
},
{
"appid": "id1",
"id": 1,
"name": "yo"
},
...
]
}
}
```
|||,
),
'#byKey':: d.fn(
|||
`byKey` aggregates an array by the value of `key`
|||,
[
d.arg('arr', d.T.array),
d.arg('key', d.T.string),
]
),
byKey(arr, key):
// find all values of key
local values = std.set([
item[key]
for item in arr
]);
// create the aggregate for the value of each key
{
[value]: [
item
for item in std.filter(
function(x)
x[key] == value,
arr
)
]
for value in values
},
'#byKeys':: d.fn(
|||
`byKey` aggregates an array by iterating over `keys`, each item in `keys` nests the
aggregate one layer deeper.
|||,
[
d.arg('arr', d.T.array),
d.arg('keys', d.T.array),
]
),
byKeys(arr, keys):
local aggregate = self.byKey(arr, keys[0]);
// if last key in keys
if std.length(keys) == 1
// then return aggregate
then aggregate
// else aggregate with remaining keys
else {
[k]: this.byKeys(aggregate[k], keys[1:])
for k in std.objectFields(aggregate)
},
}