Skip to content

Commit

Permalink
updated email transformer to have optional params and updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
evisdrenova committed Nov 20, 2023
1 parent 873f77f commit b1f72d4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 22 deletions.
3 changes: 2 additions & 1 deletion worker/internal/benthos/transformers/card_number.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,13 @@ func GenerateValidLuhnCheckCardNumber() (int64, error) {
// append the checksum to the card number
cardNum = append(cardNum, checkSum)

// convert the card number to a string
// convert the card number to a string to build the card number format
cardNumStr := ""
for _, d := range cardNum {
cardNumStr += fmt.Sprintf("%d", d)
}

// convert the card number back to an int to return it
cardNumInt, err := strconv.ParseInt(cardNumStr, 10, 64)
if err != nil {
return 0, err
Expand Down
30 changes: 26 additions & 4 deletions worker/internal/benthos/transformers/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,48 @@ var tld = []string{"com", "org", "net", "edu", "gov", "app", "dev"}

func init() {

spec := bloblang.NewPluginSpec().Param(bloblang.NewStringParam("email")).Param(bloblang.NewBoolParam("preserve_length")).Param(bloblang.NewBoolParam("preserve_domain"))
spec := bloblang.NewPluginSpec().
Param(bloblang.NewStringParam("email").Optional()).
Param(bloblang.NewBoolParam("preserve_length").Optional()).
Param(bloblang.NewBoolParam("preserve_domain").Optional())

err := bloblang.RegisterFunctionV2("emailtransformer", spec, func(args *bloblang.ParsedParams) (bloblang.Function, error) {

email, err := args.GetString("email")
// optional params return a pointer

emailPtr, err := args.GetOptionalString("email")
if err != nil {
return nil, err
}

Check warning on line 29 in worker/internal/benthos/transformers/email.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/email.go#L28-L29

Added lines #L28 - L29 were not covered by tests

preserveLength, err := args.GetBool("preserve_length")
// check if pointer is nil aka the param is not set
var email string
if emailPtr != nil {
email = *emailPtr
}

preserveLengthPtr, err := args.GetOptionalBool("preserve_length")
if err != nil {
return nil, err
}

preserveDomain, err := args.GetBool("preserve_domain")
var preserveLength bool
if preserveLengthPtr != nil {
preserveLength = *preserveLengthPtr
}

preserveDomainPtr, err := args.GetOptionalBool("preserve_domain")
if err != nil {
return nil, err
}

var preserveDomain bool
if preserveDomainPtr != nil {
preserveDomain = *preserveDomainPtr
}

return func() (any, error) {

res, err := GenerateEmail(email, preserveLength, preserveDomain)
return res, err
}, nil
Expand Down
45 changes: 28 additions & 17 deletions worker/internal/benthos/transformers/email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ func Test_EmailTransformerWithValue(t *testing.T) {
testVal := "[email protected]"
mapping := fmt.Sprintf(`root = emailtransformer(%q,true,true)`, testVal)
ex, err := bloblang.Parse(mapping)
assert.NoError(t, err)
assert.NoError(t, err, "failed to parse the email transformer")

res, err := ex.Query(nil)
Expand All @@ -103,10 +102,8 @@ func Test_EmailTransformerWithValue(t *testing.T) {
}

func Test_EmailTransformerWithEmptyValue(t *testing.T) {
testVal := ""
mapping := fmt.Sprintf(`root = emailtransformer(%q,true,true)`, testVal)
mapping := `root = emailtransformer()`
ex, err := bloblang.Parse(mapping)
assert.NoError(t, err)
assert.NoError(t, err, "failed to parse the email transformer")

res, err := ex.Query(nil)
Expand All @@ -115,24 +112,38 @@ func Test_EmailTransformerWithEmptyValue(t *testing.T) {
assert.Equal(t, true, isValidEmail(res.(string)))
}

func Test_EmailTransformerEmailParamError(t *testing.T) {
mapping := `root = emailtransformer(,true,true)`
_, err := bloblang.Parse(mapping)
assert.Error(t, err, "failed to parse the email transformer, missing param")
// testing that even if some params are passed, as long as the email param is passed, it'll generate the right value
func Test_EmailTransformerWithMissingParams(t *testing.T) {
testVal := "[email protected]"
mapping := fmt.Sprintf(`root = emailtransformer(%q,true)`, testVal)
ex, err := bloblang.Parse(mapping)
assert.NoError(t, err, "failed to parse the email transformer")

res, err := ex.Query(nil)
assert.NoError(t, err)

fmt.Println("val", res)

assert.Equal(t, true, isValidEmail(res.(string)))
}
func Test_EmailTransformerPreserveLengthParamError(t *testing.T) {
testVal := ""
mapping := fmt.Sprintf(`root = emailtransformer(%q,true)`, testVal)
_, err := bloblang.Parse(mapping)
assert.Error(t, err, "failed to parse the email transformer, missing param")

// testing that even if some params are passed, as long as the email param isn't passed, it'll always generate a random value
// Note: that if the args aren't named then it will fail, see the test below
func Test_EmailTransformerWithEmptyValueButSomeNamedParams(t *testing.T) {
mapping := `root = emailtransformer(preserve_length: true,preserve_domain: false)`
ex, err := bloblang.Parse(mapping)
assert.NoError(t, err, "failed to parse the email transformer")

res, err := ex.Query(nil)
assert.NoError(t, err)

assert.Equal(t, true, isValidEmail(res.(string)))
}
func Test_EmailTransformerErrorParams(t *testing.T) {
mapping := `root = emailtransformer(,true,true)`
_, err := bloblang.Parse(mapping)
assert.Error(t, err, "failed to parse the email transformer, missing param")

func Test_EmailTransformerWithEmptyValueButSomeUnnamedParams(t *testing.T) {
mapping := `root = emailtransformer(true, false)`
_, err := bloblang.Parse(mapping)
assert.Error(t, err, "failed to parse the email transformer")
}

func Test_ParseEmailError(t *testing.T) {
Expand Down

0 comments on commit b1f72d4

Please sign in to comment.