From 67f472a2c648cd94b585ee6c2068da2a3485dd4f Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Thu, 13 Mar 2025 15:22:54 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20Incremental*=20Generators?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generators.go | 28 ++++++++++++++- generators_examples_test.go | 72 +++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) diff --git a/generators.go b/generators.go index fdbdabd..7873107 100644 --- a/generators.go +++ b/generators.go @@ -2,6 +2,7 @@ package nomino import ( "errors" + "strconv" "time" "github.com/google/uuid" @@ -85,7 +86,7 @@ func FormattedTime(t time.Time, f string) Generator { } } -// FileTimestamp is the default format for WithTimestampUTC and WithTimeUTC +// FileTimestampNoTZ 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. @@ -97,3 +98,28 @@ 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() (string, error) { + out := strconv.Itoa(next) + next += step + return out, nil + } +} diff --git a/generators_examples_test.go b/generators_examples_test.go index 67d5995..ad7ef4c 100644 --- a/generators_examples_test.go +++ b/generators_examples_test.go @@ -18,3 +18,75 @@ 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 +}