From 0fc4369679f230655d38b0734f20007742ed4538 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Fri, 7 Mar 2025 17:00:38 -0600 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20UUID=20generator?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generators.go | 23 +++++++++++++++++++++++ generators_test.go | 33 +++++++++++++++++++++++++++++++++ go.mod | 5 ++++- go.sum | 2 ++ options.go | 4 ++-- options_test.go | 6 ++++-- 6 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 generators.go create mode 100644 generators_test.go diff --git a/generators.go b/generators.go new file mode 100644 index 0000000..099bc13 --- /dev/null +++ b/generators.go @@ -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) + } +} diff --git a/generators_test.go b/generators_test.go new file mode 100644 index 0000000..3691736 --- /dev/null +++ b/generators_test.go @@ -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) +} diff --git a/go.mod b/go.mod index d428f6c..de6af60 100644 --- a/go.mod +++ b/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 diff --git a/go.sum b/go.sum index 713a0b4..14c872b 100644 --- a/go.sum +++ b/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= diff --git a/options.go b/options.go index 395925f..67d09c1 100644 --- a/options.go +++ b/options.go @@ -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, } } diff --git a/options_test.go b/options_test.go index 1c7486b..ea9ed47 100644 --- a/options_test.go +++ b/options_test.go @@ -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) {