如何用mixin方式替换jsonnet中数组的一个元素?
how to replace one element of array in jsonnet with mixin way?
我们有从 libsonnet 导入的数组
{
"volumes": [
{
"emptyDir": {},
"name": "grafana-storage"
},
{
"configMap": {
"name": "grafana-dashboard-apiserver"
},
"name": " grafana-dashboard-apiserver"
}
]
}
我要替换这个元素:
{
"emptyDir": {},
"name": "grafana-storage"
},
与这个(相同name
):
{
"name": "grafana-storage",
"persistentVolumeClaim": {
"claimName": "product-nfs"
}
预期输出:
{
"volumes": [
{
"configMap": {
"name": "grafana-dashboard-apiserver"
},
"name": " grafana-dashboard-apiserver"
},
{
"name": "grafana-storage",
"persistentVolumeClaim": {
"claimName": "product-nfs"
}
}
]
}
"appending"很简单,但是如何删除现有的
示例:
local x = {volumes: [
{emptyDir: {}, name: 'grafana-storage' },
{configMap: {name: 'grafana-dashboard-apiserver'
}, name: ' grafana-dashboard-apiserver'},]};
x
+ {volumes+: [{name: 'grafana-storage',persistentVolumeClaim: {claimName:'product-nfs'}}]}
提前致谢~
当谈到 deriving/overloading 时,jsonnet
更容易使用地图(又名 "objects"、"dicts"、"hashes"),策略将是:1) 转换为映射,2) 覆盖,3) 转换回数组。
下面的代码片段实现了它:
local main_obj = {
volumes: [
{
emptyDir: {},
name: "grafana-storage",
},
{
configMap: {
name: "grafana-dashboard-apiserver",
},
name: "grafana-dashboard-apiserver",
},
],
};
local obj_overrides = {
volumes: [
{
name: "grafana-storage",
persistentVolumeClaim: { claimName: "product-nfs" },
},
],
};
// Convert to map for easier overloading, assumes all array elements are maps having "name" field
local toNamedMap(array) = { [x.name]: x for x in array };
// Convert back to array
local toNamedArray(map) = [{ name: x } + map[x] for x in std.objectFields(map)];
toNamedArray(toNamedMap(main_obj.volumes) + toNamedMap(obj_overrides.volumes))
我们有从 libsonnet 导入的数组
{
"volumes": [
{
"emptyDir": {},
"name": "grafana-storage"
},
{
"configMap": {
"name": "grafana-dashboard-apiserver"
},
"name": " grafana-dashboard-apiserver"
}
]
}
我要替换这个元素:
{
"emptyDir": {},
"name": "grafana-storage"
},
与这个(相同name
):
{
"name": "grafana-storage",
"persistentVolumeClaim": {
"claimName": "product-nfs"
}
预期输出:
{
"volumes": [
{
"configMap": {
"name": "grafana-dashboard-apiserver"
},
"name": " grafana-dashboard-apiserver"
},
{
"name": "grafana-storage",
"persistentVolumeClaim": {
"claimName": "product-nfs"
}
}
]
}
"appending"很简单,但是如何删除现有的
示例:
local x = {volumes: [
{emptyDir: {}, name: 'grafana-storage' },
{configMap: {name: 'grafana-dashboard-apiserver'
}, name: ' grafana-dashboard-apiserver'},]};
x
+ {volumes+: [{name: 'grafana-storage',persistentVolumeClaim: {claimName:'product-nfs'}}]}
提前致谢~
当谈到 deriving/overloading 时,jsonnet
更容易使用地图(又名 "objects"、"dicts"、"hashes"),策略将是:1) 转换为映射,2) 覆盖,3) 转换回数组。
下面的代码片段实现了它:
local main_obj = {
volumes: [
{
emptyDir: {},
name: "grafana-storage",
},
{
configMap: {
name: "grafana-dashboard-apiserver",
},
name: "grafana-dashboard-apiserver",
},
],
};
local obj_overrides = {
volumes: [
{
name: "grafana-storage",
persistentVolumeClaim: { claimName: "product-nfs" },
},
],
};
// Convert to map for easier overloading, assumes all array elements are maps having "name" field
local toNamedMap(array) = { [x.name]: x for x in array };
// Convert back to array
local toNamedArray(map) = [{ name: x } + map[x] for x in std.objectFields(map)];
toNamedArray(toNamedMap(main_obj.volumes) + toNamedMap(obj_overrides.volumes))