🚧 Pass Config to generator

This commit is contained in:
Dan Jones 2025-03-13 15:36:30 -05:00
commit 921020d9fd
6 changed files with 31 additions and 31 deletions

View file

@ -11,7 +11,7 @@ import (
// 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)
type Generator func(conf *Config) (string, error)
// WithGenerator sets the specified generator
func WithGenerator(g Generator) Option {
@ -23,7 +23,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() (string, error) {
func missingGen(*Config) (string, error) {
return "", ErrMissingGenerators
}
@ -39,14 +39,14 @@ func MultiGeneratorInOrder(gens ...Generator) Generator {
}
var idx int
return func() (string, error) {
st, err := gens[idx]()
return func(c *Config) (string, error) {
st, err := gens[idx](c)
idx = (idx + 1) % len(gens)
return st, err
}
}
func uuidGen() (string, error) {
func uuidGen(*Config) (string, error) {
u, err := uuid.NewRandom()
if err != nil {
return "", err
@ -81,7 +81,7 @@ 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() (string, error) {
return func(*Config) (string, error) {
return t.Format(f), nil
}
}
@ -117,7 +117,7 @@ func IncrementalWithStep(step int) Generator {
// 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) {
return func(*Config) (string, error) {
out := strconv.Itoa(next)
next += step
return out, nil