Each time I think I finally understood jsonnet, it comes around to hit me in the face ... -.-
I have something like the following:
local applyModifications(kp) = {
[topLvlKey]: {
[subKey]: myfunction(kp[topLvlKey][subKey])
for subKey in std.objectFieldsAll(kp[topLvlKey])
},
for topLvlKey in std.objectFieldsAll(kp)
};
I want to iterate over everything inside the first 2 levels of an object an apply some function there ...
Bascially that works ... But depending on if I use std.objectFieldsAll
or std.objectFields
, hidden fields are visible afterwards or missing completely.
How would/could I do this without touching the hidden "property"? I understand my problem is, that I use a object-comprehension here and (to refer to an error message) that those "Object comprehensions cannot have hidden fields"... But as far as I understand jsonnet, something-comprehensions are the only way to create for-loops, right?
Testcode:
// vim: set ts=2 sw=2 expandtab :
local myfunction(o) = o {
spec+: {
foo: 'bar'
}
};
local applyModifications(kp) = {
[topLvlKey]: {
[subKey]: myfunction(kp[topLvlKey][subKey])
for subKey in std.objectFieldsAll(kp[topLvlKey])
},
for topLvlKey in std.objectFieldsAll(kp)
};
local stack = {
fooService: {
fooResource: {
kind: 'PrometheusRule',
spec: {
groups: [
{ name: 'fooGroup', rules: [{ alert: 'fooAlert', expr: 'fooExpr' }] },
{ name: 'barGroup', rules: [{ alert: 'fooAlert', expr: 'fooExpr' }] },
],
},
},
},
fooService2:: {
fooResource: {
kind: 'PrometheusRule',
spec: {
groups: [
{ name: 'fooGroup', rules: [{ alert: 'fooAlert', expr: 'fooExpr' }] },
{ name: 'barGroup', rules: [{ alert: 'fooAlert', expr: 'fooExpr' }] },
],
},
},
},
};
local stack2 = applyModifications(stack);
{
modified: stack2
}