Skip to content

Commit

Permalink
Fix all linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dlsniper committed Apr 13, 2024
1 parent 72589fb commit a83c173
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 57 deletions.
1 change: 0 additions & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@
// Additions include scanning into structs, named query support, rebinding
// queries for different drivers, convenient shorthands for common error handling
// and more.
//
package sqlx
2 changes: 1 addition & 1 deletion named.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func bindArgs(names []string, arg interface{}, m *reflectx.Mapper) ([]interface{
arglist := make([]interface{}, 0, len(names))

// grab the indirected value of arg
v := reflect.ValueOf(arg)
var v reflect.Value
for v = reflect.ValueOf(arg); v.Kind() == reflect.Ptr; {
v = v.Elem()
}
Expand Down
1 change: 1 addition & 0 deletions named_context.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build go1.8
// +build go1.8

package sqlx
Expand Down
5 changes: 3 additions & 2 deletions named_context_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build go1.8
// +build go1.8

package sqlx
Expand All @@ -18,12 +19,12 @@ func TestNamedContextQueries(t *testing.T) {
ctx := context.Background()

// Check that invalid preparations fail
ns, err = db.PrepareNamedContext(ctx, "SELECT * FROM person WHERE first_name=:first:name")
_, err = db.PrepareNamedContext(ctx, "SELECT * FROM person WHERE first_name=:first:name")
if err == nil {
t.Error("Expected an error with invalid prepared statement.")
}

ns, err = db.PrepareNamedContext(ctx, "invalid sql")
_, err = db.PrepareNamedContext(ctx, "invalid sql")
if err == nil {
t.Error("Expected an error with invalid prepared statement.")
}
Expand Down
4 changes: 2 additions & 2 deletions named_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ func TestNamedQueries(t *testing.T) {
var err error

// Check that invalid preparations fail
ns, err = db.PrepareNamed("SELECT * FROM person WHERE first_name=:first:name")
_, err = db.PrepareNamed("SELECT * FROM person WHERE first_name=:first:name")
if err == nil {
t.Error("Expected an error with invalid prepared statement.")
}

ns, err = db.PrepareNamed("invalid sql")
_, err = db.PrepareNamed("invalid sql")
if err == nil {
t.Error("Expected an error with invalid prepared statement.")
}
Expand Down
1 change: 0 additions & 1 deletion reflectx/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// allows for Go-compatible named attribute access, including accessing embedded
// struct attributes and the ability to use functions and struct tags to
// customize field names.
//
package reflectx

import (
Expand Down
17 changes: 8 additions & 9 deletions reflectx/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ func TestFieldsEmbedded(t *testing.T) {

fi = fields.GetByPath("person.name")
if fi == nil {
t.Errorf("Expecting person.name to exist")
t.Fatal("Expecting person.name to exist")
}
if fi.Path != "person.name" {
t.Errorf("Expecting %s, got %s", "person.name", fi.Path)
Expand All @@ -365,15 +365,15 @@ func TestFieldsEmbedded(t *testing.T) {

fi = fields.GetByTraversal([]int{1, 0})
if fi == nil {
t.Errorf("Expecting traveral to exist")
t.Fatal("Expecting traversal to exist")
}
if fi.Path != "name" {
t.Errorf("Expecting %s, got %s", "name", fi.Path)
}

fi = fields.GetByTraversal([]int{2})
if fi == nil {
t.Errorf("Expecting traversal to exist")
t.Fatal("Expecting traversal to exist")
}
if _, ok := fi.Options["required"]; !ok {
t.Errorf("Expecting required option to be set")
Expand Down Expand Up @@ -642,7 +642,6 @@ func TestMapperMethodsByName(t *testing.T) {
A0 *B `db:"A0"`
B `db:"A1"`
A2 int
a3 int
}

val := &A{
Expand Down Expand Up @@ -847,22 +846,22 @@ func TestMustBe(t *testing.T) {
valueErr, ok := r.(*reflect.ValueError)
if !ok {
t.Errorf("unexpected Method: %s", valueErr.Method)
t.Error("expected panic with *reflect.ValueError")
return
t.Fatal("expected panic with *reflect.ValueError")
}
if valueErr.Method != "github.com/jmoiron/sqlx/reflectx.TestMustBe" {
t.Fatalf("unexpected Method: %s", valueErr.Method)
}
if valueErr.Kind != reflect.String {
t.Errorf("unexpected Kind: %s", valueErr.Kind)
t.Fatalf("unexpected Kind: %s", valueErr.Kind)
}
} else {
t.Error("expected panic")
t.Fatal("expected panic")
}
}()

typ = reflect.TypeOf("string")
mustBe(typ, reflect.Struct)
t.Error("got here, didn't expect to")
t.Fatal("got here, didn't expect to")
}

type E1 struct {
Expand Down
21 changes: 12 additions & 9 deletions sqlx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"database/sql/driver"
"errors"
"fmt"

"io/ioutil"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -51,9 +50,9 @@ func mapper() *reflectx.Mapper {

// isScannable takes the reflect.Type and the actual dest value and returns
// whether or not it's Scannable. Something is scannable if:
// * it is not a struct
// * it implements sql.Scanner
// * it has no exported fields
// - it is not a struct
// - it implements sql.Scanner
// - it has no exported fields
func isScannable(t reflect.Type) bool {
if reflect.PtrTo(t).Implements(_scannerInterface) {
return true
Expand Down Expand Up @@ -160,6 +159,8 @@ func mapperFor(i interface{}) *reflectx.Mapper {
}

var _scannerInterface = reflect.TypeOf((*sql.Scanner)(nil)).Elem()

//lint:ignore U1000 ignoring this for now
var _valuerInterface = reflect.TypeOf((*driver.Valuer)(nil)).Elem()

// Row is a reimplementation of sql.Row in order to gain access to the underlying
Expand Down Expand Up @@ -248,6 +249,8 @@ type DB struct {

// NewDb returns a new sqlx DB wrapper for a pre-existing *sql.DB. The
// driverName of the original database is required for named query support.
//
//lint:ignore ST1003 changing this would break the package interface.
func NewDb(db *sql.DB, driverName string) *DB {
return &DB{DB: db, driverName: driverName, Mapper: mapper()}
}
Expand Down Expand Up @@ -884,9 +887,9 @@ func structOnlyError(t reflect.Type) error {
// then each row must only have one column which can scan into that type. This
// allows you to do something like:
//
// rows, _ := db.Query("select id from people;")
// var ids []int
// scanAll(rows, &ids, false)
// rows, _ := db.Query("select id from people;")
// var ids []int
// scanAll(rows, &ids, false)
//
// and ids will be a list of the id results. I realize that this is a desirable
// interface to expose to users, but for now it will only be exposed via changes
Expand Down Expand Up @@ -935,9 +938,9 @@ func scanAll(rows rowsi, dest interface{}, structOnly bool) error {
var values []interface{}
var m *reflectx.Mapper

switch rows.(type) {
switch rows := rows.(type) {
case *Rows:
m = rows.(*Rows).Mapper
m = rows.Mapper
default:
m = mapper()
}
Expand Down
1 change: 1 addition & 0 deletions sqlx_context.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build go1.8
// +build go1.8

package sqlx
Expand Down
27 changes: 14 additions & 13 deletions sqlx_context_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
//go:build go1.8
// +build go1.8

// The following environment variables, if set, will be used:
//
// * SQLX_SQLITE_DSN
// * SQLX_POSTGRES_DSN
// * SQLX_MYSQL_DSN
// - SQLX_SQLITE_DSN
// - SQLX_POSTGRES_DSN
// - SQLX_MYSQL_DSN
//
// Set any of these variables to 'skip' to skip them. Note that for MySQL,
// the string '?parseTime=True' will be appended to the DSN if it's not there
// already.
//
package sqlx

import (
Expand All @@ -23,9 +23,10 @@ import (
"time"

_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx/reflectx"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"

"github.com/jmoiron/sqlx/reflectx"
)

func MultiExecContext(ctx context.Context, e ExecerContext, query string) {
Expand Down Expand Up @@ -92,7 +93,7 @@ func TestMissingNamesContextContext(t *testing.T) {
FirstName string `db:"first_name"`
LastName string `db:"last_name"`
Email string
//AddedAt time.Time `db:"added_at"`
// AddedAt time.Time `db:"added_at"`
}

// test Select first
Expand Down Expand Up @@ -485,7 +486,7 @@ func TestNamedQueryContext(t *testing.T) {
// these are tests for #73; they verify that named queries work if you've
// changed the db mapper. This code checks both NamedQuery "ad-hoc" style
// queries and NamedStmt queries, which use different code paths internally.
old := *db.Mapper
old := (*db).Mapper

type JSONPerson struct {
FirstName sql.NullString `json:"FIRST"`
Expand Down Expand Up @@ -570,7 +571,7 @@ func TestNamedQueryContext(t *testing.T) {

check(t, rows)

db.Mapper = &old
db.Mapper = old

// Test nested structs
type Place struct {
Expand Down Expand Up @@ -831,7 +832,7 @@ func TestUsageContext(t *testing.T) {
if err != nil {
t.Error(err)
}
//fmt.Printf("%#v\n%#v\n%#v\n", placesptr[0], placesptr[1], placesptr[2])
// fmt.Printf("%#v\n%#v\n%#v\n", placesptr[0], placesptr[1], placesptr[2])

// if you have null fields and use SELECT *, you must use sql.Null* in your struct
// this test also verifies that you can use either a []Struct{} or a []*Struct{}
Expand Down Expand Up @@ -1274,9 +1275,9 @@ func TestInContext(t *testing.T) {
}
RunWithSchemaContext(context.Background(), defaultSchema, t, func(ctx context.Context, db *DB, t *testing.T) {
loadDefaultFixtureContext(ctx, db, t)
//tx.MustExecContext(ctx, tx.Rebind("INSERT INTO place (country, city, telcode) VALUES (?, ?, ?)"), "United States", "New York", "1")
//tx.MustExecContext(ctx, tx.Rebind("INSERT INTO place (country, telcode) VALUES (?, ?)"), "Hong Kong", "852")
//tx.MustExecContext(ctx, tx.Rebind("INSERT INTO place (country, telcode) VALUES (?, ?)"), "Singapore", "65")
// tx.MustExecContext(ctx, tx.Rebind("INSERT INTO place (country, city, telcode) VALUES (?, ?, ?)"), "United States", "New York", "1")
// tx.MustExecContext(ctx, tx.Rebind("INSERT INTO place (country, telcode) VALUES (?, ?)"), "Hong Kong", "852")
// tx.MustExecContext(ctx, tx.Rebind("INSERT INTO place (country, telcode) VALUES (?, ?)"), "Singapore", "65")
telcodes := []int{852, 65}
q := "SELECT * FROM place WHERE telcode IN(?) ORDER BY telcode"
query, args, err := In(q, telcodes)
Expand Down Expand Up @@ -1355,7 +1356,7 @@ func TestConn(t *testing.T) {

RunWithSchemaContext(context.Background(), schema, t, func(ctx context.Context, db *DB, t *testing.T) {
conn, err := db.Connx(ctx)
defer conn.Close()
defer conn.Close() //lint:ignore SA5001 it's OK to ignore this here.
if err != nil {
t.Fatal(err)
}
Expand Down
33 changes: 16 additions & 17 deletions sqlx_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// The following environment variables, if set, will be used:
//
// * SQLX_SQLITE_DSN
// * SQLX_POSTGRES_DSN
// * SQLX_MYSQL_DSN
// - SQLX_SQLITE_DSN
// - SQLX_POSTGRES_DSN
// - SQLX_MYSQL_DSN
//
// Set any of these variables to 'skip' to skip them. Note that for MySQL,
// the string '?parseTime=True' will be appended to the DSN if it's not there
// already.
//
package sqlx

import (
Expand All @@ -23,9 +22,10 @@ import (
"time"

_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx/reflectx"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"

"github.com/jmoiron/sqlx/reflectx"
)

/* compile time checks that Db, Tx, Stmt (qStmt) implement expected interfaces */
Expand All @@ -41,7 +41,6 @@ var TestMysql = true
var sldb *DB
var pgdb *DB
var mysqldb *DB
var active = []*DB{}

func init() {
ConnectAll()
Expand Down Expand Up @@ -269,7 +268,7 @@ func TestMissingNames(t *testing.T) {
FirstName string `db:"first_name"`
LastName string `db:"last_name"`
Email string
//AddedAt time.Time `db:"added_at"`
// AddedAt time.Time `db:"added_at"`
}

// test Select first
Expand Down Expand Up @@ -659,7 +658,7 @@ func TestNamedQuery(t *testing.T) {
// these are tests for #73; they verify that named queries work if you've
// changed the db mapper. This code checks both NamedQuery "ad-hoc" style
// queries and NamedStmt queries, which use different code paths internally.
old := *db.Mapper
old := (*db).Mapper

type JSONPerson struct {
FirstName sql.NullString `json:"FIRST"`
Expand Down Expand Up @@ -744,7 +743,7 @@ func TestNamedQuery(t *testing.T) {

check(t, rows)

db.Mapper = &old
db.Mapper = old

// Test nested structs
type Place struct {
Expand Down Expand Up @@ -1016,7 +1015,7 @@ func TestUsage(t *testing.T) {
if err != nil {
t.Error(err)
}
//fmt.Printf("%#v\n%#v\n%#v\n", placesptr[0], placesptr[1], placesptr[2])
// fmt.Printf("%#v\n%#v\n%#v\n", placesptr[0], placesptr[1], placesptr[2])

// if you have null fields and use SELECT *, you must use sql.Null* in your struct
// this test also verifies that you can use either a []Struct{} or a []*Struct{}
Expand Down Expand Up @@ -1578,9 +1577,9 @@ func TestIn(t *testing.T) {
}
RunWithSchema(defaultSchema, t, func(db *DB, t *testing.T, now string) {
loadDefaultFixture(db, t)
//tx.MustExec(tx.Rebind("INSERT INTO place (country, city, telcode) VALUES (?, ?, ?)"), "United States", "New York", "1")
//tx.MustExec(tx.Rebind("INSERT INTO place (country, telcode) VALUES (?, ?)"), "Hong Kong", "852")
//tx.MustExec(tx.Rebind("INSERT INTO place (country, telcode) VALUES (?, ?)"), "Singapore", "65")
// tx.MustExec(tx.Rebind("INSERT INTO place (country, city, telcode) VALUES (?, ?, ?)"), "United States", "New York", "1")
// tx.MustExec(tx.Rebind("INSERT INTO place (country, telcode) VALUES (?, ?)"), "Hong Kong", "852")
// tx.MustExec(tx.Rebind("INSERT INTO place (country, telcode) VALUES (?, ?)"), "Singapore", "65")
telcodes := []int{852, 65}
q := "SELECT * FROM place WHERE telcode IN(?) ORDER BY telcode"
query, args, err := In(q, telcodes)
Expand Down Expand Up @@ -1864,11 +1863,11 @@ func TestIn130Regression(t *testing.T) {
}
t.Log(args)
for _, a := range args {
switch a.(type) {
switch a := a.(type) {
case string:
t.Log("ok: string", a)
case *string:
t.Error("ng: string pointer", a, *a.(*string))
t.Error("ng: string pointer", a, *a)
}
}
})
Expand All @@ -1883,11 +1882,11 @@ func TestIn130Regression(t *testing.T) {
}
t.Log(args)
for _, a := range args {
switch a.(type) {
switch a := a.(type) {
case string:
t.Log("ok: string", a)
case *string:
t.Error("ng: string pointer", a, *a.(*string))
t.Error("ng: string pointer", a, *a)
}
}
})
Expand Down
Loading

0 comments on commit a83c173

Please sign in to comment.