-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
44 lines (36 loc) · 962 Bytes
/
generator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package generator
import (
"context"
"errors"
"github.com/alexfalkowski/go-service/cache/redis/client"
"github.com/linxGnu/mssqlx"
)
// ErrNotFound for generator.
var ErrNotFound = errors.New("generator not found")
// NewGenerators of identifiers.
func NewGenerators(db *mssqlx.DBs, client client.Client) Generators {
return Generators{
"uuid": &UUID{},
"ksuid": &KSUID{},
"ulid": &ULID{},
"xid": &XID{},
"snowflake": NewSnowflake(),
"nanoid": &NanoID{},
"pg": &PG{db: db},
"redis": &Redis{client: client},
}
}
// Generators of identifiers.
type Generators map[string]Generator
// Generator from kind.
func (gs Generators) Generator(kind string) (Generator, error) {
if g, ok := gs[kind]; ok {
return g, nil
}
return nil, ErrNotFound
}
// Generator to generate an identifier.
type Generator interface {
// Generate an identifier.
Generate(ctx context.Context, name string) (string, error)
}