diff --git a/README.md b/README.md index d1dbd42..b91049d 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/generator/migration.go b/generator/migration.go index 2149254..5b62ebc 100644 --- a/generator/migration.go +++ b/generator/migration.go @@ -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{ diff --git a/generator/migration_test.go b/generator/migration_test.go index 0dbfd60..e19bfa5 100644 --- a/generator/migration_test.go +++ b/generator/migration_test.go @@ -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) { @@ -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 { @@ -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() { @@ -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), diff --git a/generator/types.go b/generator/types.go index 7da500b..caf7bf7 100644 --- a/generator/types.go +++ b/generator/types.go @@ -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", } @@ -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", } diff --git a/model.go b/model.go index e64f228..2675fcf 100644 --- a/model.go +++ b/model.go @@ -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, @@ -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. @@ -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. @@ -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. @@ -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.