Skip to content

Commit

Permalink
Fix gometalinter issues (#117)
Browse files Browse the repository at this point in the history
* Add gometalinter check

* Fix some linter issues

* More linter fixes

* More lint fixes

* More lint fixes

* Deprecate some stuff to fix linter issues

* Add more docs

* Add more doc & fixes

* Comment gometalinter for Travis build

* Most of the issues are fixed, keeping the remaining for after.
  • Loading branch information
stanislas-m authored Jun 11, 2018
1 parent c1671b2 commit 232efba
Show file tree
Hide file tree
Showing 70 changed files with 739 additions and 506 deletions.
4 changes: 4 additions & 0 deletions .gometalinter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"Enable": ["vet", "golint", "goimports", "deadcode", "gotype", "ineffassign", "misspell", "nakedret", "unconvert", "megacheck", "varcheck"]
}

26 changes: 15 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@ go:
- tip

install:
- go get -t -v ./...
- go get -t -v ./...

before_script:
- go build -v -tags sqlite -o tsoda ./soda
# travis hangs when trying to create mysql db using soda. not sure why
- mysql -e 'create database pop_test;'
- ./tsoda create -e "postgres"
- ./tsoda create -e "sqlite"
- ./tsoda migrate -e "mysql_travis"
- ./tsoda migrate -e "postgres"
- ./tsoda migrate -e "sqlite"

script: go test -tags sqlite ./... -v
- go get -u github.com/alecthomas/gometalinter
- gometalinter --install
- go build -v -tags sqlite -o tsoda ./soda
# travis hangs when trying to create mysql db using soda. not sure why
- mysql -e 'create database pop_test;'
- ./tsoda create -e "postgres"
- ./tsoda create -e "sqlite"
- ./tsoda migrate -e "mysql_travis"
- ./tsoda migrate -e "postgres"
- ./tsoda migrate -e "sqlite"

script:
# - gometalinter --vendor --deadline=5m ./...
- go test -tags sqlite ./... -v

