diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index a5411a0..73f645f 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -14,6 +14,9 @@ env: jobs: test: name: Test + strategy: + matrix: + version: ["1.20", "1.21", "1.22"] runs-on: ubuntu-latest steps: - uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f @@ -21,7 +24,7 @@ jobs: persist-credentials: false - uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 with: - go-version-file: go.mod + go-version: ${{ matrix.version }} cache: true - run: make test diff --git a/README.md b/README.md index 7c579cc..c49b84d 100644 --- a/README.md +++ b/README.md @@ -1,43 +1,23 @@ - - +# TypeID -# github.com/sumup/typeid - -# Install - -```shell -go install github.com/sumup/typeid -``` -# Overview - -TypeIDs are a draft standard for *type-safe, globally unique identifiers* based on the upcoming [UUIDv7 standard]. -Their properties, particularly k-sortability, make them suitable primary identifiers for classic database systems like PostgreSQL. -However, k-sortability may not always be desirable. For instance, you might require an identifier with high randomness entropy for security reasons. -Additionally, in distributed database systems like CockroachDB, having a k-sortable primary key can lead to hotspots and performance issues. +TypeIDs are a draft standard for *type-safe, globally unique identifiers* based on the upcoming [UUIDv7 standard]. Their properties, particularly k-sortability, make them suitable primary identifiers for classic database systems like PostgreSQL. However, k-sortability may not always be desirable. For instance, you might require an identifier with high randomness entropy for security reasons. Additionally, in distributed database systems like CockroachDB, having a k-sortable primary key can lead to hotspots and performance issues. While this package draws inspiration from the original typeid Go package ([go.jetpack.io/typeid]), it provides multiple ID types: -``` -- [typeid.Sortable] is based on UUIDv7 and is k-sortable. Its implementation adheres to the draft standard. - The suffix part is encoded in **lowercase** crockford base32. -- [typeid.Random] is also based on UUIDv4 and is completely random. Unlike `typeid.Sortable`, - the suffix part is encoded in **uppercase** crockford base32. -``` +- [`typeid.Sortable`] is based on UUIDv7 and is k-sortable. Its implementation adheres to the draft standard. The suffix part is encoded in **lowercase** crockford base32. +- [`typeid.Random`] is also based on UUIDv4 and is completely random. Unlike `typeid.Sortable`, the suffix part is encoded in **uppercase** crockford base32. Please refer to the respective type documentation for more details. -# Database Support - -ID types in this package can be used with [database/sql] and [github.com/jackc/pgx]. +## Install -When using the standard library sql, IDs will be stored as their string representation and can be scanned and valued accordingly. -When using pgx, both TEXT and UUID columns can be used directly. However, note that the type information is lost when using UUID columns, unless you take additional steps -at the database layer. Be mindful of your identifier semantics, especially in complex JOIN queries. +```shell +go install github.com/sumup/typeid +``` # Usage -To create a new ID type, define a prefix type that implements the [Prefix] interface. Then, define a TypeAlias for your ID type to [Random] or [Sortable] with your -prefix type as generic argument. +To create a new ID type, define a prefix type that implements the [Prefix] interface. Then, define a TypeAlias for your ID type to [Random] or [Sortable] with your prefix type as generic argument. Example: @@ -59,10 +39,11 @@ if err != nil { fmt.Println(userID) // --> user_01hf98sp99fs2b4qf2jm11hse4 ``` -[UUIDv7 standard]: https://www.ietf.org/archive/id/draft-peabody-dispatch-new-uuid-format-01.html#name-versions - -[UUIDv4 standard]: https://datatracker.ietf.org/doc/html/rfc4122 - +# Database Support +ID types in this package can be used with [database/sql] and [github.com/jackc/pgx]. +When using the standard library sql, IDs will be stored as their string representation and can be scanned and valued accordingly. When using pgx, both TEXT and UUID columns can be used directly. However, note that the type information is lost when using UUID columns, unless you take additional steps at the database layer. Be mindful of your identifier semantics, especially in complex JOIN queries. +[UUIDv7 standard]: https://www.ietf.org/archive/id/draft-peabody-dispatch-new-uuid-format-01.html#name-versions +[UUIDv4 standard]: https://datatracker.ietf.org/doc/html/rfc4122 diff --git a/go.mod b/go.mod index d26eacf..4d9b89c 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,6 @@ module github.com/sumup/typeid -go 1.21 - -toolchain go1.21.5 +go 1.20 require ( github.com/gofrs/uuid/v5 v5.1.0 diff --git a/go.sum b/go.sum index ca3e25a..79f5b46 100644 --- a/go.sum +++ b/go.sum @@ -1,17 +1,12 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gofrs/uuid/v5 v5.1.0 h1:S5rqVKIigghZTCBKPCw0Y+bXkn26K3TB5mvQq2Ix8dk= github.com/gofrs/uuid/v5 v5.1.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= -github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= -github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw= github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A= github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= -github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc= @@ -19,6 +14,4 @@ golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1m golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= -golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/typeid.go b/typeid.go index a21bf7f..61f8672 100644 --- a/typeid.go +++ b/typeid.go @@ -119,7 +119,7 @@ func fromUnprefixString[T instance[P], P Prefix](suffix string) (T, error) { func FromUUID[T instance[P], P Prefix](u uuid.UUID) (T, error) { if err := validatePrefix(getPrefix[P]()); err != nil { - return Nil[T, P](), err + return Nil[T](), err } // TODO: Add UUID validation for specific type return T{typedID[P]{u}}, nil @@ -128,15 +128,15 @@ func FromUUID[T instance[P], P Prefix](u uuid.UUID) (T, error) { func FromUUIDStr[T instance[P], P Prefix](uuidStr string) (T, error) { u, err := uuid.FromString(uuidStr) if err != nil { - return Nil[T, P](), fmt.Errorf("typeid from uuid string: %w", err) + return Nil[T](), fmt.Errorf("typeid from uuid string: %w", err) } - return FromUUID[T, P](u) + return FromUUID[T](u) } func FromUUIDBytes[T instance[P], P Prefix](bytes []byte) (T, error) { u, err := uuid.FromBytes(bytes) if err != nil { - return Nil[T, P](), fmt.Errorf("typeid from uuid: %w", err) + return Nil[T](), fmt.Errorf("typeid from uuid: %w", err) } - return FromUUID[T, P](u) + return FromUUID[T](u) }