Skip to content

Commit

Permalink
Small fixes (#1367)
Browse files Browse the repository at this point in the history
- Use `count` on `data.eval.params.ignore_files` as it's now
  always defined
- Set `inmem.OptReturnASTValuesOnRead(true)` on the LSP inmem
  store for a substantial performance boost!
- Remove `rego.StoreReadAST(true)` which was ignored since we
  set the store explicitly

Signed-off-by: Anders Eknert <anders@styra.com>
  • Loading branch information
anderseknert authored Jan 27, 2025
1 parent 33408e5 commit 1db229a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
6 changes: 3 additions & 3 deletions bundle/regal/config/exclusion.rego
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ excluded_file(category, title, file) if {
_exclude(pattern, file)
}

_global_ignore_patterns := data.eval.params.ignore_files

_global_ignore_patterns := merged_config.ignore.files if not data.eval.params.ignore_files
_global_ignore_patterns := data.eval.params.ignore_files if {
count(data.eval.params.ignore_files) > 0
} else := merged_config.ignore.files

# exclude imitates .gitignore pattern matching as best it can
# ref: https://git-scm.com/docs/gitignore#_pattern_format
Expand Down
1 change: 0 additions & 1 deletion internal/lsp/completions/providers/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ func prepareQuery(ctx context.Context, store storage.Store, query string) (*rego

func prepareRegoArgs(store storage.Store, query ast.Body) []func(*rego.Rego) {
return []func(*rego.Rego){
rego.StoreReadAST(true),
rego.Store(store),
rego.ParsedQuery(query),
rego.ParsedBundle("regal", &rbundle.LoadedBundle),
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func NewRegalStore() storage.Store {
// we'll need to conform to the most basic "JSON" format understood by the store
"defined_refs": map[string]any{},
},
}, inmem.OptRoundTripOnWrite(false))
}, inmem.OptRoundTripOnWrite(false), inmem.OptReturnASTValuesOnRead(true))
}

func transact(ctx context.Context, store storage.Store, writeMode bool, op func(txn storage.Transaction) error) error {
Expand Down
31 changes: 25 additions & 6 deletions internal/lsp/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ package lsp
import (
"context"
"encoding/json"
"slices"
"fmt"
"testing"

"github.com/open-policy-agent/opa/v1/ast"
"github.com/open-policy-agent/opa/v1/storage"

"github.com/styrainc/regal/internal/parse"
)

type illegalResolver struct{}

func (illegalResolver) Resolve(ref ast.Ref) (interface{}, error) {
return nil, fmt.Errorf("illegal value: %v", ref)
}

func TestPutFileModStoresRoastRepresentation(t *testing.T) {
t.Parallel()

Expand All @@ -28,7 +35,17 @@ func TestPutFileModStoresRoastRepresentation(t *testing.T) {
t.Fatalf("store.Read failed: %v", err)
}

pretty, err := json.MarshalIndent(parsed, "", " ")
parsedVal, ok := parsed.(ast.Value)
if !ok {
t.Fatalf("expected ast.Value, got %T", parsed)
}

parsedMap, err := ast.ValueToInterface(parsedVal, illegalResolver{})
if err != nil {
t.Fatalf("ast.ValueToInterface failed: %v", err)
}

pretty, err := json.MarshalIndent(parsedMap, "", " ")
if err != nil {
t.Fatalf("json.MarshalIndent failed: %v", err)
}
Expand Down Expand Up @@ -95,12 +112,14 @@ func TestPutFileRefs(t *testing.T) {
t.Fatalf("store.Read failed: %v", err)
}

arr, ok := value.([]string)
arr, ok := value.(*ast.Array)
if !ok {
t.Fatalf("expected []string, got %T", value)
t.Fatalf("expected *ast.Array, got %T", value)
}

if !slices.Equal(arr, []string{"foo", "bar"}) {
t.Fatalf("expected [foo bar], got %v", arr)
expected := ast.NewArray(ast.StringTerm("foo"), ast.StringTerm("bar"))

if !expected.Equal(arr) {
t.Errorf("expected %v, got %v", expected, arr)
}
}
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ func fromOPABuiltin(builtin ast.Builtin) *Builtin {
func fromOPACapabilities(capabilities *ast.Capabilities) *Capabilities {
var result Capabilities

result.Builtins = make(map[string]*Builtin)
result.Builtins = make(map[string]*Builtin, len(capabilities.Builtins))

for _, builtin := range capabilities.Builtins {
result.Builtins[builtin.Name] = fromOPABuiltin(*builtin)
Expand Down

0 comments on commit 1db229a

Please sign in to comment.