global_env:
- MYSQL_USER="travis"
Expand Down
8 changes: 6 additions & 2 deletions associations/association.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,22 @@ type InnerAssociation struct {
// InnerAssociations is a group of InnerAssociation.
type InnerAssociations []InnerAssociation

// AssociationSortable a type to be sortable.
// AssociationSortable allows a type to be sortable.
type AssociationSortable interface {
OrderBy() string
Association
}

// AssociationBeforeCreatable allows an association to be created before
// the parent structure.
type AssociationBeforeCreatable interface {
BeforeInterface() interface{}
BeforeSetup() error
Association
}

// AssociationAfterCreatable allows an association to be created after
// the parent structure.
type AssociationAfterCreatable interface {
AfterInterface() interface{}
AfterSetup() error
Expand Down Expand Up @@ -120,7 +124,7 @@ func (a Associations) AssociationsCreatableStatement() []AssociationCreatableSta
return stm
}

// associationParams a wrapper for associations definition
// associationParams is a wrapper for associations definition
// and creation.
type associationParams struct {
field reflect.StructField // an association field defined in model.
Expand Down
11 changes: 11 additions & 0 deletions associations/associations_for_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,18 @@ var associationBuilders = map[string]associationBuilder{}
// associations like has_many, belongs_to, has_one.
// it throws an error when it finds a field that does
// not exist for a model.
//
// Deprecated: use ForStruct instead.
func AssociationsForStruct(s interface{}, fields ...string) (Associations, error) {
return ForStruct(s, fields...)
}

// ForStruct returns all associations for
// the struct specified. It takes into account tags
// associations like has_many, belongs_to, has_one.
// it throws an error when it finds a field that does
// not exist for a model.
func ForStruct(s interface{}, fields ...string) (Associations, error) {
associations := Associations{}
innerAssociations := InnerAssociations{}

Expand Down
2 changes: 1 addition & 1 deletion associations/belongs_to_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Test_Belongs_To_Association(t *testing.T) {
id, _ := uuid.NewV1()
bar := barBelongsTo{FooID: id}

as, err := associations.AssociationsForStruct(&bar, "Foo")
as, err := associations.ForStruct(&bar, "Foo")
a.NoError(err)
a.Equal(len(as), 1)
a.Equal(reflect.Struct, as[0].Kind())
Expand Down
4 changes: 2 additions & 2 deletions associations/has_many_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Test_Has_Many_Association(t *testing.T) {
id := 1
foo := FooHasMany{ID: 1}

as, err := associations.AssociationsForStruct(&foo)
as, err := associations.ForStruct(&foo)

a.NoError(err)
a.Equal(len(as), 1)
Expand All @@ -42,7 +42,7 @@ func Test_Has_Many_SetValue(t *testing.T) {
a := require.New(t)
foo := FooHasMany{ID: 1, BarHasManies: &barHasManies{{Title: "bar"}}}

as, _ := associations.AssociationsForStruct(&foo)
as, _ := associations.ForStruct(&foo)
a.Equal(len(as), 1)

ca, ok := as[0].(associations.AssociationAfterCreatable)
Expand Down
4 changes: 2 additions & 2 deletions associations/has_one_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Test_Has_One_Association(t *testing.T) {
id, _ := uuid.NewV1()
foo := FooHasOne{ID: id}

as, err := associations.AssociationsForStruct(&foo)
as, err := associations.ForStruct(&foo)

a.NoError(err)
a.Equal(len(as), 1)
Expand All @@ -42,7 +42,7 @@ func Test_Has_One_SetValue(t *testing.T) {
id, _ := uuid.NewV1()
foo := FooHasOne{ID: id, BarHasOne: barHasOne{Title: "bar"}}

as, _ := associations.AssociationsForStruct(&foo)
as, _ := associations.ForStruct(&foo)
a.Equal(len(as), 1)

ca, ok := as[0].(associations.AssociationAfterCreatable)
Expand Down
2 changes: 1 addition & 1 deletion associations/many_to_many_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Test_Many_To_Many_Association(t *testing.T) {
id, _ := uuid.NewV1()
foo := fooManyToMany{ID: id}

as, err := associations.AssociationsForStruct(&foo)
as, err := associations.ForStruct(&foo)

a.NoError(err)
a.Equal(len(as), 1)
Expand Down
4 changes: 1 addition & 3 deletions clause.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ func (c clauses) Join(sep string) string {

func (c clauses) Args() (args []interface{}) {
for _, clause := range c {
for _, arg := range clause.Arguments {
args = append(args, arg)
}
args = append(args, clause.Arguments...)
}
return
}
Expand Down
2 changes: 2 additions & 0 deletions columns/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package columns

import "fmt"

// Column represents a SQL table column.
type Column struct {
Name string
Writeable bool
Expand All @@ -14,6 +15,7 @@ func (c Column) UpdateString() string {
return fmt.Sprintf("%s = :%s", c.Name, c.Name)
}

// SetSelectSQL sets a custom SELECT statement for the column.
func (c *Column) SetSelectSQL(s string) {
c.SelectSQL = s
c.Writeable = false
Expand Down
2 changes: 1 addition & 1 deletion columns/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (c *Columns) Add(names ...string) []*Column {
ss := ""
//support for distinct xx, or distinct on (field) table.fields
if strings.HasSuffix(name, ",r") || strings.HasSuffix(name, ",w") {
xs = []string{name[0 : len(name)-2], name[len(name)-1 : len(name)]}
xs = []string{name[0 : len(name)-2], name[len(name)-1:]}
} else {
xs = []string{name}
}
Expand Down
25 changes: 21 additions & 4 deletions columns/columns_for_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,29 @@ import (

// ColumnsForStruct returns a Columns instance for
// the struct passed in.

//
// Deprecated: use ForStruct instead.
func ColumnsForStruct(s interface{}, tableName string) (columns Columns) {
return ColumnsForStructWithAlias(s, tableName, "")
return ForStruct(s, tableName)
}

// ColumnsForStructWithAlias returns a Columns instance for the struct passed in.
// If the tableAlias is not empty, it will be used.
//
// Deprecated: use ForStructWithAlias instead.
func ColumnsForStructWithAlias(s interface{}, tableName string, tableAlias string) (columns Columns) {
return ForStructWithAlias(s, tableName, tableAlias)
}

// ForStruct returns a Columns instance for
// the struct passed in.
func ForStruct(s interface{}, tableName string) (columns Columns) {
return ForStructWithAlias(s, tableName, "")
}

// ForStructWithAlias returns a Columns instance for the struct passed in.
// If the tableAlias is not empty, it will be used.
func ForStructWithAlias(s interface{}, tableName string, tableAlias string) (columns Columns) {
columns = NewColumnsWithAlias(tableName, tableAlias)
defer func() {
if r := recover(); r != nil {
Expand Down Expand Up @@ -41,15 +58,15 @@ func ColumnsForStructWithAlias(s interface{}, tableName string, tableAlias strin
if !tag.Ignored() && !tag.Empty() {
col := tag.Value

//add writable or readable.
// add writable or readable.
tag := popTags.Find("rw")
if !tag.Empty() {
col = col + "," + tag.Value
}

cs := columns.Add(col)

//add select clause.
// add select clause.
tag = popTags.Find("select")
if !tag.Empty() {
c := cs[0]
Expand Down
10 changes: 5 additions & 5 deletions columns/columns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ type foos []foo
func Test_Column_MapsSlice(t *testing.T) {
r := require.New(t)

c1 := columns.ColumnsForStruct(&foo{}, "foo")
c2 := columns.ColumnsForStruct(&foos{}, "foo")
c1 := columns.ForStruct(&foo{}, "foo")
c2 := columns.ForStruct(&foos{}, "foo")
r.Equal(c1.String(), c2.String())
}

func Test_Columns_Basics(t *testing.T) {
r := require.New(t)

for _, f := range []interface{}{foo{}, &foo{}} {
c := columns.ColumnsForStruct(f, "foo")
c := columns.ForStruct(f, "foo")
r.Equal(len(c.Cols), 4)
r.Equal(c.Cols["first_name"], &columns.Column{Name: "first_name", Writeable: false, Readable: true, SelectSQL: "first_name as f"})
r.Equal(c.Cols["LastName"], &columns.Column{Name: "LastName", Writeable: true, Readable: true, SelectSQL: "foo.LastName"})
Expand All @@ -42,7 +42,7 @@ func Test_Columns_Add(t *testing.T) {
r := require.New(t)

for _, f := range []interface{}{foo{}, &foo{}} {
c := columns.ColumnsForStruct(f, "foo")
c := columns.ForStruct(f, "foo")
r.Equal(len(c.Cols), 4)
c.Add("foo", "first_name")
r.Equal(len(c.Cols), 5)
Expand All @@ -54,7 +54,7 @@ func Test_Columns_Remove(t *testing.T) {
r := require.New(t)

for _, f := range []interface{}{foo{}, &foo{}} {
c := columns.ColumnsForStruct(f, "foo")
c := columns.ForStruct(f, "foo")
r.Equal(len(c.Cols), 4)
c.Remove("foo", "first_name")
r.Equal(len(c.Cols), 3)
Expand Down
1 change: 1 addition & 0 deletions columns/readable_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
)

// ReadableColumns represents a list of columns Pop is allowed to read.
type ReadableColumns struct {
Columns
}
Expand Down
6 changes: 3 additions & 3 deletions columns/readable_columns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func Test_Columns_ReadableString(t *testing.T) {
r := require.New(t)
for _, f := range []interface{}{foo{}, &foo{}} {
c := columns.ColumnsForStruct(f, "foo")
c := columns.ForStruct(f, "foo")
u := c.Readable().String()
r.Equal(u, "LastName, first_name, read")
}
Expand All @@ -19,7 +19,7 @@ func Test_Columns_ReadableString(t *testing.T) {
func Test_Columns_Readable_SelectString(t *testing.T) {
r := require.New(t)
for _, f := range []interface{}{foo{}, &foo{}} {
c := columns.ColumnsForStruct(f, "foo")
c := columns.ForStruct(f, "foo")
u := c.Readable().SelectString()
r.Equal(u, "first_name as f, foo.LastName, foo.read")
}
Expand All @@ -28,7 +28,7 @@ func Test_Columns_Readable_SelectString(t *testing.T) {
func Test_Columns_ReadableString_Symbolized(t *testing.T) {
r := require.New(t)
for _, f := range []interface{}{foo{}, &foo{}} {
c := columns.ColumnsForStruct(f, "foo")
c := columns.ForStruct(f, "foo")
u := c.Readable().SymbolizedString()
r.Equal(u, ":LastName, :first_name, :read")
}
Expand Down
4 changes: 2 additions & 2 deletions columns/writeable_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"strings"
)

// WriteableColumns represents a list of columns Pop is allowed to write.
type WriteableColumns struct {
Columns
}

// UpdateString returns the SQL column list part of the UPDATE
// query.
// UpdateString returns the SQL column list part of the UPDATE query.
func (c WriteableColumns) UpdateString() string {
xs := []string{}
for _, t := range c.Cols {
Expand Down
6 changes: 3 additions & 3 deletions columns/writeable_columns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func Test_Columns_WriteableString_Symbolized(t *testing.T) {
r := require.New(t)
for _, f := range []interface{}{foo{}, &foo{}} {
c := columns.ColumnsForStruct(f, "foo")
c := columns.ForStruct(f, "foo")
u := c.Writeable().SymbolizedString()
r.Equal(u, ":LastName, :write")
}
Expand All @@ -19,7 +19,7 @@ func Test_Columns_WriteableString_Symbolized(t *testing.T) {
func Test_Columns_UpdateString(t *testing.T) {
r := require.New(t)
for _, f := range []interface{}{foo{}, &foo{}} {
c := columns.ColumnsForStruct(f, "foo")
c := columns.ForStruct(f, "foo")
u := c.Writeable().UpdateString()
r.Equal(u, "LastName = :LastName, write = :write")
}
Expand All @@ -28,7 +28,7 @@ func Test_Columns_UpdateString(t *testing.T) {
func Test_Columns_WriteableString(t *testing.T) {
r := require.New(t)
for _, f := range []interface{}{foo{}, &foo{}} {
c := columns.ColumnsForStruct(f, "foo")
c := columns.ForStruct(f, "foo")
u := c.Writeable().String()
r.Equal(u, "LastName, write")
}
Expand Down
4 changes: 2 additions & 2 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ func (c *Connection) TruncateAll() error {
}

func (c *Connection) timeFunc(name string, fn func() error) error {
now := time.Now()
start := time.Now()
err := fn()
atomic.AddInt64(&c.Elapsed, int64(time.Now().Sub(now)))
atomic.AddInt64(&c.Elapsed, int64(time.Since(start)))
if err != nil {
return errors.WithStack(err)
}
Expand Down
4 changes: 2 additions & 2 deletions executors.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (c *Connection) Create(model interface{}, excludeColumns ...string) error {
return err
}

cols := columns.ColumnsForStructWithAlias(m.Value, m.TableName(), m.As)
cols := columns.ForStructWithAlias(m.Value, m.TableName(), m.As)

if sm.TableName() == m.TableName() {
cols.Remove(excludeColumns...)
Expand Down Expand Up @@ -160,7 +160,7 @@ func (c *Connection) Update(model interface{}, excludeColumns ...string) error {
return err
}

cols := columns.ColumnsForStructWithAlias(model, m.TableName(), m.As)
cols := columns.ForStructWithAlias(model, m.TableName(), m.As)
cols.Remove("id", "created_at")

if m.TableName() == sm.TableName() {
Expand Down
4 changes: 2 additions & 2 deletions executors_eager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func (c *Connection) eagerCreate(model interface{}, excludeColumns ...string) error {
asos, err := associations.AssociationsForStruct(model, c.eagerFields...)
asos, err := associations.ForStruct(model, c.eagerFields...)
if err != nil {
return err
}
Expand Down Expand Up @@ -80,7 +80,7 @@ func (c *Connection) eagerCreate(model interface{}, excludeColumns ...string) er
}

func (c *Connection) eagerValidateAndCreate(model interface{}, excludeColumns ...string) (*validate.Errors, error) {
asos, err := associations.AssociationsForStruct(model, c.eagerFields...)
asos, err := associations.ForStruct(model, c.eagerFields...)
verrs := validate.NewErrors()

if err != nil {
Expand Down
Loading

0 comments on commit 232efba

Please sign in to comment.