Skip to content

Commit

Permalink
Prevent duplicate emails (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterBooker authored Dec 7, 2023
1 parent 527c5df commit 2721ea1
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions internal/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@ package helpers
import (
"crypto/md5"
"encoding/hex"
"sync"

"github.com/xwb1989/sqlparser"
"syreclabs.com/go/faker"
)

var (
usedEmails map[string]bool
mu sync.Mutex
)

func init() {
usedEmails = make(map[string]bool)
}

// GetFakerFuncs creates a map of faker helper functions.
func GetFakerFuncs() map[string]func(*sqlparser.SQLVal) *sqlparser.SQLVal {
fakerHelpers := map[string]func(*sqlparser.SQLVal) *sqlparser.SQLVal{
Expand Down Expand Up @@ -47,7 +57,19 @@ func generatePassword(value *sqlparser.SQLVal) *sqlparser.SQLVal {
}

func generateEmail(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte(faker.Internet().SafeEmail()))
mu.Lock()
defer mu.Unlock()

var email string
for {
email = faker.Internet().SafeEmail()
if !usedEmails[email] {
usedEmails[email] = true
break
}
}

return sqlparser.NewStrVal([]byte(email))
}

func generatePhoneNumber(value *sqlparser.SQLVal) *sqlparser.SQLVal {
Expand Down Expand Up @@ -119,5 +141,5 @@ func generateShortString(value *sqlparser.SQLVal) *sqlparser.SQLVal {
}

func generateEmptyString(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte(""))
}
return sqlparser.NewStrVal([]byte(""))
}

0 comments on commit 2721ea1

Please sign in to comment.