Skip to content

Commit

Permalink
Update to rego.v1
Browse files Browse the repository at this point in the history
Signed-off-by: Charlie Egan <[email protected]>
  • Loading branch information
charlieegan3 committed Jul 29, 2024
1 parent 8dde331 commit 6416e96
Show file tree
Hide file tree
Showing 23 changed files with 86 additions and 104 deletions.
6 changes: 0 additions & 6 deletions .regal/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ rules:
files:
- cheats/comprehensions/sets/cheat.rego
- cheats/comprehensions/objects/cheat.rego
imports:
# ignore rego.v1 imports as they'd make examples more verbose.
use-rego-v1:
level: ignore
implicit-future-keywords:
level: ignore
style:
opa-fmt:
ignore:
Expand Down
58 changes: 27 additions & 31 deletions build/cheatsheet.md

Large diffs are not rendered by default.

Binary file modified build/cheatsheet.pdf
Binary file not shown.
58 changes: 27 additions & 31 deletions build/cheatsheet.tex

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions cheats/builtins/aggregates/cheat.rego
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package cheat

import future.keywords
import rego.v1

vals := [5, 1, 4, 2, 3]

vals_count := count(vals)
vals_max := max(vals)
vals_min := min(vals)
Expand Down
2 changes: 1 addition & 1 deletion cheats/builtins/object-transforms/cheat.rego
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cheat

import future.keywords
import rego.v1

unioned := object.union({"foo": true}, {"bar": false})

Expand Down
2 changes: 1 addition & 1 deletion cheats/builtins/objects/cheat.rego
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cheat

import future.keywords
import rego.v1

obj := {"userid": "18472", "roles": [{"name": "admin"}]}

Expand Down
2 changes: 1 addition & 1 deletion cheats/builtins/regex/cheat.rego
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cheat

import future.keywords
import rego.v1

example_string := "Build Policy as Code with OPA!"

Expand Down
2 changes: 1 addition & 1 deletion cheats/builtins/strings/cheat.rego
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cheat

import future.keywords
import rego.v1

example_string := "Build Policy as Code with OPA!"

Expand Down
2 changes: 1 addition & 1 deletion cheats/comprehensions/arrays/cheat.rego
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cheat

import future.keywords
import rego.v1

doubled := [m |
some n in [1, 2, 3, 3]
Expand Down
4 changes: 2 additions & 2 deletions cheats/comprehensions/objects/cheat.rego
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cheat

import future.keywords
import rego.v1

is_even := {number: is_even |
is_even[number] := is_even if {
some number in [1, 2, 3, 4]
is_even := (number % 2) == 0
}
4 changes: 2 additions & 2 deletions cheats/comprehensions/sets/cheat.rego
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cheat

import future.keywords
import rego.v1

unique_doubled := {m |
unique_doubled contains m if {
some n in [10, 20, 30, 20, 10]
m := n * 2
}
2 changes: 1 addition & 1 deletion cheats/control_flow/and/cheat.rego
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cheat

import future.keywords
import rego.v1

valid_staff_email if {
regex.match(`^\S+@\S+\.\S+$`, input.email)
Expand Down
9 changes: 6 additions & 3 deletions cheats/control_flow/or/cheat.rego
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package cheat

import future.keywords
import rego.v1

# using multiple rules
valid_email if endswith(input.email, "@example.com")

valid_email if endswith(input.email, "@example.org")

valid_email if endswith(input.email, "@example.net")

# using functions
allowed_firstname(name) if {
startswith(name, "a")
count(name) > 2
startswith(name, "a")
count(name) > 2
}

allowed_firstname("joe") # if name == 'joe'

valid_name if {
Expand Down
6 changes: 2 additions & 4 deletions cheats/control_flow/or/output.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{
"email": "[email protected]",
"name": "anna",
"method": "GET"
}
"email": "[email protected]", "name": "anna", "method": "GET"
}
2 changes: 1 addition & 1 deletion cheats/debugging/print/cheat.rego
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cheat

import future.keywords
import rego.v1

allowed_users := {"alice", "bob", "charlie"}

Expand Down
2 changes: 1 addition & 1 deletion cheats/iteration/every/cheat.rego
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cheat

import future.keywords
import rego.v1

allow if {
prefix := sprintf("/docs/%s/", [input.userID])
Expand Down
2 changes: 1 addition & 1 deletion cheats/iteration/some/cheat.rego
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cheat

import future.keywords
import rego.v1

all_regions := {
"emea": {"west", "east"},
Expand Down
3 changes: 2 additions & 1 deletion cheats/rules/complete/cheat.rego
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cheat

import future.keywords
import rego.v1

default allow := false

Expand All @@ -12,4 +12,5 @@ allow if {
default request_quota := 100

request_quota := 1000 if input.user.internal

request_quota := 50 if input.user.plan.trial
2 changes: 1 addition & 1 deletion cheats/rules/partial/cheat.rego
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cheat

import future.keywords
import rego.v1

paths contains "/handbook/*"

Expand Down
7 changes: 2 additions & 5 deletions cheats/rules/partial/output.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
{
"paths": [
"/handbook/*",
"/teams/owl/*", "/teams/tiger/*"
]
}
"paths": ["/handbook/*", "/teams/owl/*", "/teams/tiger/*"]
}
6 changes: 2 additions & 4 deletions cheats/testing/with/cheat.rego
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package cheat

import future.keywords
import rego.v1

allow if {
input.admin == true
}
allow if input.admin == true

test_allow_when_admin if {
allow with input as {"admin": true}
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Cheat struct {
func (c *Cheat) CodeDisplay() string {
code := c.Code
code = strings.Replace(code, "package cheat", "", 1)
code = strings.Replace(code, "import future.keywords", "", 1)
code = strings.Replace(code, "import rego.v1", "", 1)
code = strings.TrimSpace(code)

return code
Expand Down Expand Up @@ -197,7 +197,7 @@ func main() {
panic(err)
}

mdOutputFile, err := os.OpenFile(mdOutputPath, os.O_RDWR, 0644)
mdOutputFile, err := os.OpenFile(mdOutputPath, os.O_RDWR, 0o644)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -239,7 +239,7 @@ func main() {
panic(err)
}

texOutputFile, err := os.OpenFile(texOutputPath, os.O_RDWR, 0644)
texOutputFile, err := os.OpenFile(texOutputPath, os.O_RDWR, 0o644)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 6416e96

Please sign in to comment.