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 {