forked from oracle/graal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common-utils.libsonnet
66 lines (66 loc) · 1.46 KB
/
common-utils.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
{
# composable: make an object composable
#
# When composing objects with `+`, the RHS overrides the LHS when fields collide,
# unless the RHS defines fields with `+:`. This is especially an issue when
# importing nested objects from JSON, where all fields are implicitly defined
# with `:`. To solve this, `composable` essentially turns `:` fields into `+:`.
#
# See the following example:
#
# input.jsonnet
# <code>
# {
# composable:: ...,
# obj1:: {
# key1: {
# foo: "foo",
# },
# key2: "key2",
# },
# obj2:: {
# key1: {
# baz : "baz",
# },
# key3: "key3",
# },
# res1: self.obj1 + self.obj2,
# res2: self.composable(self.obj1) + self.composable(self.obj2),
# }
# </code>
#
# Output of jsonnet input.jsonnet
# <code>
# {
# "res1": {
# "key1": {
# "baz": "baz"
# },
# "key2": "key2",
# "key3": "key3"
# },
# "res2": {
# "key1": {
# "baz": "baz",
# "foo": "foo"
# },
# "key2": "key2",
# "key3": "key3"
# }
# }
# </code>
# Note the missing `res1.key1.foo`!
#
local _composable(o) =
std.foldl(function(obj, key)
obj +
if std.type(o[key]) == "object" then
{ [key] +: _composable(o[key]) }
else
{ [key] : o[key] },
std.objectFields(o),
{}
),
# exported name
composable(o) :: _composable(o),
}