Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lsp: include print output in eval response #978

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions internal/lsp/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ import (
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/bundle"
"github.com/open-policy-agent/opa/rego"
"github.com/open-policy-agent/opa/topdown"
"github.com/open-policy-agent/opa/topdown/print"

"github.com/styrainc/regal/internal/lsp/uri"
"github.com/styrainc/regal/pkg/builtins"
)

func (l *LanguageServer) Eval(ctx context.Context, query string, input io.Reader) (rego.ResultSet, error) {
func (l *LanguageServer) Eval(
ctx context.Context,
query string,
input io.Reader,
printHook print.Hook,
) (rego.ResultSet, error) {
modules := l.cache.GetAllModules()
moduleFiles := make([]bundle.ModuleFile, 0, len(modules))

Expand All @@ -41,7 +46,7 @@ func (l *LanguageServer) Eval(ctx context.Context, query string, input io.Reader
Modules: moduleFiles,
}

regoArgs := prepareRegoArgs(ast.MustParseBody(query), bd)
regoArgs := prepareRegoArgs(ast.MustParseBody(query), bd, printHook)

pq, err := rego.New(regoArgs...).PrepareForEval(ctx)
if err != nil {
Expand All @@ -68,8 +73,9 @@ func (l *LanguageServer) Eval(ctx context.Context, query string, input io.Reader
}

type EvalPathResult struct {
Value any `json:"value"`
IsUndefined bool `json:"isUndefined"`
Value any `json:"value"`
IsUndefined bool `json:"isUndefined"`
PrintOutput map[int][]string `json:"printOutput"`
}

func FindInput(file string, workspacePath string) io.Reader {
Expand All @@ -88,33 +94,49 @@ func FindInput(file string, workspacePath string) io.Reader {
return nil
}

func (l *LanguageServer) EvalWorkspacePath(ctx context.Context, query string, input io.Reader) (EvalPathResult, error) {
func (l *LanguageServer) EvalWorkspacePath(
ctx context.Context,
query string,
input io.Reader,
) (EvalPathResult, error) {
resultQuery := "result := " + query

result, err := l.Eval(ctx, resultQuery, input)
hook := PrintHook{Output: make(map[int][]string)}

result, err := l.Eval(ctx, resultQuery, input, hook)
if err != nil {
return EvalPathResult{}, fmt.Errorf("failed evaluating query: %w", err)
}

if len(result) == 0 {
return EvalPathResult{IsUndefined: true}, nil
return EvalPathResult{IsUndefined: true, PrintOutput: hook.Output}, nil
}

res, ok := result[0].Bindings["result"]
if !ok {
return EvalPathResult{}, errors.New("expected result in bindings, didn't get it")
}

return EvalPathResult{Value: res}, nil
return EvalPathResult{Value: res, PrintOutput: hook.Output}, nil
anderseknert marked this conversation as resolved.
Show resolved Hide resolved
}

func prepareRegoArgs(query ast.Body, bd bundle.Bundle) []func(*rego.Rego) {
func prepareRegoArgs(query ast.Body, bd bundle.Bundle, printHook print.Hook) []func(*rego.Rego) {
return []func(*rego.Rego){
rego.ParsedQuery(query),
rego.ParsedBundle("workspace", &bd),
rego.Function2(builtins.RegalParseModuleMeta, builtins.RegalParseModule),
rego.Function1(builtins.RegalLastMeta, builtins.RegalLast),
rego.EnablePrintStatements(true),
rego.PrintHook(topdown.NewPrintHook(os.Stderr)),
rego.PrintHook(printHook),
}
}

type PrintHook struct {
Output map[int][]string
}

func (h PrintHook) Print(ctx print.Context, msg string) error {
h.Output[ctx.Location.Row] = append(h.Output[ctx.Location.Row], msg)

return nil
}
6 changes: 0 additions & 6 deletions internal/lsp/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ func TestEvalWorkspacePath(t *testing.T) {
t.Fatal(err)
}

empty := EvalPathResult{}

if res == empty {
t.Fatal("expected result, got nil")
}

if val, ok := res.Value.(bool); !ok || val != true {
t.Fatalf("expected true, got false")
}
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ func (l *LanguageServer) StartCommandWorker(ctx context.Context) {

err = l.conn.Call(ctx, "regal/showEvalResult", responseParams, &responseResult)
if err != nil {
l.logError(fmt.Errorf("failed %s notify: %v", "regal/hello", err.Error()))
l.logError(fmt.Errorf("regal/showEvalResult failed: %v", err.Error()))
}
} else {
output := filepath.Join(workspacePath, "output.json")
Expand Down