mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-26 02:04:50 -06:00
5bb4752e73
And update linter conf to ignore v0 directories entirely Finally, added the `nolintlint` linter which will help enforce better nolint hygiene in the project going forward. Signed-off-by: Anders Eknert <anders.eknert@apple.com>
60 lines
1.4 KiB
Go
60 lines
1.4 KiB
Go
// Copyright 2016 The OPA Authors. All rights reserved.
|
|
// Use of this source code is governed by an Apache2
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package repl_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/open-policy-agent/opa/v1/ast"
|
|
"github.com/open-policy-agent/opa/v1/repl"
|
|
"github.com/open-policy-agent/opa/v1/storage/inmem"
|
|
)
|
|
|
|
func ExampleREPL_OneShot() {
|
|
// Initialize context for the example. Normally the caller would obtain the
|
|
// context from an input parameter or instantiate their own.
|
|
ctx := context.Background()
|
|
|
|
// Instantiate the policy engine's storage layer.
|
|
store := inmem.New()
|
|
|
|
// Create a buffer that will receive REPL output.
|
|
var buf bytes.Buffer
|
|
|
|
// Create a new REPL.
|
|
r := repl.New(store, "", &buf, "json", 0, "").
|
|
WithRegoVersion(ast.RegoV1)
|
|
|
|
// Define a rule inside the REPL.
|
|
r.OneShot(ctx, "p if { a = [1, 2, 3, 4]; a[_] > 3 }")
|
|
|
|
// Query the rule defined above.
|
|
r.OneShot(ctx, "p")
|
|
|
|
// Inspect the output. Defining rules does not produce output so we only expect
|
|
// output from the second line of input.
|
|
fmt.Println(buf.String())
|
|
|
|
// Output:
|
|
// {
|
|
// "result": [
|
|
// {
|
|
// "expressions": [
|
|
// {
|
|
// "value": true,
|
|
// "text": "p",
|
|
// "location": {
|
|
// "row": 1,
|
|
// "col": 1
|
|
// }
|
|
// }
|
|
// ]
|
|
// }
|
|
// ]
|
|
// }
|
|
}
|