Code from github.com/anakrish/rego-rs

Authored by anakrish and mingweishih

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
Anand Krishnamoorthi
2023-02-09 10:56:54 -08:00
parent 8f67aeecb0
commit cb0b3a1790
85 changed files with 11144 additions and 0 deletions
@@ -0,0 +1,40 @@
# Copyright (c) Rego-Rs Authors.
# Licensed under the Apache 2.0 license.
cases:
- note: basic
rego: |
package test
x = 1 - 2 * 3 / 4 + 2
policy:
- spec:
head:
compr:
refr:
var: x
assign:
op: =
value:
arithexpr:
op: +
lhs:
arithexpr:
op: "-"
lhs:
number: 1
rhs:
arithexpr:
op: "/"
lhs:
arithexpr:
op: "*"
lhs:
number: 2
rhs:
number: 3
rhs:
number: 4
rhs:
number: 2
bodies: []
@@ -0,0 +1,209 @@
# Copyright (c) Rego-Rs Authors.
# Licensed under the Apache 2.0 license.
cases:
- note: case1-compr
rego: |
package test
x = {1}
y = {2}
z = [ x | y ] #, 5] # in {5}, 6]
policy:
- --skip--
- --skip--
- spec:
head:
compr:
refr:
var: z
assign:
op: =
value:
arraycompr:
term:
var: x
query:
stmts:
- span: y
literal:
expr:
var: y
bodies: []
- note: case2-array
rego: |
package test
x = {1}
y = {2}
z = [ x | y, 5] # in {5}, 6]
policy:
- --skip--
- --skip--
- spec:
head:
compr:
refr:
var: z
assign:
op: =
value:
array:
- binexpr:
op: "|"
lhs:
var: x
rhs:
var: y
- number: 5
bodies: []
- note: case3-compr
rego: |
package test
import future.keywords.in
x = {1}
y = {2}
z = [ x | y, 5 in {5}] #, 6]
policy:
- --skip--
- --skip--
- spec:
head:
compr:
refr:
var: z
assign:
op: =
value:
arraycompr:
term:
var: x
query:
stmts:
- span: y, 5 in {5}
literal:
expr:
inexpr:
key:
var: y
value:
number: 5
collection:
set:
- number: 5
bodies: []
- note: case4-array
rego: |
package test
import future.keywords.in
x = {1}
y = {2}
z = [ x | y, 5 in {5}, 6]
policy:
- --skip--
- --skip--
- spec:
head:
compr:
refr:
var: z
assign:
op: =
value:
array:
- binexpr:
op: "|"
lhs:
var: x
rhs:
var: y
- inexpr:
key:
number: 5
collection:
set:
- number: 5
- number: 6
bodies: []
- note: case5-array
rego: |
package test
import future.keywords.in
x = {1}
y = {2}
z = [ x - {10} | y, 5 in {5}] #, 6]
policy:
- --skip--
- --skip--
- spec:
head:
compr:
refr:
var: z
assign:
op: =
value:
array:
- binexpr:
op: "|"
lhs:
arithexpr:
op: "-"
lhs:
var: x
rhs:
set:
- number: 10
rhs:
var: y
- inexpr:
key:
number: 5
collection:
set:
- number: 5
bodies: []
- note: case6-compr
rego: |
package test
import future.keywords.in
x = {1}
y = {2}
z = [ (x - {10}) | y, 5 in {5}] #, 6]
policy:
- --skip--
- --skip--
- spec:
head:
compr:
refr:
var: z
assign:
op: =
value:
arraycompr:
term:
arithexpr:
op: "-"
lhs:
var: x
rhs:
set:
- number: 10
query:
stmts:
- literal:
expr:
inexpr:
key:
var: y
value:
number: 5
collection:
set:
- number: 5
bodies: []
+108
View File
@@ -0,0 +1,108 @@
# Copyright (c) Rego-Rs Authors.
# Licensed under the Apache 2.0 license.
cases:
- note: all
rego: |
package test
# One item
x = {1}
# Multiple items
y = [
2.5, "abc", [ 4, `raw`, # Trailing comma
],
# Empty array
[],
# Nested empty
[[[[[]]]]]
]
policy:
- spec:
head:
compr:
refr:
var: x
assign:
span: = {1}
op: "="
value:
set:
- number: 1
bodies: --skip--
- spec:
head:
compr:
refr:
var: y
assign:
op: "="
value:
array:
- number: 2.5
- string: abc
- array:
- number: 4
- rawstring: raw
- array: []
- array:
- array:
- array:
- array:
- array: []
bodies: []
- note: trailing-comma
rego: |
package test
x = [1,]
y = [1,2
,]
policy:
- spec:
head:
compr:
refr:
var: x
assign:
op: "="
value:
array:
span: "[1,]"
values:
- number: 1
bodies: []
- spec:
head:
compr:
refr:
var: y
assign:
op: "="
value:
array:
span: "[1,2\n,]"
values:
- number: 1
- number: 2
bodies: []
- note: no-comma
rego: |
package test
x = [1 2]
error: expecting `]` while parsing array
- note: two-trailing-commas
rego: |
package test
x = [1,2,,]
error: expecting expression
- note: unclosed
rego: |
package test
x = [ 1
error: expecting `]` while parsing array
+64
View File
@@ -0,0 +1,64 @@
# Copyright (c) Rego-Rs Authors.
# Licensed under the Apache 2.0 license.
cases:
- note: basic
rego: |
package test
# & has higher precedence than |.
# If not the following expressions would evaluate to empty set.
x = {1, 2, 3} | {2} & {4}
y = {2} & {4} | {1, 2, 3}
policy:
- spec:
head:
compr:
refr:
var: x
assign:
op: =
value:
binexpr:
op: "|"
lhs:
set:
- number: 1
- number: 2
- number: 3
rhs:
binexpr:
op: "&"
lhs:
set:
- number: 2
rhs:
set:
- number: 4
bodies: []
- spec:
head:
compr:
refr:
var: y
assign:
op: =
value:
binexpr:
op: "|"
rhs:
set:
- number: 1
- number: 2
- number: 3
lhs:
binexpr:
op: "&"
lhs:
set:
- number: 2
rhs:
set:
- number: 4
bodies: []
+43
View File
@@ -0,0 +1,43 @@
# Copyright (c) Rego-Rs Authors.
# Licensed under the Apache 2.0 license.
cases:
- note: basic
rego: |
package test
# bool ops have lower precedence than arithmetic operators
x = 1 + 2 > 3 - 2
# TODO: lock down
# different types against object
# different types against set
# strings etc
policy:
- spec:
head:
compr:
refr:
var: x
assign:
op: =
value:
boolexpr:
op: ">"
lhs:
arithexpr:
op: "+"
lhs:
number: 1
rhs:
number: 2
rhs:
arithexpr:
op: "-"
lhs:
number: 3
rhs:
number: 2
bodies: []
+48
View File
@@ -0,0 +1,48 @@
# Copyright (c) Rego-Rs Authors.
# Licensed under the Apache 2.0 license.
cases:
- note: basic
rego: |
package test
# A call in rule-ref. This creates an object.
deny[sprintf("Hello %v", ["world"])] = 1
# Trailing comma
x = inc(5,)
policy:
- spec:
head:
compr:
refr:
refbrack:
refr:
var: deny
index:
call:
fcn:
var: sprintf
params:
- string: "Hello %v"
- array:
- string: "world"
assign:
op: =
value:
number: 1
bodies: []
- spec:
head:
compr:
refr:
var: x
assign:
op: "="
value:
call:
fcn:
var: inc
params:
- number: 5
bodies: []
+59
View File
@@ -0,0 +1,59 @@
# Copyright (c) Rego-Rs Authors.
# Licensed under the Apache 2.0 license.
cases:
- note: basic
rego: |
package test
import future.keywords.in
x = 5 in [4, 5]
# in-exprs are left associative and ahve lower-precedence than bin-expr.
# The following will be parsed as
# (5 in [4, 5]) in (set() | {true})
z = 5 in [4, 5] in set() | {true}
policy:
- spec:
head:
compr:
refr:
var: x
assign:
op: "="
value:
inexpr:
key:
number: 5
collection:
array:
- number: 4
- number: 5
bodies: []
- spec:
head:
compr:
refr:
var: z
assign:
op: "="
value:
inexpr:
key:
inexpr:
key:
number: 5
collection:
array:
- number: 4
- number: 5
collection:
binexpr:
op: "|"
lhs:
set: []
rhs:
set:
- true
bodies: []
@@ -0,0 +1,53 @@
# Copyright (c) Rego-Rs Authors.
# Licensed under the Apache 2.0 license.
cases:
- note: basic
rego: |
package test
import future.keywords.in
# key, value in collection
x = 0, 5 in [5]
# Chained in-exprs
y = 0, 5 in c in d
policy:
- spec:
head:
compr:
refr:
var: x
assign:
op: "="
value:
inexpr:
key:
number: 0
value:
number: 5
collection:
array:
- number: 5
bodies: []
- spec:
head:
compr:
refr:
var: y
assign:
op: "="
value:
inexpr:
key:
inexpr:
key:
number: 0
value:
number: 5
collection:
var: c
collection:
var: d
bodies: []
+163
View File
@@ -0,0 +1,163 @@
# Copyright (c) Rego-Rs Authors.
# Licensed under the Apache 2.0 license.
cases:
- note: all
rego: |
package test
import future.keywords.in
# Empty object
x = {}
# Single field
y = {
"a" : 5
}
# Multiple fields
z = {
"a" : 5,
"b" : [ 1, 2, 3 ],
"c" : { 4, 5, 6 },
"d" : {
"a" : 5,
"b" : set()
}
# array as key
, [1,
2,
3] : 4,
# set as key
{ 1 } : 2,
# Object as key
{
"a" : 1,
"b" : 2, # Trailing comma
} : `hello,
world`
# Null, boolean
, null: false,
true : true,
# Only single var in is supported as value
"p" : "q", "r" in "d" : "e"
}
policy:
- spec:
span: x = {}
head:
compr:
span: x = {}
refr:
var: x
assign:
span: = {}
op: =
value:
object:
fields: []
bodies: []
- spec:
head:
compr:
refr:
var: y
assign:
op: =
value:
object:
fields:
- key:
string: a
value:
number: 5
bodies: []
- spec:
head:
compr:
refr:
var: z
assign:
op: =
value:
object:
fields:
- key:
string: a
value:
number: 5
- key:
string: b
value:
array:
- number: 1
- number: 2
- number: 3
- key:
string: c
value:
set:
- number: 4
- number: 5
- number : 6
- key:
string: d
value:
object:
fields:
- key:
string: a
value:
number: 5
- key:
string: b
value:
set: []
- key:
array:
- number: 1
- number: 2
- number: 3
value:
number: 4
- key:
set:
- number: 1
value:
number: 2
- key:
object:
fields:
- key:
string: a
value:
number: 1
- key:
string: b
value:
number: 2
value:
rawstring: "hello,\n world"
- key: null
value: false
- key: true
value: true
- key:
string: p
value:
string: q
- key:
inexpr:
key:
string: r
collection:
string: d
value:
string: e
bodies: []
@@ -0,0 +1,209 @@
# Copyright (c) Rego-Rs Authors.
# Licensed under the Apache 2.0 license.
cases:
- note: case1-compr
rego: |
package test
x = {1}
y = {2}
z = { x | y } #, 5} # in {5}, 6}
policy:
- --skip--
- --skip--
- spec:
head:
compr:
refr:
var: z
assign:
op: =
value:
setcompr:
term:
var: x
query:
stmts:
- span: y
literal:
expr:
var: y
bodies: []
- note: case2-set
rego: |
package test
x = {1}
y = {2}
z = { x | y, 5} # in {5}, 6}
policy:
- --skip--
- --skip--
- spec:
head:
compr:
refr:
var: z
assign:
op: =
value:
set:
- binexpr:
op: "|"
lhs:
var: x
rhs:
var: y
- number: 5
bodies: []
- note: case3-compr
rego: |
package test
import future.keywords.in
x = {1}
y = {2}
z = { x | y, 5 in {5}} #, 6}
policy:
- --skip--
- --skip--
- spec:
head:
compr:
refr:
var: z
assign:
op: =
value:
setcompr:
term:
var: x
query:
stmts:
- span: y, 5 in {5}
literal:
expr:
inexpr:
key:
var: y
value:
number: 5
collection:
set:
- number: 5
bodies: []
- note: case4-set
rego: |
package test
import future.keywords.in
x = {1}
y = {2}
z = { x | y, 5 in {5}, 6}
policy:
- --skip--
- --skip--
- spec:
head:
compr:
refr:
var: z
assign:
op: =
value:
set:
- binexpr:
op: "|"
lhs:
var: x
rhs:
var: y
- inexpr:
key:
number: 5
collection:
set:
- number: 5
- number: 6
bodies: []
- note: case5-set
rego: |
package test
import future.keywords.in
x = {1}
y = {2}
z = { x - {10} | y, 5 in {5}} #, 6}
policy:
- --skip--
- --skip--
- spec:
head:
compr:
refr:
var: z
assign:
op: =
value:
set:
- binexpr:
op: "|"
lhs:
arithexpr:
op: "-"
lhs:
var: x
rhs:
set:
- number: 10
rhs:
var: y
- inexpr:
key:
number: 5
collection:
set:
- number: 5
bodies: []
- note: case6-compr
rego: |
package test
import future.keywords.in
x = {1}
y = {2}
z = { (x - {10}) | y, 5 in {5}} #, 6}
policy:
- --skip--
- --skip--
- spec:
head:
compr:
refr:
var: z
assign:
op: =
value:
setcompr:
term:
arithexpr:
op: "-"
lhs:
var: x
rhs:
set:
- number: 10
query:
stmts:
- literal:
expr:
inexpr:
key:
var: y
value:
number: 5
collection:
set:
- number: 5
bodies: []
+112
View File
@@ -0,0 +1,112 @@
# Copyright (c) Rego-Rs Authors.
# Licensed under the Apache 2.0 license.
cases:
- note: all
rego: |
package test
# One item
x = {1}
# Multiple items
y = {
2.5, "abc", { 4, `raw`
},
# Empty set has a special syntax
set( ),
# Nested empty
{{{{{}}}}}
}
policy:
- spec:
head:
compr:
refr:
var: x
assign:
span: = {1}
op: "="
value:
set:
- number: 1
bodies: --skip--
- spec:
head:
compr:
refr:
var: y
assign:
op: "="
value:
set:
- number: 2.5
- string: abc
- set:
- number: 4
- rawstring: raw
- set: []
- set:
- set:
- set:
- set:
- object:
span: "{}"
fields: []
bodies: []
- note: trailing-comma
rego: |
package test
x = {1,}
y = {1,2
,}
policy:
- spec:
head:
compr:
refr:
var: x
assign:
op: "="
value:
set:
span: "{1,}"
values:
- number: 1
bodies: []
- spec:
head:
compr:
refr:
var: y
assign:
op: "="
value:
set:
span: "{1,2\n,}"
values:
- number: 1
- number: 2
bodies: []
- note: no-comma
rego: |
package test
x = {1 2}
error: expecting `}` while parsing set
- note: two-trailing-commas
rego: |
package test
x = {1,2,,}
error: expecting expression
- note: unclosed
rego: |
package test
x = { 1
error: expecting `}` while parsing set