diff --git a/config_test.go b/config_test.go index adc35e6..aeee9b8 100644 --- a/config_test.go +++ b/config_test.go @@ -10,7 +10,7 @@ import ( func TestNewConf(t *testing.T) { c := NewConfig() assert.Equal(t, ".txt", c.extension) - st, _ := c.generator(&c) + st, _ := c.generator() _, parseErr := uuid.Parse(st) assert.NoError(t, parseErr) } diff --git a/generators.go b/generators.go index e258716..fdbdabd 100644 --- a/generators.go +++ b/generators.go @@ -2,17 +2,15 @@ package nomino import ( "errors" - "strconv" "time" "github.com/google/uuid" - "github.com/gosimple/slug" ) // 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(conf *Config) (string, error) +type Generator func() (string, error) // WithGenerator sets the specified generator func WithGenerator(g Generator) Option { @@ -24,7 +22,7 @@ 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(*Config) (string, error) { +func missingGen() (string, error) { return "", ErrMissingGenerators } @@ -40,14 +38,14 @@ func MultiGeneratorInOrder(gens ...Generator) Generator { } var idx int - return func(c *Config) (string, error) { - st, err := gens[idx](c) + return func() (string, error) { + st, err := gens[idx]() idx = (idx + 1) % len(gens) return st, err } } -func uuidGen(*Config) (string, error) { +func uuidGen() (string, error) { u, err := uuid.NewRandom() if err != nil { return "", err @@ -82,12 +80,12 @@ func Time(t time.Time) Generator { // FormattedTime generates a date and time for the supplied time with the supplied format. func FormattedTime(t time.Time, f string) Generator { - return func(*Config) (string, error) { + return func() (string, error) { return t.Format(f), nil } } -// FileTimestampNoTZ is the default format for WithTimestampUTC and WithTimeUTC +// FileTimestamp is the default format for WithTimestampUTC and WithTimeUTC const FileTimestampNoTZ string = "2006-01-02_03-05-06" // TimestampUTC generates a date and time for the current time in UTC without a timezone in the format. @@ -99,58 +97,3 @@ func TimestampUTC() Generator { func TimeUTC(t time.Time) Generator { return FormattedTime(t.UTC(), FileTimestampNoTZ) } - -// Incremental generates a name that is a series of integers starting at 0 -func Incremental() Generator { - return IncrementalWithStartAndStep(0, 1) -} - -// IncrementalWithStart generates a name that is a series of integers starting at the specified number -func IncrementalWithStart(start int) Generator { - return IncrementalWithStartAndStep(start, 1) -} - -// IncrementalWithStep generates a name that is a series of integers, starting at 0, and increasing the specified number each time -func IncrementalWithStep(step int) Generator { - return IncrementalWithStartAndStep(0, step) -} - -// InrementalWithStartAndStep generates a name that is a series of integers, starting at the specified number, and increasing the specified step each time -func IncrementalWithStartAndStep(start, step int) Generator { - next := start - return func(*Config) (string, error) { - out := strconv.Itoa(next) - next += step - return out, nil - } -} - -// ErrMissingOriginal is the error returned by Slug if there is no filename -var ErrMissingOriginal = errors.New("missing original filename") - -func getOriginal(c *Config) (string, error) { - if c.original == "" { - return "", ErrMissingOriginal - } - name := c.original - c.original = "" - return name, nil -} - -// Slug generates a name from the original filename. -// When this is used, the original filename will be removed from the final filename. -func Slug() Generator { - return func(c *Config) (string, error) { - name, err := getOriginal(c) - return slug.Make(name), err - } -} - -// SlugWithLang generates a name from the original filename, accounting for the given language. -// When this is used, the original filename will be removed from the final filename. -func SlugWithLang(lang string) Generator { - return func(c *Config) (string, error) { - name, err := getOriginal(c) - return slug.MakeLang(name, lang), err - } -} diff --git a/generators_examples_test.go b/generators_examples_test.go index bb9b4a8..67d5995 100644 --- a/generators_examples_test.go +++ b/generators_examples_test.go @@ -3,7 +3,7 @@ package nomino import "fmt" func ExampleWithGenerator_custom_generator() { - gen := func(*Config) (string, error) { + gen := func() (string, error) { return "hello", nil } option := WithGenerator(gen) @@ -18,91 +18,3 @@ func ExampleWithGenerator_custom_generator() { // hello.txt // hello } - -func ExampleIncremental() { - conf := NewConfig(WithPrefix("foo"), WithGenerator(Incremental())) - - str, _ := Make(conf) - fmt.Println(str) - - str, _ = Make(conf) - fmt.Println(str) - - str, _ = Make(conf) - fmt.Println(str) - - // Output: - // foo_0.txt - // foo_1.txt - // foo_2.txt -} - -func ExampleIncrementalWithStart() { - conf := NewConfig(WithPrefix("foo"), WithGenerator(IncrementalWithStart(42))) - - str, _ := Make(conf) - fmt.Println(str) - - str, _ = Make(conf) - fmt.Println(str) - - str, _ = Make(conf) - fmt.Println(str) - - // Output: - // foo_42.txt - // foo_43.txt - // foo_44.txt -} - -func ExampleIncrementalWithStep() { - conf := NewConfig(WithPrefix("foo"), WithGenerator(IncrementalWithStep(2))) - - str, _ := Make(conf) - fmt.Println(str) - - str, _ = Make(conf) - fmt.Println(str) - - str, _ = Make(conf) - fmt.Println(str) - - // Output: - // foo_0.txt - // foo_2.txt - // foo_4.txt -} - -func ExampleIncrementalWithStartAndStep() { - conf := NewConfig(WithPrefix("foo"), WithGenerator(IncrementalWithStartAndStep(42, 2))) - - str, _ := Make(conf) - fmt.Println(str) - - str, _ = Make(conf) - fmt.Println(str) - - str, _ = Make(conf) - fmt.Println(str) - - // Output: - // foo_42.txt - // foo_44.txt - // foo_46.txt -} - -func ExampleSlug() { - conf := NewConfig(WithGenerator(Slug()), WithOriginal("My name is Jimmy")) - str, _ := Make(conf) - fmt.Println(str) - - // Output: my-name-is-jimmy.txt -} - -func ExampleSlugWithLang() { - conf := NewConfig(WithGenerator(SlugWithLang("de")), WithOriginal("Diese & Dass")) - str, _ := Make(conf) - fmt.Println(str) - - // Output: diese-und-dass.txt -} diff --git a/generators_test.go b/generators_test.go index fb28ce7..7282656 100644 --- a/generators_test.go +++ b/generators_test.go @@ -10,10 +10,10 @@ import ( ) func TestWithGenerator(t *testing.T) { - g := func(*Config) (string, error) { return "abc", nil } + g := func() (string, error) { return "abc", nil } var c Config WithGenerator(g)(&c) - st, err := c.generator(&c) + st, err := c.generator() assert.NoError(t, err) assert.Equal(t, "abc", st) } @@ -22,49 +22,49 @@ func TestMultiGeneratorInOrder(t *testing.T) { st1 := "abc" st2 := "def" er1 := errors.New("oops") - g1 := func(*Config) (string, error) { return st1, nil } - g2 := func(*Config) (string, error) { return st2, nil } - g3 := func(*Config) (string, error) { return "", er1 } + 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(nil) + st, err := g() assert.NoError(t, err) assert.Equal(t, st1, st) - st, err = g(nil) + st, err = g() assert.NoError(t, err) assert.Equal(t, st2, st) - st, err = g(nil) + st, err = g() assert.Zero(t, st) assert.ErrorIs(t, err, er1) - st, err = g(nil) + st, err = g() assert.NoError(t, err) assert.Equal(t, st1, st) } func TestMultiGeneratorInOrderOne(t *testing.T) { st1 := "abc" - g1 := func(*Config) (string, error) { return st1, nil } + g1 := func() (string, error) { return st1, nil } g := MultiGeneratorInOrder(g1) - st, err := g(nil) + st, err := g() assert.NoError(t, err) assert.Equal(t, st1, st) - st, err = g(nil) + st, err = g() assert.NoError(t, err) assert.Equal(t, st1, st) } func TestMultiGeneratorInOrderMissing(t *testing.T) { g := MultiGeneratorInOrder() - st, err := g(nil) + st, err := g() assert.Zero(t, st) assert.ErrorIs(t, err, ErrMissingGenerators) - st, err = g(nil) + st, err = g() assert.Zero(t, st) assert.ErrorIs(t, err, ErrMissingGenerators) } func TestUUID(t *testing.T) { - st, err := UUID()(nil) + st, err := UUID()() assert.NoError(t, err) _, parseErr := uuid.Parse(st) assert.NoError(t, parseErr) @@ -80,13 +80,13 @@ func TestUUIDFail(t *testing.T) { uuid.SetRand(badRead{}) defer uuid.SetRand(nil) - _, err := UUID()(nil) + _, err := UUID()() assert.Equal(t, errors.New("sorry"), err) } func TestTimestamp(t *testing.T) { n := time.Now() - st, err := Timestamp()(nil) + st, err := Timestamp()() assert.NoError(t, err) assert.Equal(t, n.Format(FileTimestamp), st) } @@ -94,29 +94,14 @@ func TestTimestamp(t *testing.T) { func TestTime(t *testing.T) { d := time.Date(1986, time.March, 28, 12, 0, 0, 0, time.UTC) - st, err := Time(d)(nil) + st, err := Time(d)() assert.NoError(t, err) assert.Equal(t, d.Format(FileTimestamp), st) } func TestTimestampUTC(t *testing.T) { n := time.Now() - st, err := TimestampUTC()(nil) + st, err := TimestampUTC()() assert.NoError(t, err) assert.Equal(t, n.UTC().Format(FileTimestampNoTZ), st) } - -func TestSlugMissingFilename(t *testing.T) { - conf := NewConfig(WithGenerator(Slug())) - st, err := conf.generator(&conf) - assert.Zero(t, st) - assert.ErrorIs(t, err, ErrMissingOriginal) -} - -func TestSlugRemovesOriginal(t *testing.T) { - conf := NewConfig(WithGenerator(Slug()), WithOriginal("Hello, World")) - st, err := conf.generator(&conf) - assert.Zero(t, conf.original) - assert.Equal(t, "hello-world", st) - assert.NoError(t, err) -} diff --git a/go.mod b/go.mod index 6917d49..de6af60 100644 --- a/go.mod +++ b/go.mod @@ -4,13 +4,11 @@ go 1.23.6 require ( github.com/google/uuid v1.6.0 - github.com/gosimple/slug v1.15.0 github.com/stretchr/testify v1.10.0 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect - github.com/gosimple/unidecode v1.0.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 index 8638c59..14c872b 100644 --- a/go.sum +++ b/go.sum @@ -2,10 +2,6 @@ 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/gosimple/slug v1.15.0 h1:wRZHsRrRcs6b0XnxMUBM6WK1U1Vg5B0R7VkIf1Xzobo= -github.com/gosimple/slug v1.15.0/go.mod h1:UiRaFH+GEilHstLUmcBgWcI42viBN7mAb818JrYOeFQ= -github.com/gosimple/unidecode v1.0.1 h1:hZzFTMMqSswvf0LBJZCZgThIZrpDHFXux9KeGmn6T/o= -github.com/gosimple/unidecode v1.0.1/go.mod h1:CP0Cr1Y1kogOtx0bJblKzsVWrqYaqfNOnHzpgWw4Awc= 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/make.go b/make.go index c2c222c..3d15b4a 100644 --- a/make.go +++ b/make.go @@ -6,7 +6,7 @@ import "fmt" // In general, the final filename will be [prefix]_[generated_string]_[original_filename]_[suffix].[extension]. // If the name generator returns an error (generally, it shouldn't), that will be returned instead. func Make(conf Config) (string, error) { - name, err := conf.generator(&conf) + name, err := conf.generator() if err != nil { return "", err } diff --git a/make_test.go b/make_test.go index 28096ce..dffc981 100644 --- a/make_test.go +++ b/make_test.go @@ -8,7 +8,7 @@ import ( ) func TestMake(t *testing.T) { - genOpt := WithGenerator(func(*Config) (string, error) { return "abc", nil }) + genOpt := WithGenerator(func() (string, error) { return "abc", nil }) testcases := []struct { name string opts []Option @@ -47,20 +47,8 @@ func TestMake(t *testing.T) { func TestMakeErr(t *testing.T) { retErr := errors.New("oops") - conf := NewConfig(WithGenerator(func(*Config) (string, error) { return "foobar", retErr })) + conf := NewConfig(WithGenerator(func() (string, error) { return "foobar", retErr })) st, err := Make(conf) assert.Zero(t, st) assert.ErrorIs(t, err, retErr) } - -func TestMakeDoesntChangeConf(t *testing.T) { - gen := func(c *Config) (string, error) { - c.original = "" - return "foo", nil - } - conf := NewConfig(WithGenerator(gen), WithOriginal("foobar")) - st, err := Make(conf) - assert.Equal(t, "foobar", conf.original) - assert.Equal(t, "foo.txt", st) - assert.NoError(t, err) -} diff --git a/options.go b/options.go index c7e0338..b51b756 100644 --- a/options.go +++ b/options.go @@ -1,10 +1,6 @@ package nomino -import ( - "strings" - - "github.com/gosimple/slug" -) +import "strings" // Option sets configuration parameters for Config. type Option func(c *Config) @@ -17,22 +13,6 @@ func WithOriginal(o string) Option { } } -// WithOriginal sets the original filename as a slug. -// This should not be used with the Slug Generator (as it would be redundant) -func WithOriginalSlug(o string) Option { - return func(c *Config) { - c.original = slug.Make(o) - } -} - -// WithOriginal sets the original filename as a slug, taking the language into account. -// This should not be used with the Slug Generator (as it would be redundant) -func WithOriginalSlugLang(o, lang string) Option { - return func(c *Config) { - c.original = slug.MakeLang(o, lang) - } -} - // WithPrefix sets a prefix for the generated name. func WithPrefix(p string) Option { return func(c *Config) { diff --git a/options_examples_test.go b/options_examples_test.go deleted file mode 100644 index 0c4e669..0000000 --- a/options_examples_test.go +++ /dev/null @@ -1,23 +0,0 @@ -package nomino - -import "fmt" - -func ExampleWithOriginalSlug() { - st, _ := Make(NewConfig( - WithOriginalSlug("Hello, World"), - WithGenerator(Incremental()), - )) - - fmt.Println(st) - // Output: 0_hello-world.txt -} - -func ExampleWithOriginalSlugLang() { - st, _ := Make(NewConfig( - WithOriginalSlugLang("Diese & Dass", "de"), - WithGenerator(Incremental()), - )) - - fmt.Println(st) - // Output: 0_diese-und-dass.txt -}