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