Skip to content

Commit

Permalink
first name update
Browse files Browse the repository at this point in the history
  • Loading branch information
evisdrenova committed Nov 20, 2023
1 parent 009009e commit e1140e2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 76 deletions.
2 changes: 0 additions & 2 deletions worker/internal/benthos/transformers/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ func init() {

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

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

email, err := args.GetString("email")
Expand Down Expand Up @@ -47,7 +46,6 @@ func init() {

}

// generates a random email address
func GenerateEmail(email string, preserveLength, preserveDomain bool) (string, error) {

var returnValue string
Expand Down
31 changes: 20 additions & 11 deletions worker/internal/benthos/transformers/firstName.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,25 @@ var (
func init() {

spec := bloblang.NewPluginSpec().
Param(bloblang.NewBoolParam("preserve_length"))
Param(bloblang.NewStringParam(("name"))).Param(bloblang.NewBoolParam("preserve_length"))

// register the plugin
err := bloblang.RegisterMethodV2("firstnametransformer", spec, func(args *bloblang.ParsedParams) (bloblang.Method, error) {
err := bloblang.RegisterFunctionV2("firstnametransformer", spec, func(args *bloblang.ParsedParams) (bloblang.Function, error) {

name, err := args.GetString("name")
if err != nil {
return nil, err
}

Check warning on line 37 in worker/internal/benthos/transformers/firstName.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/firstName.go#L36-L37

Added lines #L36 - L37 were not covered by tests

preserveLength, err := args.GetBool("preserve_length")
if err != nil {
return nil, err
}
return bloblang.StringMethod(func(s string) (any, error) {
res, err := GenerateFirstName(s, preserveLength)
return func() (any, error) {
res, err := GenerateFirstName(name, preserveLength)
return res, err
}), nil
}, nil

})

if err != nil {
Expand All @@ -50,11 +56,16 @@ func init() {
// Generates a random first name
func GenerateFirstName(name string, preserveLength bool) (string, error) {

if !preserveLength {
res, err := GenerateFirstNameWithRandomLength()
return res, err
if name != "" {
if !preserveLength {
res, err := GenerateFirstNameWithRandomLength()
return res, err

Check warning on line 62 in worker/internal/benthos/transformers/firstName.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/firstName.go#L61-L62

Added lines #L61 - L62 were not covered by tests
} else {
res, err := GenerateFirstNameWithLength(name)
return res, err
}
} else {
res, err := GenerateFirstNameWithLength(name)
res, err := GenerateFirstNameWithRandomLength()
return res, err
}
}
Expand All @@ -72,7 +83,6 @@ func GenerateFirstNameWithRandomLength() (string, error) {

names := data.Names

// get a random length from the first_names.json file
var nameLengths []int

for _, v := range names {
Expand Down Expand Up @@ -107,7 +117,6 @@ func GenerateFirstNameWithRandomLength() (string, error) {
return returnValue, nil
}

// main transformer logic goes here
func GenerateFirstNameWithLength(fn string) (string, error) {

var returnValue string
Expand Down
23 changes: 18 additions & 5 deletions worker/internal/benthos/transformers/firstName_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package neosync_transformers

import (
"fmt"
"testing"

"github.com/benthosdev/benthos/v4/public/bloblang"
Expand Down Expand Up @@ -39,15 +40,27 @@ func TestGenerateFirstNamePreserveLengthFalse(t *testing.T) {
assert.IsType(t, "", res, "The first name should be a string") // Check if the result is a string
}

func TestFirstNameTransformer(t *testing.T) {
mapping := `root = this.firstnametransformer(true)`
func TestFirstNameTransformerWithValue(t *testing.T) {
testVal := "bill"
mapping := fmt.Sprintf(`root = firstnametransformer(%q,true)`, testVal)
ex, err := bloblang.Parse(mapping)
assert.NoError(t, err, "failed to parse the first name transformer")

testVal := "bill"

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

assert.Len(t, res.(string), len(testVal), "Generated first name must be as long as input first name")
}

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

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

assert.IsType(t, res.(string), "", "Generated first name must be a string")
assert.NotEmpty(t, res.(string))
}
60 changes: 2 additions & 58 deletions worker/pkg/workflows/datasync/activities/activities.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,75 +567,19 @@ root.{destination_col} = transformerfunction(args)
func computeMutationFunction(col *mgmtv1alpha1.JobMapping) (string, error) {

switch col.Transformer.Value {
case "latitude":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
case "longitude":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
case "date":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
case "time_string":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
case "month_name":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
case "year_string":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
case "day_of_week":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
case "day_of_month":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
case "century":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
case "timezone":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
case "time_period":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
case "email":
pd := col.Transformer.Config.GetEmailConfig().PreserveDomain
pl := col.Transformer.Config.GetEmailConfig().PreserveLength
return fmt.Sprintf("emailtransformer(%s,%t, %t)", col.Transformer.Value, pd, pl), nil
case "mac_address":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
case "domain_name":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
case "url":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
case "username":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
case "ipv4":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
case "ipv6":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
case "password":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
case "jwt":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
case "word":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
case "sentence":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
case "paragraph":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
case "title_male":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
case "title_female":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
return fmt.Sprintf("emailtransformer(%s,%t, %t)", col.Column, pd, pl), nil

Check warning on line 573 in worker/pkg/workflows/datasync/activities/activities.go

View check run for this annotation

Codecov / codecov/patch

worker/pkg/workflows/datasync/activities/activities.go#L573

Added line #L573 was not covered by tests
case "first_name":
pl := col.Transformer.Config.GetFirstNameConfig().PreserveLength
return fmt.Sprintf("this.%s.firstnametransformer(%t)", col.Column, pl), nil
case "first_name_female":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
return fmt.Sprintf("firstnametransformer(%s,%t)", col.Column, pl), nil

Check warning on line 576 in worker/pkg/workflows/datasync/activities/activities.go

View check run for this annotation

Codecov / codecov/patch

worker/pkg/workflows/datasync/activities/activities.go#L576

Added line #L576 was not covered by tests
case "last_name":
pl := col.Transformer.Config.GetLastNameConfig().PreserveLength
return fmt.Sprintf("this.%s.lastnametransformer(%t)", col.Column, pl), nil
case "full_name":
pl := col.Transformer.Config.GetFullNameConfig().PreserveLength
return fmt.Sprintf("this.%s.fullnametransformer(%t)", col.Column, pl), nil
case "chinese_first_name":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
case "chinese_last_name":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
case "chinese_name":
return fmt.Sprintf("fake(%q)", col.Transformer.Value), nil
case "phone_number":
pl := col.Transformer.Config.GetPhoneNumberConfig().PreserveLength
ef := col.Transformer.Config.GetPhoneNumberConfig().E164Format
Expand Down

0 comments on commit e1140e2

Please sign in to comment.