From 511a81cab33ca98b5ff55a03dc2c2b8daad4e50c Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Fri, 7 Mar 2025 16:25:40 -0600 Subject: [PATCH 1/8] =?UTF-8?q?=F0=9F=9B=A0=20Add=20Taskfile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 11 +++---- Taskfile.yml | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 Taskfile.yml diff --git a/.gitignore b/.gitignore index 1a431c2..71f6a2d 100644 --- a/.gitignore +++ b/.gitignore @@ -11,11 +11,8 @@ # Test binary, built with `go test -c` *.test -# Output of the go coverage tool, specifically when used with LiteIDE -*.out - -# Dependency directories (remove the comment below to include it) -# vendor/ +# Dependency directories +vendor/ # Go workspace file go.work @@ -23,4 +20,6 @@ go.work.sum # env file .env -cover.* + +build/ +.task/ diff --git a/Taskfile.yml b/Taskfile.yml new file mode 100644 index 0000000..025ff4b --- /dev/null +++ b/Taskfile.yml @@ -0,0 +1,86 @@ +version: '3' + +tasks: + default: + cmds: + - task: fmt + - task: test + - task: build + + fmt: + desc: Format go code + sources: + - '**/*.go' + cmds: + - go fmt ./... + - go mod tidy + + gen: + desc: Generate files + sources: + - '**/*.go' + cmds: + - go generate ./... + + vet: + desc: Vet go code + sources: + - '**/*.go' + cmds: + - go vet ./... + + critic: + desc: Critique go code + sources: + - '**/*.go' + cmds: + - gocritic check ./... + + staticcheck: + desc: Static check go code + sources: + - '**/*.go' + cmds: + - staticcheck ./... + + vuln: + desc: Check for vulnerabilities + sources: + - '**/*.go' + cmds: + - govulncheck ./... + + lint: + desc: Do static analysis + deps: + - vet + - critic + - staticcheck + - vuln + + test: + desc: Run unit tests + deps: [fmt, vet] + sources: + - '**/*.go' + generates: + - build/cover.out + cmds: + - go test -race -cover -coverprofile build/cover.out ./... + + coverage-report: + desc: Build coverage report + deps: [test] + sources: + - build/cover.out + generates: + - build/cover.html + cmds: + - go tool cover -html=build/cover.out -o build/cover.html + + serve-report: + desc: Serve the coverage report + deps: [coverage-report] + cmds: + - ip addr list | grep inet + - php -S 0.0.0.0:3265 -t build From bd8f9ae8a68d800918f452876449c967d2a1abe8 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Fri, 7 Mar 2025 16:26:24 -0600 Subject: [PATCH 2/8] =?UTF-8?q?=E2=9C=A8=20Add=20options.go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 8 ++++++++ go.sum | 10 ++++++++++ options.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ options_test.go | 48 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+) create mode 100644 go.sum create mode 100644 options.go create mode 100644 options_test.go diff --git a/go.mod b/go.mod index ff9df1b..d428f6c 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,11 @@ module codeberg.org/danjones000/nomino go 1.23.6 + +require github.com/stretchr/testify v1.10.0 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..713a0b4 --- /dev/null +++ b/go.sum @@ -0,0 +1,10 @@ +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/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= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +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/options.go b/options.go new file mode 100644 index 0000000..395925f --- /dev/null +++ b/options.go @@ -0,0 +1,52 @@ +package nomino + +import "strings" + +type generator func() string + +type config struct { + original string + prefix string + suffix string + extension string + generator generator +} + +func defaultConf() config { + return config{ + extension: ".txt", + generator: func() string { return "abc" }, + } +} + +type Option func(c *config) + +func WithOriginal(o string) Option { + return func(c *config) { + c.original = "_" + o + } +} + +func WithPrefix(p string) Option { + return func(c *config) { + c.prefix = p + "_" + } +} + +func WithSuffix(s string) Option { + return func(c *config) { + c.suffix = "_" + s + } +} + +func WithoutExtension() Option { + return func(c *config) { + c.extension = "" + } +} + +func WithExtension(ext string) Option { + return func(c *config) { + c.extension = "." + strings.TrimPrefix(ext, ".") + } +} diff --git a/options_test.go b/options_test.go new file mode 100644 index 0000000..1c7486b --- /dev/null +++ b/options_test.go @@ -0,0 +1,48 @@ +package nomino + +import ( + "testing" + + "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()) +} + +func TestWithOriginal(t *testing.T) { + var c config + name := "foobar" + WithOriginal(name)(&c) + assert.Equal(t, "_"+name, c.original) +} + +func TestWithPrefix(t *testing.T) { + var c config + pref := "draft" + WithPrefix(pref)(&c) + assert.Equal(t, pref+"_", c.prefix) +} + +func TestWithSuffix(t *testing.T) { + var c config + suff := "out" + WithSuffix(suff)(&c) + assert.Equal(t, "_"+suff, c.suffix) +} + +func TestWithoutExtension(t *testing.T) { + c := config{extension: ".foobar"} + WithoutExtension()(&c) + assert.Equal(t, "", c.extension) +} + +func TestWithExtension(t *testing.T) { + var c config + ext := "yaml" + WithExtension(ext)(&c) + assert.Equal(t, "."+ext, c.extension) +} From 0fc4369679f230655d38b0734f20007742ed4538 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Fri, 7 Mar 2025 17:00:38 -0600 Subject: [PATCH 3/8] =?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) { From ee627547a8d5c775fef3c581fac21347bd5a17a4 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Mon, 10 Mar 2025 11:47:01 -0500 Subject: [PATCH 4/8] =?UTF-8?q?=E2=9C=A8=20Add=20time-based=20generators?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generators.go | 26 ++++++++++++++++++++++++++ generators_test.go | 19 +++++++++++++++++++ options.go | 2 -- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/generators.go b/generators.go index 099bc13..403f440 100644 --- a/generators.go +++ b/generators.go @@ -1,9 +1,13 @@ package nomino import ( + "time" + "github.com/google/uuid" ) +type generator func() (string, error) + func setGenerator(c *config, g generator) { c.generator = g } @@ -21,3 +25,25 @@ func WithUUID() Option { setGenerator(c, uuidGen) } } + +const FileTimestamp string = "2006-01-02_03-05-06-0700" + +func WithTimestamp() Option { + return WithTimestampFormat(FileTimestamp) +} + +func WithTimestampFormat(f string) Option { + return WithFormattedTime(time.Now(), f) +} + +func WithTime(t time.Time) Option { + return WithFormattedTime(t, FileTimestamp) +} + +func WithFormattedTime(t time.Time, f string) Option { + return func(c *config) { + setGenerator(c, func() (string, error) { + return t.Format(f), nil + }) + } +} diff --git a/generators_test.go b/generators_test.go index 3691736..fb3f8c3 100644 --- a/generators_test.go +++ b/generators_test.go @@ -3,6 +3,7 @@ package nomino import ( "errors" "testing" + "time" "github.com/google/uuid" "github.com/stretchr/testify/assert" @@ -31,3 +32,21 @@ func TestWithUUIDFail(t *testing.T) { _, err := c.generator() assert.Equal(t, errors.New("sorry"), err) } + +func TestWithTimestamp(t *testing.T) { + var c config + WithTimestamp()(&c) + n := time.Now() + st, err := c.generator() + assert.NoError(t, err) + assert.Equal(t, n.Format(FileTimestamp), st) +} + +func TestWithTime(t *testing.T) { + var c config + d := time.Date(1986, time.March, 28, 12, 0, 0, 0, time.UTC) + WithTime(d)(&c) + st, err := c.generator() + assert.NoError(t, err) + assert.Equal(t, d.Format(FileTimestamp), st) +} diff --git a/options.go b/options.go index 67d09c1..6f122ce 100644 --- a/options.go +++ b/options.go @@ -2,8 +2,6 @@ package nomino import "strings" -type generator func() (string, error) - type config struct { original string prefix string From 1af608d7c9e95da2464c6d9f8a10f6064b4b7cc1 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Mon, 10 Mar 2025 11:52:55 -0500 Subject: [PATCH 5/8] =?UTF-8?q?=E2=9C=A8=20Add=20UTC=20time-based=20genera?= =?UTF-8?q?tors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generators.go | 10 ++++++++++ generators_test.go | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/generators.go b/generators.go index 403f440..db2bce1 100644 --- a/generators.go +++ b/generators.go @@ -47,3 +47,13 @@ func WithFormattedTime(t time.Time, f string) Option { }) } } + +const FileTimestampNoTZ string = "2006-01-02_03-05-06" + +func WithTimestampUTC() Option { + return WithTimeUTC(time.Now()) +} + +func WithTimeUTC(t time.Time) Option { + return WithFormattedTime(t.UTC(), FileTimestampNoTZ) +} diff --git a/generators_test.go b/generators_test.go index fb3f8c3..6f4cc2d 100644 --- a/generators_test.go +++ b/generators_test.go @@ -50,3 +50,12 @@ func TestWithTime(t *testing.T) { assert.NoError(t, err) assert.Equal(t, d.Format(FileTimestamp), st) } + +func TestWithTimestampUTC(t *testing.T) { + var c config + WithTimestampUTC()(&c) + n := time.Now() + st, err := c.generator() + assert.NoError(t, err) + assert.Equal(t, n.UTC().Format(FileTimestampNoTZ), st) +} From abe7acffd453eb312613d8611780ae7a37c9d5f9 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Mon, 10 Mar 2025 13:46:13 -0500 Subject: [PATCH 6/8] =?UTF-8?q?=F0=9F=92=A1=20Document=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generators.go | 11 +++++++++++ options.go | 7 +++++++ 2 files changed, 18 insertions(+) diff --git a/generators.go b/generators.go index db2bce1..682c405 100644 --- a/generators.go +++ b/generators.go @@ -20,26 +20,34 @@ func uuidGen() (string, error) { return u.String(), nil } +// WithUUID sets a generator that creates a UUIDv4 func WithUUID() Option { return func(c *config) { setGenerator(c, uuidGen) } } +// FileTimestamp is the default format for WithTimestamp and WithTime const FileTimestamp string = "2006-01-02_03-05-06-0700" +// WithTimestamp sets a generator that creates a date and time for the current time. +// The format is FileStamp func WithTimestamp() Option { return WithTimestampFormat(FileTimestamp) } +// WithTimestampFormat sets a generator the creates a date and time for the current time with the supplied format. func WithTimestampFormat(f string) Option { return WithFormattedTime(time.Now(), f) } +// WithTime sets a generator that creates a date and time for the supplied time. +// The format is FileStamp func WithTime(t time.Time) Option { return WithFormattedTime(t, FileTimestamp) } +// WithFormattedTime sets a generator that creates a date and time for the supplied time with the supplied format. func WithFormattedTime(t time.Time, f string) Option { return func(c *config) { setGenerator(c, func() (string, error) { @@ -48,12 +56,15 @@ func WithFormattedTime(t time.Time, f string) Option { } } +// FileTimestamp is the default format for WithTimestampUTC and WithTimeUTC const FileTimestampNoTZ string = "2006-01-02_03-05-06" +// WithTimestampUTC sets a generator the creates a date and time for the current time in UTC without a timezone in the format. func WithTimestampUTC() Option { return WithTimeUTC(time.Now()) } +// WithTimeUTC sets a generate that creates a date and time for the supplied time in UTC without a timezone in the format. func WithTimeUTC(t time.Time) Option { return WithFormattedTime(t.UTC(), FileTimestampNoTZ) } diff --git a/options.go b/options.go index 6f122ce..e318446 100644 --- a/options.go +++ b/options.go @@ -17,32 +17,39 @@ func defaultConf() config { } } +// Option is an option for nomino type Option func(c *config) +// WithOriginal sets the original filename. +// This will be included in the generated name after the generated string and before the suffix. func WithOriginal(o string) Option { return func(c *config) { c.original = "_" + o } } +// WithPrefix sets a prefix for the generated name. func WithPrefix(p string) Option { return func(c *config) { c.prefix = p + "_" } } +// WithSuffix sets a suffix for the generated name. It will be included in the base name before the suffix. func WithSuffix(s string) Option { return func(c *config) { c.suffix = "_" + s } } +// WithoutExtension sets no extension for the generated filename. By default, it will be txt func WithoutExtension() Option { return func(c *config) { c.extension = "" } } +// WithExtension sets the extension for the generated filename. func WithExtension(ext string) Option { return func(c *config) { c.extension = "." + strings.TrimPrefix(ext, ".") From ce5f823d6401476c1d699fe716d6be65310ab37a Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Mon, 10 Mar 2025 13:48:11 -0500 Subject: [PATCH 7/8] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Export=20Generator,=20?= =?UTF-8?q?and=20switch=20to=20WithGenerator=20Option?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generators.go | 64 ++++++++++++++++++++++++---------------------- generators_test.go | 37 ++++++++++++++------------- options.go | 2 +- 3 files changed, 53 insertions(+), 50 deletions(-) diff --git a/generators.go b/generators.go index 682c405..01242fe 100644 --- a/generators.go +++ b/generators.go @@ -6,10 +6,16 @@ import ( "github.com/google/uuid" ) -type generator func() (string, error) +// Generator is a function that returns the "random" portion of the returned filename. +// Technically, it doesn't necessarily need to be random, and could be based on time, or a counter, +// for example. +type Generator func() (string, error) -func setGenerator(c *config, g generator) { - c.generator = g +// WithGenerator sets the specified generator +func WithGenerator(g Generator) Option { + return func(c *config) { + c.generator = g + } } func uuidGen() (string, error) { @@ -20,51 +26,47 @@ func uuidGen() (string, error) { return u.String(), nil } -// WithUUID sets a generator that creates a UUIDv4 -func WithUUID() Option { - return func(c *config) { - setGenerator(c, uuidGen) - } +// UUID generates a UUIDv4. +func UUID() Generator { + return uuidGen } // FileTimestamp is the default format for WithTimestamp and WithTime const FileTimestamp string = "2006-01-02_03-05-06-0700" -// WithTimestamp sets a generator that creates a date and time for the current time. -// The format is FileStamp -func WithTimestamp() Option { - return WithTimestampFormat(FileTimestamp) +// Timestamp generates a a date and time for the current time. +// It is formatted accourding to FileTimestamp +func Timestamp() Generator { + return TimestampWithFormat(FileTimestamp) } -// WithTimestampFormat sets a generator the creates a date and time for the current time with the supplied format. -func WithTimestampFormat(f string) Option { - return WithFormattedTime(time.Now(), f) +// TimestampWithFormat generates a date and time for the current time with the supplied format. +func TimestampWithFormat(f string) Generator { + return FormattedTime(time.Now(), FileTimestamp) } -// WithTime sets a generator that creates a date and time for the supplied time. -// The format is FileStamp -func WithTime(t time.Time) Option { - return WithFormattedTime(t, FileTimestamp) +// Time generates a date and time for the supplied time. +// It is formatted accourding to FileTimestamp +func Time(t time.Time) Generator { + return FormattedTime(t, FileTimestamp) } -// WithFormattedTime sets a generator that creates a date and time for the supplied time with the supplied format. -func WithFormattedTime(t time.Time, f string) Option { - return func(c *config) { - setGenerator(c, func() (string, error) { - return t.Format(f), nil - }) +// FormattedTime generates a date and time for the supplied time with the supplied format. +func FormattedTime(t time.Time, f string) Generator { + return func() (string, error) { + return t.Format(f), nil } } // FileTimestamp is the default format for WithTimestampUTC and WithTimeUTC const FileTimestampNoTZ string = "2006-01-02_03-05-06" -// WithTimestampUTC sets a generator the creates a date and time for the current time in UTC without a timezone in the format. -func WithTimestampUTC() Option { - return WithTimeUTC(time.Now()) +// TimestampUTC generates a date and time for the current time in UTC without a timezone in the format. +func TimestampUTC() Generator { + return TimeUTC(time.Now()) } -// WithTimeUTC sets a generate that creates a date and time for the supplied time in UTC without a timezone in the format. -func WithTimeUTC(t time.Time) Option { - return WithFormattedTime(t.UTC(), FileTimestampNoTZ) +// TimeUTC generates a date and time for the supplied time in UTC without a timezone in the format. +func TimeUTC(t time.Time) Generator { + return FormattedTime(t.UTC(), FileTimestampNoTZ) } diff --git a/generators_test.go b/generators_test.go index 6f4cc2d..96759ff 100644 --- a/generators_test.go +++ b/generators_test.go @@ -9,11 +9,18 @@ import ( "github.com/stretchr/testify/assert" ) -func TestWithUUID(t *testing.T) { +func TestWithGenerator(t *testing.T) { + g := func() (string, error) { return "abc", nil } var c config - WithUUID()(&c) + WithGenerator(g)(&c) st, err := c.generator() assert.NoError(t, err) + assert.Equal(t, "abc", st) +} + +func TestUUID(t *testing.T) { + st, err := UUID()() + assert.NoError(t, err) _, parseErr := uuid.Parse(st) assert.NoError(t, parseErr) } @@ -24,38 +31,32 @@ func (badRead) Read([]byte) (int, error) { return 0, errors.New("sorry") } -func TestWithUUIDFail(t *testing.T) { - var c config +func TestUUIDFail(t *testing.T) { uuid.SetRand(badRead{}) defer uuid.SetRand(nil) - WithUUID()(&c) - _, err := c.generator() + + _, err := UUID()() assert.Equal(t, errors.New("sorry"), err) } -func TestWithTimestamp(t *testing.T) { - var c config - WithTimestamp()(&c) +func TestTimestamp(t *testing.T) { n := time.Now() - st, err := c.generator() + st, err := Timestamp()() assert.NoError(t, err) assert.Equal(t, n.Format(FileTimestamp), st) } -func TestWithTime(t *testing.T) { - var c config +func TestTime(t *testing.T) { d := time.Date(1986, time.March, 28, 12, 0, 0, 0, time.UTC) - WithTime(d)(&c) - st, err := c.generator() + + st, err := Time(d)() assert.NoError(t, err) assert.Equal(t, d.Format(FileTimestamp), st) } -func TestWithTimestampUTC(t *testing.T) { - var c config - WithTimestampUTC()(&c) +func TestTimestampUTC(t *testing.T) { n := time.Now() - st, err := c.generator() + st, err := TimestampUTC()() assert.NoError(t, err) assert.Equal(t, n.UTC().Format(FileTimestampNoTZ), st) } diff --git a/options.go b/options.go index e318446..7f93d57 100644 --- a/options.go +++ b/options.go @@ -7,7 +7,7 @@ type config struct { prefix string suffix string extension string - generator generator + generator Generator } func defaultConf() config { From 5c4e66d1443593b1446cd0b9d82be58cdd824cf3 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Mon, 10 Mar 2025 14:25:00 -0500 Subject: [PATCH 8/8] =?UTF-8?q?=E2=9C=A8=20Add=20MultiGenerator?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generators.go | 27 +++++++++++++++++++++++++++ generators_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/generators.go b/generators.go index 01242fe..c409988 100644 --- a/generators.go +++ b/generators.go @@ -1,6 +1,7 @@ package nomino import ( + "errors" "time" "github.com/google/uuid" @@ -18,6 +19,32 @@ func WithGenerator(g Generator) Option { } } +// ErrMissingGenerators is returned by a multi-generator if no generators are supplied. +var ErrMissingGenerators = errors.New("no generators supplied") + +func missingGen() (string, error) { + return "", ErrMissingGenerators +} + +// MultiGeneratorInOrder allows the use of multiple generators. Each new invokation will use the next generator in turn. +// If none are passed, the generator will always return ErrMissingGenerators. +func MultiGeneratorInOrder(gens ...Generator) Generator { + if len(gens) == 0 { + return missingGen + } + + if len(gens) == 1 { + return gens[0] + } + + var idx int + return func() (string, error) { + st, err := gens[idx]() + idx = (idx + 1) % len(gens) + return st, err + } +} + func uuidGen() (string, error) { u, err := uuid.NewRandom() if err != nil { diff --git a/generators_test.go b/generators_test.go index 96759ff..210181f 100644 --- a/generators_test.go +++ b/generators_test.go @@ -18,6 +18,51 @@ func TestWithGenerator(t *testing.T) { assert.Equal(t, "abc", st) } +func TestMultiGeneratorInOrder(t *testing.T) { + st1 := "abc" + st2 := "def" + er1 := errors.New("oops") + g1 := func() (string, error) { return st1, nil } + g2 := func() (string, error) { return st2, nil } + g3 := func() (string, error) { return "", er1 } + g := MultiGeneratorInOrder(g1, g2, g3) + st, err := g() + assert.NoError(t, err) + assert.Equal(t, st1, st) + st, err = g() + assert.NoError(t, err) + assert.Equal(t, st2, st) + st, err = g() + assert.Zero(t, st) + assert.ErrorIs(t, err, er1) + st, err = g() + assert.NoError(t, err) + assert.Equal(t, st1, st) +} + +func TestMultiGeneratorInOrderOne(t *testing.T) { + st1 := "abc" + g1 := func() (string, error) { return st1, nil } + g := MultiGeneratorInOrder(g1) + + st, err := g() + assert.NoError(t, err) + assert.Equal(t, st1, st) + st, err = g() + assert.NoError(t, err) + assert.Equal(t, st1, st) +} + +func TestMultiGeneratorInOrderMissing(t *testing.T) { + g := MultiGeneratorInOrder() + st, err := g() + assert.Zero(t, st) + assert.ErrorIs(t, err, ErrMissingGenerators) + st, err = g() + assert.Zero(t, st) + assert.ErrorIs(t, err, ErrMissingGenerators) +} + func TestUUID(t *testing.T) { st, err := UUID()() assert.NoError(t, err)