Skip to content

Commit

Permalink
updated phone transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
evisdrenova committed Nov 21, 2023
1 parent b9bde23 commit 919394f
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 52 deletions.
113 changes: 76 additions & 37 deletions worker/internal/benthos/transformers/phone.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,57 @@ import (
func init() {

spec := bloblang.NewPluginSpec().
Param(bloblang.NewBoolParam("preserve_length")).
Param(bloblang.NewBoolParam("e164_format")).
Param(bloblang.NewBoolParam("include_hyphens"))
Param(bloblang.NewStringParam("phone").Optional()).
Param(bloblang.NewBoolParam("preserve_length").Optional()).
Param(bloblang.NewBoolParam("e164_format").Optional()).
Param(bloblang.NewBoolParam("include_hyphens").Optional())

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

preserveLength, err := args.GetBool("preserve_length")
phonePtr, err := args.GetOptionalString("phone")
if err != nil {
return nil, err
}
var phone string
if phonePtr != nil {
phone = *phonePtr
}

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

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

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

Check warning on line 46 in worker/internal/benthos/transformers/phone.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/phone.go#L45-L46

Added lines #L45 - L46 were not covered by tests

var includeHyphens bool
if includeHyphensPtr != nil {
includeHyphens = *includeHyphensPtr
}

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

return bloblang.StringMethod(func(s string) (any, error) {
res, err := GeneratePhoneNumber(s, preserveLength, e164, includeHyphens)
var e164 bool
if e164Ptr != nil {
e164 = *e164Ptr
}

return func() (any, error) {
res, err := GeneratePhoneNumber(phone, preserveLength, e164, includeHyphens)
return res, err
}), nil
}, nil
})

