Skip to content

Commit

Permalink
Handle remaining golint issues
Browse files Browse the repository at this point in the history
* Missing documentation of public API
* Dot imports
* Conditions style
  • Loading branch information
stanislas-m committed Oct 19, 2017
1 parent 4a55bb8 commit f87faec
Show file tree
Hide file tree
Showing 22 changed files with 87 additions and 28 deletions.
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func init() {
LoadConfigFile()
}

// LoadConfigFile loads a POP config file from the configured lookup paths
func LoadConfigFile() error {
path, err := findConfigPath()
if err != nil {
Expand All @@ -48,10 +49,12 @@ func LoadConfigFile() error {
return LoadFrom(f)
}

// LookupPaths returns the current configuration lookup paths
func LookupPaths() []string {
return lookupPaths
}

// AddLookupPaths add paths to the current lookup paths list
func AddLookupPaths(paths ...string) error {
lookupPaths = append(paths, lookupPaths...)
return LoadConfigFile()
Expand Down
6 changes: 6 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ func (c *Connection) String() string {
return c.URL()
}

// URL returns the datasource connection string
func (c *Connection) URL() string {
return c.Dialect.URL()
}

// MigrationURL returns the datasource connection string used for running the migrations
func (c *Connection) MigrationURL() string {
return c.Dialect.MigrationURL()
}
Expand Down Expand Up @@ -80,6 +82,7 @@ func Connect(e string) (*Connection, error) {
return c, errors.Wrapf(err, "couldn't open connection for %s", e)
}

// Open creates a new datasource connection
func (c *Connection) Open() error {
if c.Store != nil {
return nil
Expand All @@ -92,6 +95,7 @@ func (c *Connection) Open() error {
return errors.Wrap(err, "coudn't connection to database")
}

// Close destroys an active datasource connection
func (c *Connection) Close() error {
return errors.Wrap(c.Store.Close(), "couldn't close connection")
}
Expand Down Expand Up @@ -120,6 +124,7 @@ func (c *Connection) Transaction(fn func(tx *Connection) error) error {

}

// NewTransaction starts a new transaction on the connection
func (c *Connection) NewTransaction() (*Connection, error) {
var cn *Connection
if c.TX == nil {
Expand Down Expand Up @@ -166,6 +171,7 @@ func (c *Connection) Q() *Query {
return Q(c)
}

// TruncateAll truncates all data from the datasource
func (c *Connection) TruncateAll() error {
return c.Dialect.TruncateAll(c)
}
Expand Down
3 changes: 3 additions & 0 deletions connection_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/pkg/errors"
)

// ConnectionDetails stores the data needed to connect to a datasource
type ConnectionDetails struct {
// Example: "postgres" or "sqlite3" or "mysql"
Dialect string
Expand Down Expand Up @@ -114,6 +115,7 @@ func (cd *ConnectionDetails) Parse(port string) error {
return cd.Finalize()
}

// RetrySleep returns the amount of time to wait between two connection retries
func (cd *ConnectionDetails) RetrySleep() time.Duration {
d, err := time.ParseDuration(defaults.String(cd.Options["retry_sleep"], "1ms"))
if err != nil {
Expand All @@ -122,6 +124,7 @@ func (cd *ConnectionDetails) RetrySleep() time.Duration {
return d
}

// RetryLimit returns the maximum number of accepted connection retries
func (cd *ConnectionDetails) RetryLimit() int {
i, err := strconv.Atoi(defaults.String(cd.Options["retry_limit"], "1000"))
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"io"

. "github.com/markbates/pop/columns"
"github.com/markbates/pop/columns"
"github.com/markbates/pop/fizz"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
Expand All @@ -20,8 +20,8 @@ type dialect interface {
MigrationURL() string
Details() *ConnectionDetails
TranslateSQL(string) string
Create(store, *Model, Columns) error
Update(store, *Model, Columns) error
Create(store, *Model, columns.Columns) error
Update(store, *Model, columns.Columns) error
Destroy(store, *Model) error
SelectOne(store, *Model, Query) error
SelectMany(store, *Model, Query) error
Expand All @@ -34,7 +34,7 @@ type dialect interface {
TruncateAll(*Connection) error
}

func genericCreate(s store, model *Model, cols Columns) error {
func genericCreate(s store, model *Model, cols columns.Columns) error {
keyType := model.PrimaryKeyType()
switch keyType {
case "int", "int64":
Expand Down Expand Up @@ -75,7 +75,7 @@ func genericCreate(s store, model *Model, cols Columns) error {
return errors.Errorf("can not use %s as a primary key type!", keyType)
}

func genericUpdate(s store, model *Model, cols Columns) error {
func genericUpdate(s store, model *Model, cols columns.Columns) error {
stmt := fmt.Sprintf("UPDATE %s SET %s where %s", model.TableName(), cols.Writeable().UpdateString(), model.whereID())
Log(stmt)
_, err := s.NamedExec(stmt, model.Value)
Expand Down
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
So what does Pop do exactly? Well, it wraps the absolutely amazing https://github.com/jmoiron/sqlx library. It cleans up some of the common patterns and workflows usually associated with dealing with databases in Go.
Package pop wraps the absolutely amazing https://github.com/jmoiron/sqlx library. It cleans up some of the common patterns and workflows usually associated with dealing with databases in Go.
Pop makes it easy to do CRUD operations, run migrations, and build/execute queries. Is Pop an ORM? I'll leave that up to you, the reader, to decide.
Expand Down
21 changes: 18 additions & 3 deletions executors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ package pop
import (
"fmt"

. "github.com/markbates/pop/columns"
"github.com/markbates/pop/columns"
"github.com/markbates/validate"
uuid "github.com/satori/go.uuid"
)

// Reload fetch fresh data for a given model, using its ID
func (c *Connection) Reload(model interface{}) error {
sm := Model{Value: model}
return c.Find(model, sm.ID())
}

// Exec runs the given query
func (q *Query) Exec() error {
return q.Connection.timeFunc("Exec", func() error {
sql, args := q.ToSQL(nil)
Expand All @@ -22,6 +24,8 @@ func (q *Query) Exec() error {
})
}

// ValidateAndSave applies validation rules on the given entry, then save it
// if the validation succeed, excluding the given columns.
func (c *Connection) ValidateAndSave(model interface{}, excludeColumns ...string) (*validate.Errors, error) {
sm := &Model{Value: model}
verrs, err := sm.validateSave(c)
Expand All @@ -36,6 +40,8 @@ func (c *Connection) ValidateAndSave(model interface{}, excludeColumns ...string

var emptyUUID = uuid.Nil.String()

// Save wraps the Create and Update methods. It executes a Create if no ID is provided with the entry;
// or issues an Update otherwise.
func (c *Connection) Save(model interface{}, excludeColumns ...string) error {
sm := &Model{Value: model}
id := sm.ID()
Expand All @@ -46,6 +52,8 @@ func (c *Connection) Save(model interface{}, excludeColumns ...string) error {
return c.Update(model, excludeColumns...)
}

// ValidateAndCreate applies validation rules on the given entry, then creates it
// if the validation succeed, excluding the given columns.
func (c *Connection) ValidateAndCreate(model interface{}, excludeColumns ...string) (*validate.Errors, error) {
sm := &Model{Value: model}
verrs, err := sm.validateCreate(c)
Expand All @@ -58,6 +66,8 @@ func (c *Connection) ValidateAndCreate(model interface{}, excludeColumns ...stri
return verrs, c.Create(model, excludeColumns...)
}

// Create add a new given entry to the database, excluding the given columns.
// It updates `created_at` and `updated_at` columns automatically.
func (c *Connection) Create(model interface{}, excludeColumns ...string) error {
return c.timeFunc("Create", func() error {
var err error
Expand All @@ -71,7 +81,7 @@ func (c *Connection) Create(model interface{}, excludeColumns ...string) error {
return err
}

cols := ColumnsForStructWithAlias(model, sm.TableName(), sm.As)
cols := columns.ColumnsForStructWithAlias(model, sm.TableName(), sm.As)
cols.Remove(excludeColumns...)

sm.touchCreatedAt()
Expand All @@ -89,6 +99,8 @@ func (c *Connection) Create(model interface{}, excludeColumns ...string) error {
})
}

// ValidateAndUpdate applies validation rules on the given entry, then update it
// if the validation succeed, excluding the given columns.
func (c *Connection) ValidateAndUpdate(model interface{}, excludeColumns ...string) (*validate.Errors, error) {
sm := &Model{Value: model}
verrs, err := sm.validateUpdate(c)
Expand All @@ -101,6 +113,8 @@ func (c *Connection) ValidateAndUpdate(model interface{}, excludeColumns ...stri
return verrs, c.Update(model, excludeColumns...)
}

// Update writes changes from an entry to the database, excluding the given columns.
// It updates the `updated_at` column automatically.
func (c *Connection) Update(model interface{}, excludeColumns ...string) error {
return c.timeFunc("Update", func() error {
var err error
Expand All @@ -113,7 +127,7 @@ func (c *Connection) Update(model interface{}, excludeColumns ...string) error {
return err
}

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

Expand All @@ -130,6 +144,7 @@ func (c *Connection) Update(model interface{}, excludeColumns ...string) error {
})
}

// Destroy deletes a given entry from the database
func (c *Connection) Destroy(model interface{}) error {
return c.timeFunc("Destroy", func() error {
var err error
Expand Down
3 changes: 3 additions & 0 deletions finders.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ func (q Query) Count(model interface{}) (int, error) {
return q.CountByField(model, "*")
}

// CountByField counts the number of records in the database, for a given field.
//
// q.Where("sex = ?", "f").Count(&User{}, "name")
func (q Query) CountByField(model interface{}, field string) (int, error) {
res := &rowCount{}
err := q.Connection.timeFunc("Count", func() error {
Expand Down
1 change: 1 addition & 0 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"
)

// GroupClause holds the field to apply the GROUP clause on
type GroupClause struct {
Field string
}
Expand Down
1 change: 1 addition & 0 deletions having.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
)

// HavingClause defines a condition and its arguments for a HAVING clause
type HavingClause struct {
Condition string
Arguments []interface{}
Expand Down
1 change: 1 addition & 0 deletions migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/pkg/errors"
)

// MigrationCreate writes contents for a given migration in normalized files
func MigrationCreate(path, name, ext string, up, down []byte) error {
n := time.Now().UTC()
s := n.Format("20060102150405")
Expand Down
2 changes: 2 additions & 0 deletions migration_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pop

import "github.com/pkg/errors"

// Migration handles the data for a given database migration
type Migration struct {
// Path to the migration (./migrations/123_create_widgets.up.sql)
Path string
Expand All @@ -26,6 +27,7 @@ func (mf Migration) Run(c *Connection) error {
return mf.Runner(mf, c)
}

// Migrations is a collection of Migration
type Migrations []Migration

func (mfs Migrations) Len() int {
Expand Down
2 changes: 2 additions & 0 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func MapTableName(name string, tableName string) {
tableMap[name] = tableName
}

// Value is the contents of a `Model`.
type Value interface{}

// Model is used throughout Pop to wrap the end user interface
Expand All @@ -54,6 +55,7 @@ func (m *Model) ID() interface{} {
return fbn.Interface()
}

// PrimaryKeyType gives the primary key type of the `Model`.
func (m *Model) PrimaryKeyType() string {
fbn, err := m.fieldByName("ID")
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

// Load MySQL Go driver
_ "github.com/go-sql-driver/mysql"
. "github.com/markbates/pop/columns"
"github.com/markbates/pop/columns"
"github.com/markbates/pop/fizz"
"github.com/markbates/pop/fizz/translators"
"github.com/pkg/errors"
Expand Down Expand Up @@ -38,11 +38,11 @@ func (m *mysql) MigrationURL() string {
return m.URL()
}

func (m *mysql) Create(s store, model *Model, cols Columns) error {
func (m *mysql) Create(s store, model *Model, cols columns.Columns) error {
return errors.Wrap(genericCreate(s, model, cols), "mysql create")
}

func (m *mysql) Update(s store, model *Model, cols Columns) error {
func (m *mysql) Update(s store, model *Model, cols columns.Columns) error {
return errors.Wrap(genericUpdate(s, model, cols), "mysql update")
}

Expand Down
11 changes: 9 additions & 2 deletions paginator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ import (
"github.com/markbates/going/defaults"
)

// PaginatorPerPageDefault is the amount of results per page
var PaginatorPerPageDefault = 20

// PaginatorPageKey is the query parameter holding the current page index
var PaginatorPageKey = "page"

// PaginatorPerPageKey is the query parameter holding the amount of results per page
// to override the default one
var PaginatorPerPageKey = "per_page"

// Paginator is a type used to represent the pagination of records
Expand Down Expand Up @@ -47,6 +53,7 @@ func NewPaginator(page int, perPage int) *Paginator {
return p
}

// PaginationParams is a parameters provider interface to get the pagination params from
type PaginationParams interface {
Get(key string) string
}
Expand Down Expand Up @@ -92,7 +99,7 @@ func (q *Query) Paginate(page int, perPage int) *Query {
return q
}

// Paginate records returned from the database.
// PaginateFromParams paginates records returned from the database.
//
// q := c.PaginateFromParams(req.URL.Query())
// q.All(&[]User{})
Expand All @@ -101,7 +108,7 @@ func (c *Connection) PaginateFromParams(params PaginationParams) *Query {
return Q(c).PaginateFromParams(params)
}

// Paginate records returned from the database.
// PaginateFromParams paginates records returned from the database.
//
// q = q.PaginateFromParams(req.URL.Query())
// q.All(&[]User{})
Expand Down
4 changes: 4 additions & 0 deletions pop.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import (
"github.com/fatih/color"
)

// Debug mode, to toggle verbose log traces
var Debug = false

// Color mode, to toggle colored logs
var Color = true
var logger = log.New(os.Stdout, "[POP] ", log.LstdFlags)

// Log a formatted string to the logger
var Log = func(s string, args ...interface{}) {
if Debug {
if len(args) > 0 {
Expand Down
Loading

0 comments on commit f87faec

Please sign in to comment.