Add options.go

This commit is contained in:
Dan Jones 2025-03-07 16:26:24 -06:00
commit bd8f9ae8a6
4 changed files with 118 additions and 0 deletions

8
go.mod
View file

@ -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
)

10
go.sum Normal file
View file

@ -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=

52
options.go Normal file
View file

@ -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, ".")
}
}

48
options_test.go Normal file
View file

@ -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)
}