Rego:设定理解未定义

发布于 2025-02-04 21:44:31 字数 268 浏览 3 评论 0原文

我试图理解为什么以下有关使用设置理解的示例给出不同的结果:

  1. ​/a>

在第一个示例中,尽管foo [“ c”]未定义,rlt仍将其评估为空集。我希望rlt也会不确定。

在第二个示例中,我删除了该函数,但直接将rlt2设置为设置理解的结果。这次它确实返回了未定义。

有人可以在这里解释区别吗?

I am trying to understand why the following examples on using set comprehension give different results:

  1. https://play.openpolicyagent.org/p/5x5mXmsyr0
  2. https://play.openpolicyagent.org/p/IVQlTYcVpD

In the first example, rlt is evaluated to an empty set despite foo["c"] is undefined. I expect rlt to also be undefined.

In the second example, I removed the function but directly set rlt2 to the result of a set comprehension. This time it does return undefined.

Can someone explain the difference here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酷炫老祖宗 2025-02-11 21:44:31

我认为您在这里看到的是类型的检查器,尽其所能。

您遇到的错误是因为在编译时,类型检查器知道存在哪些键。
对于函数调用,foo是一个函数参数,

myFunc(foo) = rlt {
    rlt := {f | f := foo["c"]}
}

编译器无法分辨foo [“ c”]是否存在或不存在 - 这取决于实际调用。您可能会这样定义该函数,但是以其他方式使用它,

$ echo '{"c": 123}' | opa eval -I -d policy.rego 'data.play.myFunc(input)'

因此它不会进行任何更深的流分析。

现在,rlt不是不确定的,因为设置(对象,数组)综合永远不会不确定 - 如果它们的身体始终不确定,则整个集合将变为空集(对象,数组)。

I think what you see here is the type checker doing the best it can.

The error you get, is because at compile time, the type checker knows which keys exist.
For the function call, foo is a function argument,

myFunc(foo) = rlt {
    rlt := {f | f := foo["c"]}
}

and the compiler cannot tell if foo["c"] exists or does not exist -- that depends on the actual call. You might define the function like that, but use it in other ways like

$ echo '{"c": 123}' | opa eval -I -d policy.rego 'data.play.myFunc(input)'

So it doesn't do any kind of deeper flow analysis.

Now rlt is not undefined because set (object, array) comprehensions are never undefined -- if their bodies are always undefined, the overall collection becomes an empty set (object, array).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文