Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ issues:
max-same-issues: 0
uniq-by-line: true
formatters:
exclusions:
paths:
- internal/uuid
enable:
- gofmt
- goimports
Expand Down Expand Up @@ -117,6 +120,8 @@ linters:
multi-func: false
exclusions:
generated: lax
paths:
- internal/uuid
presets:
- comments
- common-false-positives
Expand Down
3 changes: 2 additions & 1 deletion encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package typeid
import (
"fmt"

"github.com/gofrs/uuid/v5"
"github.com/jackc/pgx/v5/pgtype"

"github.com/sumup/typeid/internal/uuid"
)

// idImplementation is a helper constraint asserting the existence of the typeID methods on the type.
Expand Down
21 changes: 7 additions & 14 deletions generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"unsafe"

"github.com/gofrs/uuid/v5"
"github.com/sumup/typeid/internal/uuid"
)

// processor is an internal structure to handle different types of typedIDs, as they
Expand All @@ -17,7 +17,7 @@ type processor struct {
// b32Decode decode a UUID using the resp. base32 decoding.
b32Decode func(string) (uuid.UUID, error)
// Generates a new universal unique identifier.
generateUUID func() (uuid.UUID, error)
generateUUID func() uuid.UUID
}

func from[P Prefix](suffix string, p *processor) (typedID[P], error) {
Expand All @@ -37,23 +37,16 @@ func from[P Prefix](suffix string, p *processor) (typedID[P], error) {

// generate generates
func generate[P Prefix](p *processor) (typedID[P], error) {
var err error

if err = validatePrefix(getPrefix[P]()); err != nil {
if err := validatePrefix(getPrefix[P]()); err != nil {
return nilID[P](), err
}

tid := typedID[P]{}
tid.uuid, err = p.generateUUID()
if err != nil {
return nilID[P](), err
}
return tid, nil
return typedID[P]{uuid: p.generateUUID()}, nil
}

func nilID[P Prefix]() typedID[P] {
return typedID[P]{
uuid: uuid.Nil,
uuid: uuid.UUID{},
}
}

Expand All @@ -78,11 +71,11 @@ func validatePrefix(prefix string) error {

func decodeSuffix(suffix string, p *processor) (uuid.UUID, error) {
if len(suffix) != suffixStrLen {
return uuid.Nil, fmt.Errorf("invalid suffix: %s. Suffix length is %d, expected %d", suffix, len(suffix), suffixStrLen)
return uuid.UUID{}, fmt.Errorf("invalid suffix: %s. Suffix length is %d, expected %d", suffix, len(suffix), suffixStrLen)
}

if suffix[0] > '7' {
return uuid.Nil, fmt.Errorf("invalid suffix: '%s'. Suffix must start with a 0-7 digit to avoid overflows", suffix)
return uuid.UUID{}, fmt.Errorf("invalid suffix: '%s'. Suffix must start with a 0-7 digit to avoid overflows", suffix)
}

return p.b32Decode(suffix)
Expand Down
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@ module github.com/sumup/typeid

go 1.25.0

require (
github.com/gofrs/uuid/v5 v5.5.1
github.com/jackc/pgx/v5 v5.10.0
)
require github.com/jackc/pgx/v5 v5.11.0
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
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.5.1 h1:z1Ce19/JwNidXpy3tOQc3241lnJLKdKyq/xlNvlD4Ng=
github.com/gofrs/uuid/v5 v5.5.1/go.mod h1:bbAA98EoIlxyRHIVg6ektCSsZ5n8mSbwgEhvhMYlZgg=
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-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.10.0 h1:VhSvgU2jSli8o3AqIEOTJr7rZwAEUVo4E4XhR94Zfr0=
github.com/jackc/pgx/v5 v5.10.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=
github.com/jackc/pgx/v5 v5.11.0 h1:IzBBtyK9AHqf98cctWFifYSci2hgQR/cd56wB4p+ogg=
github.com/jackc/pgx/v5 v5.11.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
27 changes: 27 additions & 0 deletions internal/uuid/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright 2009 The Go Authors.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google LLC nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13 changes: 13 additions & 0 deletions internal/uuid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Vendored Go UUID implementation

`uuid.go` is copied from Go **1.27.1**:

- https://github.com/golang/go/blob/go1.27.1/src/uuid/uuid.go

The BSD 3-Clause license is reproduced in `LICENSE`. The implementation is
unchanged.
Keep local compatibility code outside this directory so future updates can be
compared directly with upstream.

This copy allows typeid to keep supporting Go 1.25 without a UUID module
dependency. It uses only APIs available in Go 1.25.
Loading