Skip to content

Commit

Permalink
docs/content: fix go integration example
Browse files Browse the repository at this point in the history
1. module should be policy string above
2. pass context instance for .Eval

also add decision output in the example

Fixes #2367

Signed-off-by: Gaga Pan <gaga5lala1013@gmail.com>
  • Loading branch information
gaga5lala authored and tsandall committed Jun 23, 2020
1 parent 02b6214 commit 12da577
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion docs/content/integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,28 @@ parameterized with different options like the query, policy module(s), data
store, etc.

```go

module := `
package example.authz
default allow = false
allow {
some id
input.method = "GET"
input.path = ["salary", id]
input.subject.user = id
}
allow {
is_admin
}
is_admin {
input.subject.groups[_] = "admin"
}
`

query, err := rego.New(
rego.Query("x = data.example.authz.allow"),
rego.Module("example.rego", module),
Expand All @@ -202,7 +224,8 @@ input := map[string]interface{}{
},
}

results, err := query.Eval(context.Context, rego.EvalInput(input))
ctx := context.TODO()
results, err := query.Eval(ctx, rego.EvalInput(input))
```

The `rego.PreparedEvalQuery#Eval` function returns a _result set_ that contains
Expand All @@ -221,6 +244,7 @@ if err != nil {
// Handle unexpected result type.
} else {
// Handle result/decision.
// fmt.Printf("%+v", results) => [{Expressions:[true] Bindings:map[x:true]}]
}
```

Expand Down

0 comments on commit 12da577

Please sign in to comment.