Skip to content

Commit

Permalink
moves a few dependencies internally to cut down on the number or exte…
Browse files Browse the repository at this point in the history
…rnal (#399)

deps
  • Loading branch information
markbates authored and stanislas-m committed Jun 14, 2019
1 parent 6df815a commit 05102f3
Show file tree
Hide file tree
Showing 29 changed files with 239 additions and 35 deletions.
2 changes: 1 addition & 1 deletion associations/associations_for_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"

"github.com/gobuffalo/pop/columns"
"github.com/markbates/oncer"
"github.com/gobuffalo/pop/internal/oncer"
)

// If a field match with the regexp, it will be considered as a valid field definition.
Expand Down
2 changes: 1 addition & 1 deletion associations/belongs_to_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/gobuffalo/flect"
"github.com/gobuffalo/nulls"
"github.com/gobuffalo/pop/columns"
"github.com/gobuffalo/x/defaults"
"github.com/gobuffalo/pop/internal/defaults"
)

// belongsToAssociation is the implementation for the belongs_to association type in a model.
Expand Down
2 changes: 1 addition & 1 deletion associations/has_one_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/gobuffalo/flect"
"github.com/gobuffalo/nulls"
"github.com/gobuffalo/x/defaults"
"github.com/gobuffalo/pop/internal/defaults"
)

// hasOneAssociation is a 1 to 1 kind of association. It's used on
Expand Down
2 changes: 1 addition & 1 deletion associations/many_to_many_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/gobuffalo/flect"
"github.com/gobuffalo/x/defaults"
"github.com/gobuffalo/pop/internal/defaults"
"github.com/gofrs/uuid"
)

Expand Down
2 changes: 1 addition & 1 deletion columns/columns_for_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package columns
import (
"reflect"

"github.com/markbates/oncer"
"github.com/gobuffalo/pop/internal/oncer"
)

// ColumnsForStruct returns a Columns instance for
Expand Down
4 changes: 2 additions & 2 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"sync/atomic"
"time"

"github.com/gobuffalo/pop/internal/defaults"
"github.com/gobuffalo/pop/internal/randx"
"github.com/jmoiron/sqlx"
"github.com/markbates/going/defaults"
"github.com/markbates/going/randx"
"github.com/pkg/errors"
)

Expand Down
4 changes: 2 additions & 2 deletions connection_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"strings"
"time"

"github.com/gobuffalo/pop/internal/defaults"
"github.com/gobuffalo/pop/internal/oncer"
"github.com/gobuffalo/pop/logging"
"github.com/markbates/going/defaults"
"github.com/markbates/oncer"
"github.com/pkg/errors"
)

Expand Down
2 changes: 1 addition & 1 deletion dialect_cockroach.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
"github.com/gobuffalo/fizz"
"github.com/gobuffalo/fizz/translators"
"github.com/gobuffalo/pop/columns"
"github.com/gobuffalo/pop/internal/defaults"
"github.com/gobuffalo/pop/logging"
"github.com/jmoiron/sqlx"
"github.com/markbates/going/defaults"
"github.com/pkg/errors"
)

Expand Down
4 changes: 2 additions & 2 deletions dialect_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
"github.com/gobuffalo/fizz"
"github.com/gobuffalo/fizz/translators"
"github.com/gobuffalo/pop/columns"
"github.com/gobuffalo/pop/internal/defaults"
"github.com/gobuffalo/pop/internal/oncer"
"github.com/gobuffalo/pop/logging"
"github.com/markbates/going/defaults"
"github.com/markbates/oncer"
"github.com/pkg/errors"
)

Expand Down
2 changes: 1 addition & 1 deletion dialect_postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
"github.com/gobuffalo/fizz"
"github.com/gobuffalo/fizz/translators"
"github.com/gobuffalo/pop/columns"
"github.com/gobuffalo/pop/internal/defaults"
"github.com/gobuffalo/pop/logging"
"github.com/jmoiron/sqlx"
pg "github.com/lib/pq"
"github.com/markbates/going/defaults"
"github.com/pkg/errors"
)

Expand Down
2 changes: 1 addition & 1 deletion dialect_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"github.com/gobuffalo/fizz"
"github.com/gobuffalo/fizz/translators"
"github.com/gobuffalo/pop/columns"
"github.com/gobuffalo/pop/internal/defaults"
"github.com/gobuffalo/pop/logging"
"github.com/markbates/going/defaults"
_ "github.com/mattn/go-sqlite3" // Load SQLite3 CGo driver
"github.com/pkg/errors"
)
Expand Down
36 changes: 36 additions & 0 deletions internal/defaults/defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package defaults

func String(s1, s2 string) string {
if s1 == "" {
return s2
}
return s1
}

