✨ Add UUID generator
This commit is contained in:
parent
bd8f9ae8a6
commit
0fc4369679
6 changed files with 68 additions and 5 deletions
23
generators.go
Normal file
23
generators.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package nomino
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func setGenerator(c *config, g generator) {
|
||||
c.generator = g
|
||||
}
|
||||
|
||||
func uuidGen() (string, error) {
|
||||
u, err := uuid.NewRandom()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return u.String(), nil
|
||||
}
|
||||
|
||||
func WithUUID() Option {
|
||||
return func(c *config) {
|
||||
setGenerator(c, uuidGen)
|
||||
}
|
||||
}
|
||||
33
generators_test.go
Normal file
33
generators_test.go
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package nomino
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestWithUUID(t *testing.T) {
|
||||
var c config
|
||||
WithUUID()(&c)
|
||||
st, err := c.generator()
|
||||
assert.NoError(t, err)
|
||||
_, parseErr := uuid.Parse(st)
|
||||
assert.NoError(t, parseErr)
|
||||
}
|
||||
|
||||
type badRead struct{}
|
||||
|
||||
func (badRead) Read([]byte) (int, error) {
|
||||
return 0, errors.New("sorry")
|
||||
}
|
||||
|
||||
func TestWithUUIDFail(t *testing.T) {
|
||||
var c config
|
||||
uuid.SetRand(badRead{})
|
||||
defer uuid.SetRand(nil)
|
||||
WithUUID()(&c)
|
||||
_, err := c.generator()
|
||||
assert.Equal(t, errors.New("sorry"), err)
|
||||
}
|
||||
5
go.mod
5
go.mod
|
|
@ -2,7 +2,10 @@ module codeberg.org/danjones000/nomino
|
|||
|
||||
go 1.23.6
|
||||
|
||||
require github.com/stretchr/testify v1.10.0
|
||||
require (
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/stretchr/testify v1.10.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
|
|
|
|||
2
go.sum
2
go.sum
|
|
@ -1,5 +1,7 @@
|
|||
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/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
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.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ package nomino
|
|||
|
||||
import "strings"
|
||||
|
||||
type generator func() string
|
||||
type generator func() (string, error)
|
||||
|
||||
type config struct {
|
||||
original string
|
||||
|
|
@ -15,7 +15,7 @@ type config struct {
|
|||
func defaultConf() config {
|
||||
return config{
|
||||
extension: ".txt",
|
||||
generator: func() string { return "abc" },
|
||||
generator: uuidGen,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,14 +3,16 @@ package nomino
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDefaultConf(t *testing.T) {
|
||||
c := defaultConf()
|
||||
assert.Equal(t, ".txt", c.extension)
|
||||
// assert.Nil(t, c.generator)
|
||||
assert.Equal(t, "abc", c.generator())
|
||||
st, _ := c.generator()
|
||||
_, parseErr := uuid.Parse(st)
|
||||
assert.NoError(t, parseErr)
|
||||
}
|
||||
|
||||
func TestWithOriginal(t *testing.T) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue