From e9e997c405e5657188d6e035fc116f554f31b5c5 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Wed, 1 Apr 2020 00:25:01 -0600 Subject: [PATCH] Parameterize genString by length --- http/server.go | 5 ++--- store.go | 9 ++++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/http/server.go b/http/server.go index 7ed77b6..1bd5cf0 100644 --- a/http/server.go +++ b/http/server.go @@ -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 diff --git a/store.go b/store.go index a04ebe8..c8e9402 100644 --- a/store.go +++ b/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 }