Parameterize genString by length

This commit is contained in:
Brandon Dyck 2020-04-01 00:25:01 -06:00
parent 9dfb70279f
commit e9e997c405
2 changed files with 8 additions and 6 deletions

View File

@ -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

View File

@ -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
}