Created
October 13, 2020 20:22
-
-
Save tsandall/dd83a06a83c44435b8340621f3a47569 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"github.com/open-policy-agent/opa/ast" | |
"github.com/open-policy-agent/opa/rego" | |
) | |
func main() { | |
set := ast.NewTerm(ast.NewSet(ast.StringTerm("hello"), ast.StringTerm("goodbye"))) | |
// Construct a module containing a rule that generates the set. | |
// The set will be located at data.foo.bar. | |
module := &ast.Module{Package: ast.MustParsePackage("package foo")} | |
module.Rules = append(module.Rules, &ast.Rule{ | |
Module: module, | |
Head: &ast.Head{ | |
Name: ast.Var("bar"), | |
Value: set, | |
}, | |
Body: ast.NewBody(ast.NewExpr(ast.BooleanTerm(true))), | |
}) | |
// Check if "hello" is in the set. | |
r := rego.New(rego.ParsedModule(module), rego.Query(`data.foo.bar["hello"]`)) | |
rs, err := r.Eval(context.Background()) | |
fmt.Println(rs) | |
fmt.Println(err) | |
// Check if "deadbeef" is in the set. | |
r = rego.New(rego.ParsedModule(module), rego.Query(`data.foo.bar["deadbeef"]`)) | |
rs, err = r.Eval(context.Background()) | |
fmt.Println(rs) | |
fmt.Println(err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment