Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Relocated constants for query flags to a dedicated package to allow f…
…or safer reuse.
  • Loading branch information
rhodeon authored and kyleconroy committed Nov 25, 2024
commit 5c1546d07a55b6665349496138698d89a79fdf7f
8 changes: 3 additions & 5 deletions internal/cmd/vet.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/sqlc-dev/sqlc/internal/constants"
"io"
"log"
"os"
Expand Down Expand Up @@ -38,9 +39,6 @@ var ErrFailedChecks = errors.New("failed checks")

var pjson = protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}

const RuleDbPrepare = "sqlc/db-prepare"
const QueryFlagSqlcVetDisable = "@sqlc-vet-disable"

func NewCmdVet() *cobra.Command {
return &cobra.Command{
Use: "vet",
Expand Down Expand Up @@ -110,7 +108,7 @@ func Vet(ctx context.Context, dir, filename string, opts *Options) error {
}

rules := map[string]rule{
RuleDbPrepare: {NeedsPrepare: true},
constants.QueryRuleDbPrepare: {NeedsPrepare: true},
}

for _, c := range conf.Rules {
Expand Down Expand Up @@ -540,7 +538,7 @@ func (c *checker) checkSQL(ctx context.Context, s config.SQL) error {
cfg := vetConfig(req)
for i, query := range req.Queries {
md := result.Queries[i].Metadata
if md.Flags[QueryFlagSqlcVetDisable] {
if md.Flags[constants.QueryFlagSqlcVetDisable] {
// If the vet disable flag is specified without any rules listed, all rules are ignored.
if len(md.RuleSkiplist) == 0 {
if debug.Active {
Expand Down
12 changes: 12 additions & 0 deletions internal/constants/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package constants

// Flags
const (
QueryFlagParam = "@param"
QueryFlagSqlcVetDisable = "@sqlc-vet-disable"
)

// Rules
const (
QueryRuleDbPrepare = "sqlc/db-prepare"
)
5 changes: 3 additions & 2 deletions internal/metadata/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package metadata
import (
"bufio"
"fmt"
"github.com/sqlc-dev/sqlc/internal/constants"
"strings"
"unicode"

Expand Down Expand Up @@ -134,7 +135,7 @@ func ParseParamsAndFlags(comments []string) (map[string]string, map[string]bool,
}

switch token {
case "@param":
case constants.QueryFlagParam:
s.Scan()
name := s.Text()
var rest []string
Expand All @@ -144,7 +145,7 @@ func ParseParamsAndFlags(comments []string) (map[string]string, map[string]bool,
}
params[name] = strings.Join(rest, " ")

case "@sqlc-vet-disable":
case constants.QueryFlagSqlcVetDisable:
flags[token] = true

// Vet rules can all be disabled in the same line or split across lines .i.e.
Expand Down