Skip to content

Commit 1ca4dca

Browse files
committed
Relocated constants for query flags to a dedicated package to allow for safer reuse.
1 parent 1213fe2 commit 1ca4dca

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

internal/cmd/vet.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9+
"github.com/sqlc-dev/sqlc/internal/constants"
910
"io"
1011
"log"
1112
"os"
@@ -38,9 +39,6 @@ var ErrFailedChecks = errors.New("failed checks")
3839

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

41-
const RuleDbPrepare = "sqlc/db-prepare"
42-
const QueryFlagSqlcVetDisable = "@sqlc-vet-disable"
43-
4442
func NewCmdVet() *cobra.Command {
4543
return &cobra.Command{
4644
Use: "vet",
@@ -110,7 +108,7 @@ func Vet(ctx context.Context, dir, filename string, opts *Options) error {
110108
}
111109

112110
rules := map[string]rule{
113-
RuleDbPrepare: {NeedsPrepare: true},
111+
constants.QueryRuleDbPrepare: {NeedsPrepare: true},
114112
}
115113

116114
for _, c := range conf.Rules {
@@ -540,7 +538,7 @@ func (c *checker) checkSQL(ctx context.Context, s config.SQL) error {
540538
cfg := vetConfig(req)
541539
for i, query := range req.Queries {
542540
md := result.Queries[i].Metadata
543-
if md.Flags[QueryFlagSqlcVetDisable] {
541+
if md.Flags[constants.QueryFlagSqlcVetDisable] {
544542
// If the vet disable flag is specified without any rules listed, all rules are ignored.
545543
if len(md.RuleSkiplist) == 0 {
546544
if debug.Active {

internal/constants/query.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package constants
2+
3+
// Flags
4+
const (
5+
QueryFlagParam = "@param"
6+
QueryFlagSqlcVetDisable = "@sqlc-vet-disable"
7+
)
8+
9+
// Rules
10+
const (
11+
QueryRuleDbPrepare = "sqlc/db-prepare"
12+
)

internal/metadata/meta.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package metadata
33
import (
44
"bufio"
55
"fmt"
6+
"github.com/sqlc-dev/sqlc/internal/constants"
67
"strings"
78
"unicode"
89

@@ -134,7 +135,7 @@ func ParseParamsAndFlags(comments []string) (map[string]string, map[string]bool,
134135
}
135136

136137
switch token {
137-
case "@param":
138+
case constants.QueryFlagParam:
138139
s.Scan()
139140
name := s.Text()
140141
var rest []string
@@ -144,7 +145,7 @@ func ParseParamsAndFlags(comments []string) (map[string]string, map[string]bool,
144145
}
145146
params[name] = strings.Join(rest, " ")
146147

147-
case "@sqlc-vet-disable":
148+
case constants.QueryFlagSqlcVetDisable:
148149
flags[token] = true
149150

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

0 commit comments

Comments
 (0)