Pure MoonBit Open Policy Agent (OPA) Rego v1 syntax, AST and policy engine
///|
test "compile and evaluate a Rego v1 policy with template strings" {
let src =
#|package okf.decisions
#|
#|# METADATA
#|# title: Decision needs owner
#|deny contains msg if {
#| some doc in input.documents
#| doc.type == "Decision"
#| not doc.fields.owner
#| msg := $"decision {doc.path} has no owner"
#|}
#|
let policy = @rego.compile_sources([src])
let input = @value.Value::from_json_string(
"{\"documents\": [{\"path\": \"docs/adr-1.md\", \"type\": \"Decision\", \"fields\": {}}]}",
)
let res = @rego.eval_query_source(
policy,
"data.okf.decisions.deny",
input=Some(input),
)
assert_eq(
res.first_value(),
Some(
@value.Value::set([
@value.Value::String("decision docs/adr-1.md has no owner"),
]),
),
)
}///|
test "register a custom built-in and evaluate with StepBudget" {
let custom_reg = @rego.tier1_builtins().with_builtin(
@builtins.BuiltinName("custom.double"),
@builtins.BuiltinArity::fixed(1),
(args, _) => {
match args[0] {
Number(d) => Ok(Some(Number(d.mul(@value.Decimal::from_int(2)))))
_ => Ok(None)
}
},
)
let policy = @rego.compile_sources(
["package demo\nans := custom.double(21)\n"],
builtins=custom_reg,
)
let opts = @rego.default_options()
.with_builtins(custom_reg)
.with_step_budget(@rego.step_budget(1_000))
let ans = @rego.eval_rule(policy, ["demo", "ans"], options=opts)
assert_eq(ans, Some(@value.Value::int(42)))
}///|
test "run pure Rego policy tests with Tier-2 built-ins and deterministic Clock" {
let src =
#|package okf.checks_test
#|
#|test_tier2_helpers if {
#| semver.compare("1.10.0", "1.2.0") > 0
#| regex.match(`^ADR-[0-9]{4}$`, "ADR-0042")
#| glob.match("docs/**/*.{md,yaml}", ["/"], "docs/adr/0042.md")
#| units.parse_bytes("2KiB") == 2048
#| time.now_ns() == 1700000000000000000
#|}
#|
#|todo_test_future_rule if {
#| false
#|}
#|
let policy = @rego.compile_sources([src])
let opts = @rego.default_options().with_clock(
@builtins.Clock::from_unix_nanos(1700000000000000000L),
)
let report = @rego.run_tests(policy, options=opts)
assert_true(report.all_passed())
assert_eq(report.passed_count(), 1)
assert_eq(report.skipped_count(), 1)
}///|
test "parse a Rego v1 module with METADATA and lossless CST" {
let src =
#|package okf.decisions
#|
#|# METADATA
#|# title: Decision needs owner
#|deny contains msg if {
#| some doc in input.documents
#| not doc.fields.owner
#| msg := $"decision {doc.path} has no owner"
#|}
#|
let cst = @rego.parse_module_cst(src)
assert_false(cst.has_errors())
assert_eq(cst.to_source(), src)
let mod = @rego.parse_module(src)
assert_eq(mod.package_path().to_string(), "okf.decisions")
assert_eq(mod.rules().length(), 1)
assert_eq(
mod.rules()[0].annotations()[0].title(),
Some("Decision needs owner"),
)
}///|
test "walk a parsed CST with SyntaxCursor" {
let cst = @rego.parse_module_cst("package demo\nallow := true\n")
let root = @syntax.SyntaxCursor::root(cst.root())
guard root.first_child() is Some(pkg) else { fail("missing package") }
assert_eq(pkg.node().kind(), @syntax.PackageDecl)
guard pkg.next_sibling() is Some(rule) else { fail("missing rule") }
assert_eq(rule.node().kind(), @syntax.Rule)
}pub(all) suberror PolicyError {
Lower(LowerError)
Compile(CompileError)
Eval(EvalError)
} derive(Eq, Debug)impl Show for PolicyErrorfn compile(modules : ArrayView[Module], builtins? : BuiltinRegistry) -> CompiledPolicy raise CompileErrorfn compile_sources(sources : ArrayView[String], builtins? : BuiltinRegistry) -> CompiledPolicy raise PolicyErrorfn eval_query(policy : CompiledPolicy, query : CompiledQuery, input? : Value?, data? : Value, options? : EngineOptions) -> EvalResult raise EvalErrorfn eval_query_source(policy : CompiledPolicy, query_source : String, input? : Value?, data? : Value, options? : EngineOptions) -> EvalResult raise PolicyErrorfn eval_rule(policy : CompiledPolicy, path : ArrayView[String], input? : Value?, data? : Value, options? : EngineOptions) -> Value? raise EvalErrorfn run_tests(policy : CompiledPolicy, data? : Value, options? : EngineOptions, filter? : TestFilter) -> TestReportInstall
Download zipPure MoonBit Open Policy Agent (OPA) Rego v1 syntax, AST and policy engine