✨ Add MultiGenerator
This commit is contained in:
parent
ce5f823d64
commit
5c4e66d144
2 changed files with 72 additions and 0 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package nomino
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
|
@ -18,6 +19,32 @@ 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) {
|
||||
return "", ErrMissingGenerators
|
||||
}
|
||||
|
||||
// MultiGeneratorInOrder allows the use of multiple generators. Each new invokation will use the next generator in turn.
|
||||
// If none are passed, the generator will always return ErrMissingGenerators.
|
||||
func MultiGeneratorInOrder(gens ...Generator) Generator {
|
||||
if len(gens) == 0 {
|
||||
return missingGen
|
||||
}
|
||||
|
||||
if len(gens) == 1 {
|
||||
return gens[0]
|
||||
}
|
||||
|
||||
var idx int
|
||||
return func() (string, error) {
|
||||
st, err := gens[idx]()
|
||||
idx = (idx + 1) % len(gens)
|
||||
return st, err
|
||||
}
|
||||
}
|
||||
|
||||
func uuidGen() (string, error) {
|
||||
u, err := uuid.NewRandom()
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue