Skip to content

Commit

Permalink
feat: support for last 3 version of go
Browse files Browse the repository at this point in the history
  • Loading branch information
matoous committed May 14, 2024
1 parent dbb8211 commit f8f4551
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 49 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ env:
jobs:
test:
name: Test
strategy:
matrix:
version: [1.20, 1.21, 1.22]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f
with:
persist-credentials: false
- uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491
with:
go-version-file: go.mod
go-version: ${{ matrix.version }}
cache: true
- run: make test

Expand Down
47 changes: 14 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,23 @@
<!-- DO NOT EDIT. -->
<!-- Automatically generated with https://go.jpap.org/godoc-readme-gen -->
# 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:

Expand All @@ -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
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 0 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
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=
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
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=
10 changes: 5 additions & 5 deletions typeid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

0 comments on commit f8f4551

Please sign in to comment.