mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
feat: or keyword (#315)
Add `or` operator to Rego languages. Available via `rego-extensions` Cargo feature. If the evaluated lhs value is not false, null or undefined it is returned. Otherwise rhs is evaluated and returned. or operator has least precedence, and is left-associative. closes #314
This commit is contained in:
committed by
GitHub
parent
8498274356
commit
7565ec3ecf
@@ -0,0 +1,52 @@
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
# Licensed under the MIT License.
|
||||
cases:
|
||||
- note: basic
|
||||
modules:
|
||||
- |
|
||||
package test
|
||||
import rego.v1
|
||||
|
||||
x := data.foo or 2 # undefined lhs
|
||||
y := false or 3 # false rhs
|
||||
z := null or 4
|
||||
a := data.foo or false or null or 5
|
||||
b := startswith("a", "b") or startswith("a", "a")
|
||||
c := 5 in [1,2] or 6 in [6]
|
||||
d := x if {
|
||||
x := false or [1, 2][_]
|
||||
x > 1
|
||||
}
|
||||
e if 1 > 2 or false
|
||||
query: data.test
|
||||
want_result:
|
||||
x: 2
|
||||
y: 3
|
||||
z: 4
|
||||
a: 5
|
||||
b: true
|
||||
c: true
|
||||
d: 2
|
||||
- note: Azure Policy
|
||||
modules:
|
||||
- |
|
||||
package policy
|
||||
|
||||
effect := parameters.effect if {
|
||||
resource.type == "Microsoft.Storage/storageaccounts"
|
||||
resource.properties.networkAcls.defaultAction == "Deny"
|
||||
or count(resource.properties.networkAcls.ipRules) >= 1
|
||||
}
|
||||
|
||||
resource := input.resource
|
||||
parameters := input.parameters
|
||||
input:
|
||||
resource:
|
||||
type: "Microsoft.Storage/storageaccounts"
|
||||
properties:
|
||||
networksAcls:
|
||||
ipRules: ["rule1", "rule2"]
|
||||
parameters:
|
||||
effect: "Deny"
|
||||
query: data.policy.effect
|
||||
want_result: "Deny"
|
||||
+9
-2
@@ -198,6 +198,13 @@ fn match_expr_impl(e: &Expr, v: &Value) -> Result<()> {
|
||||
match_expr(value, &v["inexpr"]["value"])?;
|
||||
match_expr(collection, &v["inexpr"]["collection"])
|
||||
}
|
||||
|
||||
#[cfg(feature = "rego-extensions")]
|
||||
Expr::OrExpr { span, lhs, rhs } => {
|
||||
match_span_opt(span, &v["orexpr"]["span"])?;
|
||||
match_expr(lhs, &v["orexpr"]["lhs"])?;
|
||||
match_expr(rhs, &v["orexpr"]["rhs"])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -324,8 +331,8 @@ fn match_expr_opt(s: &Span, e: &Option<Ref<Expr>>, v: &Value) -> Result<()> {
|
||||
|
||||
fn match_bin_op(s: &Span, op: &BinOp, v: &Value) -> Result<()> {
|
||||
match (op, v) {
|
||||
(BinOp::And, Value::String(s)) if s.as_ref() == "&" => Ok(()),
|
||||
(BinOp::Or, Value::String(s)) if s.as_ref() == "|" => Ok(()),
|
||||
(BinOp::Intersection, Value::String(s)) if s.as_ref() == "&" => Ok(()),
|
||||
(BinOp::Union, Value::String(s)) if s.as_ref() == "|" => Ok(()),
|
||||
_ => bail!(
|
||||
"{}",
|
||||
s.source.message(
|
||||
|
||||
Reference in New Issue
Block a user