Skip to content

Commit 408f111

Browse files
committed
Cleanup code
1 parent 04425ad commit 408f111

6 files changed

Lines changed: 68 additions & 35 deletions

File tree

.golangci.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
version: "2"
2+
linters:
3+
default: all
4+
disable:
5+
- cyclop
6+
- depguard
7+
- err113
8+
- errcheck
9+
- exhaustruct
10+
- funlen
11+
- gochecknoglobals
12+
- gochecknoinits
13+
- gocognit
14+
- goconst
15+
- lll
16+
- maintidx
17+
- mnd
18+
- nestif
19+
- nilnil
20+
- nlreturn
21+
- paralleltest
22+
- prealloc
23+
- sqlclosecheck
24+
- testableexamples
25+
- testpackage
26+
- varnamelen
27+
- wrapcheck
28+
- wsl
29+
settings:
30+
gosec:
31+
excludes:
32+
- G101
33+
- G304
34+
- G390
35+
gocritic:
36+
disabled-checks:
37+
- exitAfterDefer

dburl.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ func Parse(urlstr string) (*URL, error) {
128128
// determine scheme for file
129129
s := u.opaqueOrPath()
130130
switch {
131-
case u.Transport != "tcp", strings.Index(u.OriginalScheme, "+") != -1:
131+
case u.Transport != "tcp",
132+
strings.Contains(u.OriginalScheme, "+"):
132133
return nil, ErrInvalidTransportProtocol
133134
case s == "":
134135
return nil, ErrMissingPath
@@ -386,11 +387,7 @@ var Stat = func(name string) (fs.FileInfo, error) {
386387
//
387388
// Used internally to read file headers.
388389
var OpenFile = func(name string) (fs.File, error) {
389-
f, err := os.OpenFile(name, os.O_RDONLY, 0)
390-
if err != nil {
391-
return nil, err
392-
}
393-
return f, nil
390+
return os.OpenFile(name, os.O_RDONLY, 0)
394391
}
395392

396393
// BuildURL creates a dsn using the mapped components.
@@ -556,7 +553,6 @@ func getComponent(m map[string]any, v ...string) (string, bool) {
556553
if z, ok := getFirst(m, v...); ok {
557554
str := fmt.Sprintf("%v", z)
558555
return str, str != ""
559-
560556
}
561557
return "", false
562558
}

dburl_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ func TestBadParse(t *testing.T) {
5757
{`databend://`, ErrMissingHost},
5858
{`unknown_file.ext3`, ErrInvalidDatabaseScheme},
5959
}
60-
for i, tt := range tests {
61-
test := tt
60+
for i, test := range tests {
6261
t.Run(strconv.Itoa(i), func(t *testing.T) {
6362
testBadParse(t, test.s, test.exp)
6463
})
@@ -986,8 +985,7 @@ func TestParse(t *testing.T) {
986985
},
987986
}
988987
m := make(map[string]bool)
989-
for i, tt := range tests {
990-
test := tt
988+
for i, test := range tests {
991989
t.Run(strconv.Itoa(i), func(t *testing.T) {
992990
if _, ok := m[test.s]; ok {
993991
t.Fatalf("%s is already tested", test.s)

dsn.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
// OdbcIgnoreQueryPrefixes are the query prefixes to ignore when generating the
12-
// odbc DSN. Used by GenOdbc
12+
// odbc DSN. Used by [GenOdbc].
1313
var OdbcIgnoreQueryPrefixes []string
1414

1515
// GenScheme returns a generator that will generate a scheme based on the
@@ -427,7 +427,7 @@ func GenOdbc(u *URL) (string, string, error) {
427427
}
428428
// build q
429429
q := u.Query()
430-
q.Set("Driver", "{"+strings.Replace(u.Transport, "+", " ", -1)+"}")
430+
q.Set("Driver", "{"+strings.ReplaceAll(u.Transport, "+", " ")+"}")
431431
q.Set("Server", host)
432432
if port == "" {
433433
proto := strings.ToLower(u.Transport)
@@ -458,7 +458,7 @@ func GenOdbc(u *URL) (string, string, error) {
458458
func GenOleodbc(u *URL) (string, string, error) {
459459
props, _, err := GenOdbc(u)
460460
if err != nil {
461-
return "", "", nil
461+
return "", "", err
462462
}
463463
return `Provider=MSDASQL.1;Extended Properties="` + props + `"`, "", nil
464464
}
@@ -518,9 +518,10 @@ func GenPresto(u *URL) (string, string, error) {
518518
}
519519
// force port
520520
if z.Port() == "" {
521-
if z.Scheme == "http" {
521+
switch z.Scheme {
522+
case "http":
522523
z.Host += ":8080"
523-
} else if z.Scheme == "https" {
524+
case "https":
524525
z.Host += ":8443"
525526
}
526527
}
@@ -562,16 +563,12 @@ func GenSnowflake(u *URL) (string, string, error) {
562563

563564
// GenSpanner generates a spanner DSN from the passed URL.
564565
func GenSpanner(u *URL) (string, string, error) {
565-
project, instance, dbname := u.Hostname(), "", strings.TrimPrefix(u.Path, "/")
566+
project := u.Hostname()
566567
if project == "" {
567568
return "", "", ErrMissingHost
568569
}
569-
i := strings.Index(dbname, "/")
570-
if i == -1 {
571-
return "", "", ErrMissingPath
572-
}
573-
instance, dbname = dbname[:i], dbname[i+1:]
574-
if instance == "" || dbname == "" {
570+
instance, dbname, ok := strings.Cut(strings.TrimPrefix(u.Path, "/"), "/")
571+
if !ok || instance == "" || dbname == "" {
575572
return "", "", ErrMissingPath
576573
}
577574
return fmt.Sprintf(`projects/%s/instances/%s/databases/%s`, project, instance, dbname), "", nil
@@ -608,13 +605,14 @@ func GenSqlserver(u *URL) (string, string, error) {
608605
func GenTableStore(u *URL) (string, string, error) {
609606
var transport string
610607
splits := strings.Split(u.OriginalScheme, "+")
611-
if len(splits) == 0 {
608+
switch {
609+
case len(splits) == 0:
612610
return "", "", ErrInvalidDatabaseScheme
613-
} else if len(splits) == 1 || splits[1] == "https" {
611+
case len(splits) == 1, splits[1] == "https":
614612
transport = "https"
615-
} else if splits[1] == "http" {
613+
case splits[1] == "http":
616614
transport = "http"
617-
} else {
615+
default:
618616
return "", "", ErrInvalidTransportProtocol
619617
}
620618
z := &url.URL{
@@ -708,6 +706,8 @@ func genOptionsOdbc(q url.Values, skipWhenEmpty bool, ignore, ignorePrefixes []s
708706
// following:
709707
//
710708
// genOptions(u.Query(), "", "=", ";", ",", false)
709+
//
710+
//nolint:unparam
711711
func genOptions(q url.Values, joiner, assign, sep, valSep string, skipWhenEmpty bool, ignore, ignorePrefixes []string) string {
712712
if len(q) == 0 {
713713
return ""

example_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ func Example() {
1212
if err != nil {
1313
log.Fatal(err)
1414
}
15+
defer db.Close()
1516
res, err := db.Query("SELECT ...")
1617
if err != nil {
1718
log.Fatal(err)
@@ -33,6 +34,7 @@ func Example_parse() {
3334
if err != nil {
3435
log.Fatal(err)
3536
}
37+
defer db.Close()
3638
res, err := db.Query("SELECT ...")
3739
if err != nil {
3840
log.Fatal(err)

passfile/passfile.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ func Parse(r io.Reader) ([]Entry, error) {
5353
// split and check length
5454
v := strings.Split(line, ":")
5555
if len(v) != 6 {
56-
return nil, &ErrInvalidEntry{i}
56+
return nil, &InvalidEntryError{i}
5757
}
5858
// make sure no blank entries exist
5959
for j := range v {
6060
if v[j] == "" {
61-
return nil, &ErrEmptyField{i, j}
61+
return nil, &EmptyFieldError{i, j}
6262
}
6363
}
6464
entries = append(entries, NewEntry(v))
@@ -260,23 +260,23 @@ func (err *FileError) Unwrap() error {
260260
return err.Err
261261
}
262262

263-
// ErrInvalidEntry is the invalid entry error.
264-
type ErrInvalidEntry struct {
263+
// InvalidEntryError is the invalid entry error.
264+
type InvalidEntryError struct {
265265
Line int
266266
}
267267

268268
// Error satisfies the error interface.
269-
func (err *ErrInvalidEntry) Error() string {
269+
func (err *InvalidEntryError) Error() string {
270270
return fmt.Sprintf("invalid entry at line %d", err.Line)
271271
}
272272

273-
// ErrEmptyField is the empty field error.
274-
type ErrEmptyField struct {
273+
// EmptyFieldError is the empty field error.
274+
type EmptyFieldError struct {
275275
Line int
276276
Field int
277277
}
278278

279279
// Error satisfies the error interface.
280-
func (err *ErrEmptyField) Error() string {
280+
func (err *EmptyFieldError) Error() string {
281281
return fmt.Sprintf("line %d has empty field %d", err.Line, err.Field)
282282
}

0 commit comments

Comments
 (0)