forked from kapicorp/kapitan-reference
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.libsonnet
48 lines (37 loc) · 1.54 KB
/
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
// Generic Jsonnet utilities. We might want to propose most of these to the Jsonnet stdlib.
local id = function(value) value;
{
objectGet(object, key, default=null):: if key in object then object[key] else default,
objectHas(object, key, default=false):: if key in object && object[key] != null then true else default,
objectValues(object):: [object[k] for k in std.objectFields(object)],
mergeObjects(objects):: std.foldl(function(a, b) a + b, objects, {}),
trace(object, text=''):: std.trace('Trace %s: %s' % [text, std.toString(object)], object),
deepMerge(obj1, obj2)::
local isIterable = function(value) $.arrayHas(['object', 'array'], std.type(value));
assert isIterable(obj1) : 'obj1 must be iterable';
assert isIterable(obj2) : 'obj2 must be iterable';
assert std.type(obj1) == std.type(obj2) : 'obj1 and obj2 must be of the same type';
obj1
+
obj2
+ (
if std.type(obj1) == 'object' then {
[k]: $.deepMerge(obj1[k], obj2[k])
for k in std.objectFields(obj2)
if k in obj1 && isIterable(obj1[k]) && isIterable(obj2[k])
} else []
)
,
arrayHas(array, item):: [i for i in array if i == item] != [],
// Report whether of the elements in `array` is true
any(array, func=id):: [v for v in array if func(v)] != [],
coalesce(values)::
local non_null_values = [v for v in values if v != null];
if non_null_values != [] then non_null_values[0] else null
,
jsonSchema:: {
nullable(objectSchema):: {
anyOf: [objectSchema, { type: 'null' }],
},
},
}