Skip to content

Commit

Permalink
NEOS-285: refactor random string tr (#488)
Browse files Browse the repository at this point in the history
  • Loading branch information
evisdrenova authored Nov 2, 2023
1 parent eed9981 commit 2f6f457
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 57 deletions.
8 changes: 4 additions & 4 deletions worker/internal/benthos/transformers/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func GenerateEmailPreserveLength(e string, pl bool) (string, error) {

tld := transformer_utils.SliceString(splitGeneratedDomain[1], len(splitDomain[1]))

un, err := GenerateRandomStringWithLength(int64(len(parsedEmail[0])))
un, err := transformer_utils.GenerateRandomStringWithLength(int64(len(parsedEmail[0])))
if err != nil {
return "", nil
}
Expand All @@ -152,7 +152,7 @@ func GenerateEmailPreserveDomainAndLength(e string, pd, pl bool) (string, error)

unLength := len(parsedEmail[0])

un, err := GenerateRandomStringWithLength(int64(len(parsedEmail[0])))
un, err := transformer_utils.GenerateRandomStringWithLength(int64(len(parsedEmail[0])))
if err != nil {
return "", err
}
Expand All @@ -166,7 +166,7 @@ func GenerateDomain() (string, error) {

var result string

domain, err := GenerateRandomStringWithLength(6)
domain, err := transformer_utils.GenerateRandomStringWithLength(6)

if err != nil {
return "", fmt.Errorf("unable to generate random domain name")
Expand All @@ -190,7 +190,7 @@ func GenerateRandomUsername() (string, error) {
return "", err
}

username, err := GenerateRandomStringWithLength(int64(randLength))
username, err := transformer_utils.GenerateRandomStringWithLength(int64(randLength))
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion worker/internal/benthos/transformers/firstName.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func GenerateFirstNameWithLength(fn string) (string, error) {
}
returnValue = res
} else {
res, err := GenerateRandomStringWithLength(int64(len(fn)))
res, err := transformer_utils.GenerateRandomStringWithLength(int64(len(fn)))
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion worker/internal/benthos/transformers/lastname.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func GenerateLastNameWithLength(fn string) (string, error) {
}
returnValue = res
} else {
res, err := GenerateRandomStringWithLength(int64(len(fn)))
res, err := transformer_utils.GenerateRandomStringWithLength(int64(len(fn)))
if err != nil {
return "", err
}
Expand Down
58 changes: 10 additions & 48 deletions worker/internal/benthos/transformers/random_string.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package neosync_transformers

import (
"crypto/rand"
"fmt"
"math/big"

"github.com/benthosdev/benthos/v4/public/bloblang"
_ "github.com/benthosdev/benthos/v4/public/components/io"
transformer_utils "github.com/nucleuscloud/neosync/worker/internal/benthos/transformers/utils"
)

const defaultStrLength = 10
Expand Down Expand Up @@ -35,7 +34,7 @@ func init() {
}

return bloblang.StringMethod(func(s string) (any, error) {
res, err := ProcessRandomString(s, preserveLength, strLength)
res, err := GenerateRandomString(s, preserveLength, strLength)
return res, err
}), nil
})
Expand All @@ -47,12 +46,16 @@ func init() {
}

// main transformer logic goes here
func ProcessRandomString(s string, preserveLength bool, strLength int64) (string, error) {
func GenerateRandomString(s string, preserveLength bool, strLength int64) (string, error) {
var returnValue string

if preserveLength && strLength > 0 {
return "", fmt.Errorf("preserve length and int length params cannot both be true")
}

if preserveLength {

val, err := GenerateRandomStringWithLength(int64(len(s)))
val, err := transformer_utils.GenerateRandomStringWithLength(int64(len(s)))

if err != nil {
return "", fmt.Errorf("unable to generate a random string with length")
Expand All @@ -62,17 +65,7 @@ func ProcessRandomString(s string, preserveLength bool, strLength int64) (string

} else if strLength > 0 {

val, err := GenerateRandomStringWithLength(strLength)

if err != nil {
return "", fmt.Errorf("unable to generate a random string with length")
}

returnValue = val

} else if preserveLength && strLength > 0 {

val, err := GenerateRandomStringWithLength(strLength)
val, err := transformer_utils.GenerateRandomStringWithLength(strLength)

if err != nil {
return "", fmt.Errorf("unable to generate a random string with length")
Expand All @@ -82,7 +75,7 @@ func ProcessRandomString(s string, preserveLength bool, strLength int64) (string

} else {

val, err := GenerateRandomStringWithLength(defaultStrLength)
val, err := transformer_utils.GenerateRandomStringWithLength(defaultStrLength)

if err != nil {
return "", fmt.Errorf("unable to generate a random string with length")
Expand All @@ -94,34 +87,3 @@ func ProcessRandomString(s string, preserveLength bool, strLength int64) (string

return returnValue, nil
}

func GenerateRandomStringWithLength(l int64) (string, error) {

const alphanumeric = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"

if l <= 0 {
return "", fmt.Errorf("the length cannot be zero or negative")
}

// Create a random source using crypto/rand
source := rand.Reader

// Calculate the max index in the alphabet string
maxIndex := big.NewInt(int64(len(alphanumeric)))

result := make([]byte, l)

for i := int64(0); i < l; i++ {
// Generate a random index in the range [0, len(alphabet))
index, err := rand.Int(source, maxIndex)
if err != nil {
return "", fmt.Errorf("unable to generate a random index for random string generation")
}

// Get the character at the generated index and append it to the result
result[i] = alphanumeric[index.Int64()]
}

return string(result), nil

}
6 changes: 3 additions & 3 deletions worker/internal/benthos/transformers/random_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestRandomStringPreserveLengthTrue(t *testing.T) {
val := "hellothe"
expectedLength := 8

res, err := ProcessRandomString(val, true, -1)
res, err := GenerateRandomString(val, true, -1)

assert.NoError(t, err)
assert.Equal(t, len(res), expectedLength, "The output string should be as long as the input string")
Expand All @@ -23,7 +23,7 @@ func TestRandomStringPreserveLengthFalse(t *testing.T) {
val := "hello"
expectedLength := 10

res, err := ProcessRandomString(val, false, -1)
res, err := GenerateRandomString(val, false, -1)

assert.NoError(t, err)
assert.Equal(t, len(res), expectedLength, "The output string should be a default 10 characters long")
Expand All @@ -35,7 +35,7 @@ func TestRandomStringPreserveLengthFalseStrLength(t *testing.T) {
val := "hello"
expectedLength := 14

res, err := ProcessRandomString(val, false, int64(expectedLength))
res, err := GenerateRandomString(val, false, int64(expectedLength))

assert.NoError(t, err)
assert.Equal(t, len(res), expectedLength, "The output string should be as long as the input string")
Expand Down
32 changes: 32 additions & 0 deletions worker/internal/benthos/transformers/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,35 @@ func IsLastDigitZero(n int64) bool {

return false
}

// generate a random string of length l
func GenerateRandomStringWithLength(l int64) (string, error) {

const alphanumeric = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"

if l <= 0 {
return "", fmt.Errorf("the length cannot be zero or negative")
}

// Create a random source using crypto/rand
source := rand.Reader

// Calculate the max index in the alphabet string
maxIndex := big.NewInt(int64(len(alphanumeric)))

result := make([]byte, l)

for i := int64(0); i < l; i++ {
// Generate a random index in the range [0, len(alphabet))
index, err := rand.Int(source, maxIndex)
if err != nil {
return "", fmt.Errorf("unable to generate a random index for random string generation")
}

// Get the character at the generated index and append it to the result
result[i] = alphanumeric[index.Int64()]
}

return string(result), nil

}
10 changes: 10 additions & 0 deletions worker/internal/benthos/transformers/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,13 @@ func TestIsLastDigitZeroFalse(t *testing.T) {
res := IsLastDigitZero(value)
assert.Equal(t, res, false, "The last digit is not zero.")
}

func TestRandomStringGeneration(t *testing.T) {

expectedLength := 5
res, err := GenerateRandomStringWithLength(int64(expectedLength))

assert.NoError(t, err)
assert.Equal(t, len(res), expectedLength, "The output string should be as long as the input string")

}

0 comments on commit 2f6f457

Please sign in to comment.