【发布时间】:2021-10-29 15:05:41
【问题描述】:
每次我觉得我终于理解了 jsonnet 的时候,它就来打我的脸……-.-
我有类似以下的内容:
local applyModifications(kp) = {
[topLvlKey]: {
[subKey]: myfunction(kp[topLvlKey][subKey])
for subKey in std.objectFieldsAll(kp[topLvlKey])
},
for topLvlKey in std.objectFieldsAll(kp)
};
我想遍历对象前 2 层内的所有内容,并在其中应用一些函数...
基本上可以...但取决于我使用的是std.objectFieldsAll 还是std.objectFields,隐藏字段之后是可见的还是完全丢失。
如果不触及隐藏的“属性”,我将/可以如何做到这一点? 我知道我的问题是,我在这里使用了对象理解并且(参考错误消息)那些“对象理解不能有隐藏字段”...... 但据我了解 jsonnet,something-comprehensions 是创建 for 循环的唯一方法,对吧?
测试代码:
// 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
}
【问题讨论】:
标签: jsonnet