if err != nil {
Expand All @@ -48,46 +73,59 @@ func init() {
}

// generates a random phone number and returns it as a string
func GeneratePhoneNumber(number string, preserveLength, e164Format, includeHyphens bool) (string, error) {
func GeneratePhoneNumber(phone string, preserveLength, e164Format, includeHyphens bool) (string, error) {

if preserveLength && includeHyphens {
return "", fmt.Errorf("the preserve length param cannot be true if the include hyphens is true")
}

if preserveLength && !includeHyphens && !e164Format {
res, err := GeneratePhoneNumberPreserveLengthNoHyphensNotE164(number)
if err != nil {
return "", err
}
if phone != "" {

return res, nil
if preserveLength && !includeHyphens && !e164Format {
res, err := GeneratePhoneNumberPreserveLengthNoHyphensNotE164(phone)
if err != nil {
return "", err
}

Check warning on line 88 in worker/internal/benthos/transformers/phone.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/phone.go#L87-L88

Added lines #L87 - L88 were not covered by tests

} else if !preserveLength && includeHyphens && !e164Format {
// only works with 10 digit-based phone numbers like in the US
res, err := GenerateRandomPhoneNumberWithHyphens()
if err != nil {
return "", err
}
return res, nil

return res, nil
} else if !preserveLength && includeHyphens && !e164Format {
// only works with 10 digit-based phone numbers like in the US
res, err := GenerateRandomPhoneNumberWithHyphens()
if err != nil {
return "", err
}

Check warning on line 97 in worker/internal/benthos/transformers/phone.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/phone.go#L93-L97

Added lines #L93 - L97 were not covered by tests

} else if !preserveLength && !includeHyphens && e164Format {
return res, nil

Check warning on line 99 in worker/internal/benthos/transformers/phone.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/phone.go#L99

Added line #L99 was not covered by tests

/* outputs in e164 format -> for ex. +873104859612, regex: ^\+[1-9]\d{1,14}$ */
res, err := GenerateE164FormatPhoneNumber()
if err != nil {
return "", err
}
} else if !preserveLength && !includeHyphens && e164Format {

return res, nil
} else if e164Format && preserveLength && !includeHyphens {
/* outputs in e164 format -> for ex. +873104859612, regex: ^\+[1-9]\d{1,14}$ */
res, err := GenerateE164FormatPhoneNumber()
if err != nil {
return "", err
}

Check warning on line 107 in worker/internal/benthos/transformers/phone.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/phone.go#L103-L107

Added lines #L103 - L107 were not covered by tests

res, err := GenerateE164FormatPhoneNumberPreserveLength(number)
if err != nil {
return "", err
}
return res, nil

Check warning on line 109 in worker/internal/benthos/transformers/phone.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/phone.go#L109

Added line #L109 was not covered by tests
} else if e164Format && preserveLength && !includeHyphens {

return res, nil
res, err := GenerateE164FormatPhoneNumberPreserveLength(phone)
if err != nil {
return "", err
}

Check warning on line 115 in worker/internal/benthos/transformers/phone.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/phone.go#L112-L115

Added lines #L112 - L115 were not covered by tests

return res, nil

Check warning on line 117 in worker/internal/benthos/transformers/phone.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/phone.go#L117

Added line #L117 was not covered by tests

} else {

res, err := GenerateRandomPhoneNumberWithNoHyphens()
if err != nil {
return "", err
}

Check warning on line 124 in worker/internal/benthos/transformers/phone.go

View check run for this annotation

Codecov / codecov/patch

worker/internal/benthos/transformers/phone.go#L123-L124

Added lines #L123 - L124 were not covered by tests

return res, nil

}

} else {

Expand All @@ -99,6 +137,7 @@ func GeneratePhoneNumber(number string, preserveLength, e164Format, includeHyphe
return res, nil

}

}

// generates a random phone number by presrving the length of the input phone number, removes hyphens and is not in e164 format
Expand Down
42 changes: 27 additions & 15 deletions worker/internal/benthos/transformers/phone_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package neosync_transformers

import (
"fmt"
"strings"
"testing"

"github.com/benthosdev/benthos/v4/public/bloblang"
"github.com/stretchr/testify/assert"
)

func TestGeneratePhoneNumberPreserveLengthTrue(t *testing.T) {
func Test_GeneratePhoneNumberPreserveLengthTrue(t *testing.T) {

pn := "1838492832"
expectedLength := 10
Expand All @@ -20,7 +21,7 @@ func TestGeneratePhoneNumberPreserveLengthTrue(t *testing.T) {

}

func TestGeneratePhoneNumberPreserveLengthTrueHyphens(t *testing.T) {
func Test_GeneratePhoneNumberPreserveLengthTrueHyphens(t *testing.T) {

// we strip the hyphens when we Generate the phone number and the include hyphens param is set to false so the return val will not include hyphens
pn := "183-849-2838"
Expand All @@ -33,7 +34,7 @@ func TestGeneratePhoneNumberPreserveLengthTrueHyphens(t *testing.T) {

}

func TestGeneratePhoneNumberPreserveLengthFalseHyphens(t *testing.T) {
func Test_GeneratePhoneNumberPreserveLengthFalseHyphens(t *testing.T) {

pn := "183-849-2831"

Expand All @@ -44,7 +45,7 @@ func TestGeneratePhoneNumberPreserveLengthFalseHyphens(t *testing.T) {

}

func TestGeneratePhoneNumberPreserveLengthFalseNoHyphens(t *testing.T) {
func Test_GeneratePhoneNumberPreserveLengthFalseNoHyphens(t *testing.T) {

res, err := GenerateRandomPhoneNumberWithNoHyphens()

Expand All @@ -53,7 +54,7 @@ func TestGeneratePhoneNumberPreserveLengthFalseNoHyphens(t *testing.T) {
assert.Equal(t, len(res), 10)
}

func TestGeneratePhoneNumberPreserveLengthFalseIncludeHyphensTrue(t *testing.T) {
func Test_GeneratePhoneNumberPreserveLengthFalseIncludeHyphensTrue(t *testing.T) {

expectedLength := 12

Expand All @@ -64,7 +65,7 @@ func TestGeneratePhoneNumberPreserveLengthFalseIncludeHyphensTrue(t *testing.T)

}

func TestGeneratePhoneNumberPreserveLengthTrueIncludeHyphensTrueError(t *testing.T) {
func Test_GeneratePhoneNumberPreserveLengthTrueIncludeHyphensTrueError(t *testing.T) {

pn := "183-849-2839"
_, err := GeneratePhoneNumber(pn, true, false, true)
Expand All @@ -73,7 +74,7 @@ func TestGeneratePhoneNumberPreserveLengthTrueIncludeHyphensTrueError(t *testing

}

func TestGeneratePhoneNumberE164Format(t *testing.T) {
func Test_GeneratePhoneNumberE164Format(t *testing.T) {

pn := "+2393573894"
expectedLength := 11
Expand All @@ -86,7 +87,7 @@ func TestGeneratePhoneNumberE164Format(t *testing.T) {

}

func TestGeneratePhoneNumberE164FormatPreserveLength(t *testing.T) {
func Test_GeneratePhoneNumberE164FormatPreserveLength(t *testing.T) {

pn := "+2393573894"
expectedLength := 11
Expand All @@ -99,8 +100,20 @@ func TestGeneratePhoneNumberE164FormatPreserveLength(t *testing.T) {

}

func TestPhoneNumberTransformer(t *testing.T) {
mapping := `root = this.phonetransformer(true, false, false)`
func Test_PhoneNumberTransformerWithValue(t *testing.T) {
testVal := "6183849282"
mapping := fmt.Sprintf(`root = phonetransformer(%q, true, false, false)`, testVal)
ex, err := bloblang.Parse(mapping)
assert.NoError(t, err, "failed to parse the phone transformer")

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

assert.Len(t, res.(string), len(testVal), "Generated phone number must be the same length as the input phone number")
}

func Test_PhoneNumberTransformerWithNoValue(t *testing.T) {
mapping := `root = phonetransformer()`
ex, err := bloblang.Parse(mapping)
assert.NoError(t, err, "failed to parse the phone transformer")

Expand All @@ -111,8 +124,7 @@ func TestPhoneNumberTransformer(t *testing.T) {

assert.Len(t, res.(string), len(testVal), "Generated phone number must be the same length as the input phone number")
}

func TestValidateE164True(t *testing.T) {
func Test_ValidateE164True(t *testing.T) {

val := "+6272636472"

Expand All @@ -121,7 +133,7 @@ func TestValidateE164True(t *testing.T) {
assert.Equal(t, res, true, "The e164 number should have a plus sign at the 0th index and be 10 < x < 15 characters long.")
}

func TestValidateE164FalseTooLong(t *testing.T) {
func Test_ValidateE164FalseTooLong(t *testing.T) {

val := "627263647278439"

Expand All @@ -130,7 +142,7 @@ func TestValidateE164FalseTooLong(t *testing.T) {
assert.Equal(t, res, false, "The e164 number should be x < 15 characters long.")
}

func TestValidateE164FalseNoPlusSign(t *testing.T) {
func Test_ValidateE164FalseNoPlusSign(t *testing.T) {

val := "6272636472784"

Expand All @@ -139,7 +151,7 @@ func TestValidateE164FalseNoPlusSign(t *testing.T) {
assert.Equal(t, res, false, "The e164 number should have a plus sign at the beginning.")
}

func TestValidateE164FalseTooshort(t *testing.T) {
func Test_ValidateE164FalseTooshort(t *testing.T) {

val := "627263"

Expand Down

0 comments on commit 919394f

Please sign in to comment.