func Int(i1, i2 int) int {
if i1 == 0 {
return i2
}
return i1
}

func Int64(i1, i2 int64) int64 {
if i1 == 0 {
return i2
}
return i1
}

func Float32(i1, i2 float32) float32 {
if i1 == 0.0 {
return i2
}
return i1
}

func Float64(i1, i2 float64) float64 {
if i1 == 0.0 {
return i2
}
return i1
}
52 changes: 52 additions & 0 deletions internal/defaults/defaults_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package defaults

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_String(t *testing.T) {
a := assert.New(t)

a.Equal(String("", "foo"), "foo")
a.Equal(String("bar", "foo"), "bar")
var s string
a.Equal(String(s, "foo"), "foo")
}

func Test_Int(t *testing.T) {
a := assert.New(t)

a.Equal(Int(0, 1), 1)
a.Equal(Int(2, 1), 2)
var s int
a.Equal(Int(s, 1), 1)
}

func Test_Int64(t *testing.T) {
a := assert.New(t)

a.Equal(Int64(0, 1), int64(1))
a.Equal(Int64(2, 1), int64(2))
var s int64
a.Equal(Int64(s, 1), int64(1))
}

func Test_Float32(t *testing.T) {
a := assert.New(t)

a.Equal(Float32(0, 1), float32(1))
a.Equal(Float32(2, 1), float32(2))
var s float32
a.Equal(Float32(s, 1), float32(1))
}

func Test_Float64(t *testing.T) {
a := assert.New(t)

a.Equal(Float64(0, 1), float64(1))
a.Equal(Float64(2, 1), float64(2))
var s float64
a.Equal(Float64(s, 1), float64(1))
}
20 changes: 20 additions & 0 deletions internal/oncer/deprecate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package oncer

import (
"fmt"
"io"
"os"
)

const deprecated = "DEPRECATED"

var deprecationWriter io.Writer = os.Stdout

func Deprecate(depth int, name string, msg string) {
Do(deprecated+name, func() {
fmt.Fprintf(deprecationWriter, "[%s] %s has been deprecated.\n", deprecated, name)
if len(msg) > 0 {
fmt.Fprintf(deprecationWriter, "\t%s\n", msg)
}
})
}
26 changes: 26 additions & 0 deletions internal/oncer/oncer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package oncer

import (
"sync"
)

var onces = &sync.Map{}

func Do(name string, fn func()) {
o, _ := onces.LoadOrStore(name, &sync.Once{})
if once, ok := o.(*sync.Once); ok {
once.Do(fn)
}
}

func Reset(names ...string) {
if len(names) == 0 {
onces = &sync.Map{}
return
}

for _, n := range names {
onces.Delete(n)
onces.Delete(deprecated + n)
}
}
10 changes: 10 additions & 0 deletions internal/randx/randx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package randx

import (
"math/rand"
"time"
)

func init() {
rand.Seed(time.Now().UnixNano())
}
35 changes: 35 additions & 0 deletions internal/randx/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package randx

import (
"math/rand"
"time"
)

// source https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-go

const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const (
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)

var src = rand.NewSource(time.Now().UnixNano())

func String(n int) string {
b := make([]byte, n)
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}

return string(b)
}
18 changes: 18 additions & 0 deletions internal/randx/string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package randx

import (
"math/rand"
"testing"

"github.com/stretchr/testify/require"
)

func init() {
rand.Seed(1)
}

func Test_String(t *testing.T) {
r := require.New(t)
r.Len(String(5), 5)
r.Len(String(50), 50)
}
2 changes: 1 addition & 1 deletion logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"os"

"github.com/fatih/color"
"github.com/gobuffalo/pop/internal/oncer"
"github.com/gobuffalo/pop/logging"
"github.com/markbates/oncer"
)

type logger func(lvl logging.Level, s string, args ...interface{})
Expand Down
2 changes: 1 addition & 1 deletion migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/gobuffalo/makr"
"github.com/markbates/oncer"
"github.com/gobuffalo/pop/internal/oncer"
)

// MigrationCreate writes contents for a given migration in normalized files
Expand Down
27 changes: 17 additions & 10 deletions packrd/packed-packr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion paginator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"strconv"

"github.com/markbates/going/defaults"
"github.com/gobuffalo/pop/internal/defaults"
)

// PaginatorPerPageDefault is the amount of results per page
Expand Down
2 changes: 1 addition & 1 deletion query_joins.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package pop

import (
"github.com/gobuffalo/pop/internal/oncer"
"github.com/gobuffalo/pop/logging"
"github.com/markbates/oncer"
)

// Join will append a JOIN clause to the query
Expand Down
Loading

0 comments on commit 05102f3

Please sign in to comment.