From db2f12522dcdc3e791d0d3b5a8d5532b8220f0fc Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Fri, 14 Mar 2025 21:46:30 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20extra=20Options=20to=20Make?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- make.go | 14 ++++++++++---- make_examples_test.go | 17 +++++++++++++++++ make_test.go | 15 +++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/make.go b/make.go index 538c7e0..640ec39 100644 --- a/make.go +++ b/make.go @@ -5,12 +5,21 @@ import "fmt" // Make generates a random filename. The behavior can be controlled by specifying Options // 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) { +func Make(conf Config, opts ...Option) (string, error) { + for _, opt := range opts { + opt(&conf) + } + name, err := conf.generator(&conf) if err != nil { return "", err } + seperateConf(&conf) + return fmt.Sprintf("%s%s%s%s%s", conf.prefix, name, conf.original, conf.suffix, conf.extension), nil +} + +func seperateConf(conf *Config) { if conf.prefix != "" { conf.prefix += conf.separator } @@ -19,8 +28,5 @@ func Make(conf Config) (string, error) { } if conf.suffix != "" { conf.suffix = conf.separator + conf.suffix - } - - return fmt.Sprintf("%s%s%s%s%s", conf.prefix, name, conf.original, conf.suffix, conf.extension), nil } diff --git a/make_examples_test.go b/make_examples_test.go index be09fc5..88b8521 100644 --- a/make_examples_test.go +++ b/make_examples_test.go @@ -11,3 +11,20 @@ func ExampleMake_basic() { out, _ := nomino.Make(nomino.NewConfig()) fmt.Println(out) } + +func ExampleMake_withExtraOptions() { + gen := nomino.Incremental() + conf := nomino.NewConfig( + nomino.WithGenerator(gen), + nomino.WithPrefix("pre"), + ) + + st, _ := nomino.Make(conf, nomino.WithOriginal("foobar")) + fmt.Println(st) + st, _ = nomino.Make(conf, nomino.WithOriginal("baz")) + fmt.Println(st) + + // Output: + // pre_0_foobar.txt + // pre_1_baz.txt +} diff --git a/make_test.go b/make_test.go index ffdecee..0970c48 100644 --- a/make_test.go +++ b/make_test.go @@ -66,3 +66,18 @@ func TestMakeDoesntChangeConf(t *testing.T) { assert.Equal(t, "foo.txt", st) assert.NoError(t, err) } + +func TestMakeOptsDoesntChangeConf(t *testing.T) { + gen := Incremental() + conf := NewConfig(WithGenerator(gen), WithPrefix("pre")) + + st, err := Make(conf, WithOriginal("foobar")) + assert.Equal(t, "", conf.original) + assert.Equal(t, "pre_0_foobar.txt", st) + assert.NoError(t, err) + + st, err = Make(conf, WithOriginal("baz")) + assert.Equal(t, "", conf.original) + assert.Equal(t, "pre_1_baz.txt", st) + assert.NoError(t, err) +}