Rego object.get 多级钥匙

Rego object.get with multileve key

有什么方法可以将 object.get 与多级密钥一起使用..?

我的输入是这样的: { "pipelineParameters" : { "k8" : { "NODES" : "1" }, "ec2": { "NODES" : "0" } }

我的数据看起来像 { "key": "pipelineParameters.k8.NODES" }

如何从基于多级键的输入中获取值

示例代码

https://play.openpolicyagent.org/p/iR15XnMctP

object.get功能不支持多级按键。如果您将键表示为数组,则可以为此使用 walk 函数:

input = {
    "pipelineParameters" : {
        "k8" : {
            "NODES" : "1"
        },
        "ec2": {
           "NODES" : "0"
        }
    }
}

例如:

> walk(input, [["pipelineParameters", "k8", "NODES"], "1"])
true
> walk(input, [["pipelineParameters", "k8",  "NODES"], x])
+-----+
|  x  |
+-----+
| "1" |
+-----+
> walk(input, [["pipelineParameters", y,  "NODES"], x])
+-----+-------+
|  x  |   y   |
+-----+-------+
| "1" | "k8"  |
| "0" | "ec2" |
+-----+-------+

要将您的密钥转换为数组,您只需编写:

split(key, ".")

例如:

split("pipelineParameters.k8.NODES", ".")
[
  "pipelineParameters",
  "k8",
  "NODES"
]

综合起来:

> walk(input, [split("pipelineParameters.k8.NODES", "."), x])
+-----+
|  x  |
+-----+
| "1" |
+-----+