Skip to content

Commit

Permalink
feat(debug): add debug flag and docs for dumping vet rule variables (#…
Browse files Browse the repository at this point in the history
…2521)

* feat(debug): add debug flag and docs for dumping vet rule variables

* feat(debug): dump vet rule env as JSON
  • Loading branch information
andrewmbenton committed Jul 25, 2023
1 parent e3c657d commit 10e51f8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/reference/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ return an error.

`SQLCDEBUG=processplugins=0`

### dumpvetenv

The `dumpvetenv` command prints the variables available to a `sqlc vet` rule
during evaluation.

`SQLCDEBUG=dumpvetenv=1`

### dumpexplain

The `dumpexplain` command prints the JSON-formatted result from running
Expand Down
5 changes: 5 additions & 0 deletions internal/cmd/vet.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,11 @@ func (c *checker) checkSQL(ctx context.Context, s config.SQL) error {
evalMap["mysql"] = engineOutput.MySQL
}

if debug.Debug.DumpVetEnv {
fmt.Printf("vars for rule '%s' evaluating against query '%s':\n", name, query.Name)
debug.DumpAsJSON(evalMap)
}

out, _, err := (*rule.Program).Eval(evalMap)
if err != nil {
return err
Expand Down
9 changes: 9 additions & 0 deletions internal/debug/dump.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package debug

import (
"encoding/json"
"fmt"
"os"

"github.com/davecgh/go-spew/spew"
Expand All @@ -23,3 +25,10 @@ func Dump(n ...interface{}) {
spew.Dump(n)
}
}

func DumpAsJSON(a any) {
if Active {
out, _ := json.MarshalIndent(a, "", " ")
fmt.Printf("%s\n", out)
}
}
5 changes: 5 additions & 0 deletions internal/opts/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
// dumpcatalog: setting dumpcatalog=1 will print the parsed database schema
// trace: setting trace=<path> will output a trace
// processplugins: setting processplugins=0 will disable process-based plugins
// dumpvetenv: setting dumpvetenv=1 will print the variables available to
// a vet rule during evaluation
// dumpexplain: setting dumpexplain=1 will print the JSON-formatted output
// from executing EXPLAIN ... on a query during vet rule evaluation

Expand All @@ -20,6 +22,7 @@ type Debug struct {
DumpCatalog bool
Trace string
ProcessPlugins bool
DumpVetEnv bool
DumpExplain bool
}

Expand Down Expand Up @@ -50,6 +53,8 @@ func DebugFromString(val string) Debug {
}
case pair == "processplugins=0":
d.ProcessPlugins = false
case pair == "dumpvetenv=1":
d.DumpVetEnv = true
case pair == "dumpexplain=1":
d.DumpExplain = true
}
Expand Down

0 comments on commit 10e51f8

Please sign in to comment.