Skip to content

Commit

Permalink
Swap satori/go.uuid to gofrs/uuid (#289)
Browse files Browse the repository at this point in the history
* Swap satori/go.uuid to gofrs/uuid

Signed-off-by: Maxim Sukharev <[email protected]>

* add generator test for uuids

Signed-off-by: Maxim Sukharev <[email protected]>
  • Loading branch information
smacker authored and Roberto Santalla committed Nov 15, 2018
1 parent 8759af1 commit c3e1e4d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ Primary key types need to satisfy the [Identifier](https://godoc.org/github.com/
The following types can be used as primary key:

* `int64`
* [`uuid.UUID`](https://godoc.org/github.com/satori/go.uuid#UUID)
* [`uuid.UUID`](https://godoc.org/github.com/gofrs/uuid#UUID)
* [`kallax.ULID`](https://godoc.org/github.com/src-d/go-kallax/#ULID): this is a type kallax provides that implements a lexically sortable UUID. You can store it as `uuid` like any other UUID, but internally it's an ULID and you will be able to sort lexically by it.

Due to how sql mapping works, pointers to `uuid.UUID` and `kallax.ULID` are not set to `nil` if they appear as `NULL` in the database, but to [`uuid.Nil`](https://godoc.org/github.com/satori/go.uuid#pkg-variables). Using pointers to UUIDs is discouraged for this reason.
Expand Down
39 changes: 20 additions & 19 deletions generator/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -990,25 +990,26 @@ var typeMappings = map[string]ColumnType{
"gopkg.in/src-d/go-kallax.v1.UUID": UUIDColumn,
"gopkg.in/src-d/go-kallax.v1.NumericID": BigIntColumn,
"github.com/satori/go.uuid.UUID": UUIDColumn,
"string": TextColumn,
"rune": ColumnType("char(1)"),
"uint8": SmallIntColumn,
"int8": SmallIntColumn,
"byte": SmallIntColumn,
"uint16": IntegerColumn,
"int16": SmallIntColumn,
"uint32": BigIntColumn,
"int32": IntegerColumn,
"uint": NumericColumn(20),
"int": BigIntColumn,
"int64": BigIntColumn,
"uint64": NumericColumn(20),
"float32": RealColumn,
"float64": DoubleColumn,
"bool": BooleanColumn,
"url.URL": TextColumn,
"time.Time": TimestamptzColumn,
"time.Duration": BigIntColumn,
"github.com/gofrs/uuid.UUID": UUIDColumn,
"string": TextColumn,
"rune": ColumnType("char(1)"),
"uint8": SmallIntColumn,
"int8": SmallIntColumn,
"byte": SmallIntColumn,
"uint16": IntegerColumn,
"int16": SmallIntColumn,
"uint32": BigIntColumn,
"int32": IntegerColumn,
"uint": NumericColumn(20),
"int": BigIntColumn,
"int64": BigIntColumn,
"uint64": NumericColumn(20),
"float32": RealColumn,
"float64": DoubleColumn,
"bool": BooleanColumn,
"url.URL": TextColumn,
"time.Time": TimestamptzColumn,
"time.Duration": BigIntColumn,
}

var idTypeMappings = map[string]ColumnType{
Expand Down
20 changes: 20 additions & 0 deletions generator/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import (

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

// we need this external libs to be able to generate code corrrectly
_ "github.com/gofrs/uuid"
_ "github.com/satori/go.uuid"
)

func TestNewMigration(t *testing.T) {
Expand Down Expand Up @@ -572,6 +576,8 @@ package foo
import (
"gopkg.in/src-d/go-kallax.v1"
"net/url"
satori "github.com/satori/go.uuid"
gofrs "github.com/gofrs/uuid"
)
type User struct {
Expand Down Expand Up @@ -607,6 +613,14 @@ type ProfileMetadata struct {
// a json field
Metadata map[string]interface{}
}
// contains all possible uuid types
type UUIDTable struct {
kallax.Model
ID kallax.ULID ` + "`pk:\"\"`" + `
SatoriUUID satori.UUID
GofrsUUID gofrs.UUID
}
`

func (s *PackageTransformerSuite) SetupTest() {
Expand Down Expand Up @@ -648,6 +662,12 @@ func (s *PackageTransformerSuite) TestTransform() {
mkCol("metadata", JSONBColumn, false, true, nil),
mkCol("profile_id", BigIntColumn, false, true, mkRef("profiles", "id", false)),
),
mkTable(
"uuidtable",
mkCol("id", UUIDColumn, true, true, nil),
mkCol("satori_uuid", UUIDColumn, false, true, nil),
mkCol("gofrs_uuid", UUIDColumn, false, true, nil),
),
mkTable(
"users",
mkCol("id", UUIDColumn, true, true, nil),
Expand Down
2 changes: 2 additions & 0 deletions generator/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ var specialTypes = map[string]string{
"gopkg.in/src-d/go-kallax.v1.ULID": "kallax.ULID",
"gopkg.in/src-d/go-kallax.v1.NumericID": "kallax.NumericID",
"github.com/satori/go.uuid.UUID": "kallax.UUID",
"github.com/gofrs/uuid.UUID": "kallax.UUID",
"net/url.URL": "url.URL",
"time.Time": "time.Time",
}
Expand Down Expand Up @@ -984,6 +985,7 @@ var identifierTypes = map[string]string{
"gopkg.in/src-d/go-kallax.v1.ULID": "kallax.ULID",
"gopkg.in/src-d/go-kallax.v1.NumericID": "kallax.NumericID",
"github.com/satori/go.uuid.UUID": "kallax.UUID",
"github.com/gofrs/uuid.UUID": "kallax.UUID",
"int64": "kallax.NumericID",
}

Expand Down
10 changes: 5 additions & 5 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"fmt"
"time"

"github.com/gofrs/uuid"
"github.com/oklog/ulid"
uuid "github.com/satori/go.uuid"
)

// Model contains all the basic fields that make something a model, that is,
Expand Down Expand Up @@ -340,7 +340,7 @@ func (id ULID) Value() (driver.Value, error) {
// IsEmpty returns whether the ID is empty or not. An empty ID means it has not
// been set yet.
func (id ULID) IsEmpty() bool {
return uuid.Equal(uuid.UUID(id), uuid.Nil)
return uuid.UUID(id) == uuid.Nil
}

// String returns the string representation of the ID.
Expand All @@ -355,7 +355,7 @@ func (id ULID) Equals(other Identifier) bool {
return false
}

return uuid.Equal(uuid.UUID(id), uuid.UUID(*v))
return uuid.UUID(id) == uuid.UUID(*v)
}

// Raw returns the underlying raw value.
Expand Down Expand Up @@ -430,7 +430,7 @@ func (id UUID) Value() (driver.Value, error) {
// IsEmpty returns whether the ID is empty or not. An empty ID means it has not
// been set yet.
func (id UUID) IsEmpty() bool {
return uuid.Equal(uuid.UUID(id), uuid.Nil)
return uuid.UUID(id) == uuid.Nil
}

// String returns the string representation of the ID.
Expand All @@ -445,7 +445,7 @@ func (id UUID) Equals(other Identifier) bool {
return false
}

return uuid.Equal(uuid.UUID(id), uuid.UUID(*v))
return uuid.UUID(id) == uuid.UUID(*v)
}

// Raw returns the underlying raw value.
Expand Down

0 comments on commit c3e1e4d

Please sign in to comment.