♻️ Export Generator, and switch to WithGenerator Option
This commit is contained in:
parent
abe7acffd4
commit
ce5f823d64
3 changed files with 53 additions and 50 deletions
|
|
@ -6,10 +6,16 @@ import (
|
||||||
"github.com/google/uuid"
|
"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) {
|
// WithGenerator sets the specified generator
|
||||||
c.generator = g
|
func WithGenerator(g Generator) Option {
|
||||||
|
return func(c *config) {
|
||||||
|
c.generator = g
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func uuidGen() (string, error) {
|
func uuidGen() (string, error) {
|
||||||
|
|
@ -20,51 +26,47 @@ func uuidGen() (string, error) {
|
||||||
return u.String(), nil
|
return u.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithUUID sets a generator that creates a UUIDv4
|
// UUID generates a UUIDv4.
|
||||||
func WithUUID() Option {
|
func UUID() Generator {
|
||||||
return func(c *config) {
|
return uuidGen
|
||||||
setGenerator(c, uuidGen)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FileTimestamp is the default format for WithTimestamp and WithTime
|
// FileTimestamp is the default format for WithTimestamp and WithTime
|
||||||
const FileTimestamp string = "2006-01-02_03-05-06-0700"
|
const FileTimestamp string = "2006-01-02_03-05-06-0700"
|
||||||
|
|
||||||
// WithTimestamp sets a generator that creates a date and time for the current time.
|
// Timestamp generates a a date and time for the current time.
|
||||||
// The format is FileStamp
|
// It is formatted accourding to FileTimestamp
|
||||||
func WithTimestamp() Option {
|
func Timestamp() Generator {
|
||||||
return WithTimestampFormat(FileTimestamp)
|
return TimestampWithFormat(FileTimestamp)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithTimestampFormat sets a generator the creates a date and time for the current time with the supplied format.
|
// TimestampWithFormat generates a date and time for the current time with the supplied format.
|
||||||
func WithTimestampFormat(f string) Option {
|
func TimestampWithFormat(f string) Generator {
|
||||||
return WithFormattedTime(time.Now(), f)
|
return FormattedTime(time.Now(), FileTimestamp)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithTime sets a generator that creates a date and time for the supplied time.
|
// Time generates a date and time for the supplied time.
|
||||||
// The format is FileStamp
|
// It is formatted accourding to FileTimestamp
|
||||||
func WithTime(t time.Time) Option {
|
func Time(t time.Time) Generator {
|
||||||
return WithFormattedTime(t, FileTimestamp)
|
return FormattedTime(t, FileTimestamp)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithFormattedTime sets a generator that creates a date and time for the supplied time with the supplied format.
|
// FormattedTime generates a date and time for the supplied time with the supplied format.
|
||||||
func WithFormattedTime(t time.Time, f string) Option {
|
func FormattedTime(t time.Time, f string) Generator {
|
||||||
return func(c *config) {
|
return func() (string, error) {
|
||||||
setGenerator(c, func() (string, error) {
|
return t.Format(f), nil
|
||||||
return t.Format(f), nil
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// FileTimestamp 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"
|
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.
|
// TimestampUTC generates a date and time for the current time in UTC without a timezone in the format.
|
||||||
func WithTimestampUTC() Option {
|
func TimestampUTC() Generator {
|
||||||
return WithTimeUTC(time.Now())
|
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.
|
// TimeUTC generates a date and time for the supplied time in UTC without a timezone in the format.
|
||||||
func WithTimeUTC(t time.Time) Option {
|
func TimeUTC(t time.Time) Generator {
|
||||||
return WithFormattedTime(t.UTC(), FileTimestampNoTZ)
|
return FormattedTime(t.UTC(), FileTimestampNoTZ)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,11 +9,18 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"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
|
var c config
|
||||||
WithUUID()(&c)
|
WithGenerator(g)(&c)
|
||||||
st, err := c.generator()
|
st, err := c.generator()
|
||||||
assert.NoError(t, err)
|
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)
|
_, parseErr := uuid.Parse(st)
|
||||||
assert.NoError(t, parseErr)
|
assert.NoError(t, parseErr)
|
||||||
}
|
}
|
||||||
|
|
@ -24,38 +31,32 @@ func (badRead) Read([]byte) (int, error) {
|
||||||
return 0, errors.New("sorry")
|
return 0, errors.New("sorry")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWithUUIDFail(t *testing.T) {
|
func TestUUIDFail(t *testing.T) {
|
||||||
var c config
|
|
||||||
uuid.SetRand(badRead{})
|
uuid.SetRand(badRead{})
|
||||||
defer uuid.SetRand(nil)
|
defer uuid.SetRand(nil)
|
||||||
WithUUID()(&c)
|
|
||||||
_, err := c.generator()
|
_, err := UUID()()
|
||||||
assert.Equal(t, errors.New("sorry"), err)
|
assert.Equal(t, errors.New("sorry"), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWithTimestamp(t *testing.T) {
|
func TestTimestamp(t *testing.T) {
|
||||||
var c config
|
|
||||||
WithTimestamp()(&c)
|
|
||||||
n := time.Now()
|
n := time.Now()
|
||||||
st, err := c.generator()
|
st, err := Timestamp()()
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, n.Format(FileTimestamp), st)
|
assert.Equal(t, n.Format(FileTimestamp), st)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWithTime(t *testing.T) {
|
func TestTime(t *testing.T) {
|
||||||
var c config
|
|
||||||
d := time.Date(1986, time.March, 28, 12, 0, 0, 0, time.UTC)
|
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.NoError(t, err)
|
||||||
assert.Equal(t, d.Format(FileTimestamp), st)
|
assert.Equal(t, d.Format(FileTimestamp), st)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWithTimestampUTC(t *testing.T) {
|
func TestTimestampUTC(t *testing.T) {
|
||||||
var c config
|
|
||||||
WithTimestampUTC()(&c)
|
|
||||||
n := time.Now()
|
n := time.Now()
|
||||||
st, err := c.generator()
|
st, err := TimestampUTC()()
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, n.UTC().Format(FileTimestampNoTZ), st)
|
assert.Equal(t, n.UTC().Format(FileTimestampNoTZ), st)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ type config struct {
|
||||||
prefix string
|
prefix string
|
||||||
suffix string
|
suffix string
|
||||||
extension string
|
extension string
|
||||||
generator generator
|
generator Generator
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaultConf() config {
|
func defaultConf() config {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue