Parameterize genString by length
This commit is contained in:
parent
9dfb70279f
commit
e9e997c405
@ -220,11 +220,10 @@ const keyAdminCode = "admin_code"
|
||||
|
||||
var chars = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
|
||||
func genString() (string, error) {
|
||||
const length = 10
|
||||
func genString(length int) (string, error) {
|
||||
charsLength := big.NewInt(int64(len(chars)))
|
||||
var maxN big.Int
|
||||
maxN.Exp(charsLength, big.NewInt(length), nil)
|
||||
maxN.Exp(charsLength, big.NewInt(int64(length)), nil)
|
||||
n, err := rand.Int(rand.Reader, &maxN)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
9
store.go
9
store.go
@ -11,7 +11,7 @@ import (
|
||||
"crawshaw.io/sqlite/sqlitex"
|
||||
)
|
||||
|
||||
type GenString func() (string, error)
|
||||
type GenString func(length int) (string, error)
|
||||
|
||||
type Store struct {
|
||||
pool *sqlitex.Pool
|
||||
@ -102,15 +102,18 @@ type CreateEventResult struct {
|
||||
const dbDateLayout = "2006-01-02"
|
||||
|
||||
func (s *Store) CreateEvent(ctx context.Context, cmd CreateEventCommand) (result CreateEventResult, err error) {
|
||||
const alphaIDLength = 10
|
||||
const adminCodeLength = 10
|
||||
|
||||
conn := s.pool.Get(ctx)
|
||||
defer s.pool.Put(conn)
|
||||
|
||||
alphaID, err := s.genString()
|
||||
alphaID, err := s.genString(alphaIDLength)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
adminCode, err := s.genString()
|
||||
adminCode, err := s.genString(adminCodeLength)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user