Skip to content

Commit

Permalink
updated phone transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
evisdrenova committed Dec 1, 2023
1 parent 3006bac commit 91a22cb
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 265 deletions.
248 changes: 119 additions & 129 deletions backend/gen/go/protos/mgmt/v1alpha1/transformer.pb.go

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion backend/protos/mgmt/v1alpha1/transformer.proto
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ message GenerateState {}
message GenerateStreetAddress {}

message GenerateStringPhone {
bool e164_format = 1;
bool include_hyphens = 2;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ func (s *Service) GetSystemTransformers(
{Value: string(GenerateStringPhone), Config: &mgmtv1alpha1.TransformerConfig{
Config: &mgmtv1alpha1.TransformerConfig_GenerateStringPhoneConfig{
GenerateStringPhoneConfig: &mgmtv1alpha1.GenerateStringPhone{
E164Format: false,
IncludeHyphens: false,
},
},
Expand Down
3 changes: 0 additions & 3 deletions backend/sql/postgresql/models/transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ type GenerateStateConfig struct{}
type GenerateStreetAddressConfig struct{}

type GenerateStringPhoneConfig struct {
E164Format bool `json:"e164Format"`
IncludeHyphens bool `json:"includeHyphens"`
}

Expand Down Expand Up @@ -242,7 +241,6 @@ func (t *TransformerConfigs) FromTransformerConfigDto(tr *mgmtv1alpha1.Transform
t.GenerateStreetAddress = &GenerateStreetAddressConfig{}
case *mgmtv1alpha1.TransformerConfig_GenerateStringPhoneConfig:
t.GenerateStringPhone = &GenerateStringPhoneConfig{
E164Format: tr.GetGenerateStringPhoneConfig().E164Format,
IncludeHyphens: tr.GetGenerateStringPhoneConfig().IncludeHyphens,
}
case *mgmtv1alpha1.TransformerConfig_GenerateStringConfig:
Expand Down Expand Up @@ -459,7 +457,6 @@ func (t *TransformerConfigs) ToTransformerConfigDto(tr *TransformerConfigs) *mgm
return &mgmtv1alpha1.TransformerConfig{
Config: &mgmtv1alpha1.TransformerConfig_GenerateStringPhoneConfig{
GenerateStringPhoneConfig: &mgmtv1alpha1.GenerateStringPhone{
E164Format: tr.GenerateStringPhone.E164Format,
IncludeHyphens: tr.GenerateStringPhone.IncludeHyphens,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,6 @@ export default function CustomGenerateStringPhoneNumberForm(
</FormItem>
)}
/>
<FormField
name={`config.config.value.e164Format`}
control={fc.control}
render={({ field }) => (
<FormItem className="flex flex-row items-center justify-between rounded-lg border p-3 shadow-sm">
<div className="space-y-0.5">
<FormLabel>Format in E164 Format</FormLabel>
<FormDescription>
Format the output phone number in the E164 Format. For ex.
+1892393573894
</FormDescription>
</div>
<FormControl>
<Switch
checked={field.value}
onCheckedChange={field.onChange}
disabled={
fc.watch('config.config.value.includeHyphens') || isDisabled
}
/>
</FormControl>
</FormItem>
)}
/>
</div>
);
}
38 changes: 1 addition & 37 deletions frontend/app/transformers/Sheetforms/GenerateStringPhoneForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,7 @@ export default function GenerateStringPhoneForm(props: Props): ReactElement {
const [ih, setIh] = useState<boolean>(
config?.includeHyphens ? config?.includeHyphens : false
);
const [e164, setE164] = useState<boolean>(
config?.e164Format ? config?.e164Format : false
);

const handleSubmit = () => {
fc.setValue(
`mappings.${index}.transformer.config.config.value.e164Format`,
e164,
{
shouldValidate: false,
}
);
fc.setValue(
`mappings.${index}.transformer.config.config.value.includeHyphens`,
ih,
Expand All @@ -66,32 +55,7 @@ export default function GenerateStringPhoneForm(props: Props): ReactElement {
</FormDescription>
</div>
<FormControl>
<Switch
checked={ih}
disabled={e164}
onCheckedChange={() => setIh(!ih)}
/>
</FormControl>
</FormItem>
)}
/>
<FormField
name={`mappings.${index}.transformer.config.config.value.e164Format`}
render={() => (
<FormItem className="flex flex-row items-center justify-between rounded-lg border p-3 shadow-sm">
<div className="space-y-0.5">
<FormLabel>Format in E164 Format</FormLabel>
<FormDescription>
Format the output phone number in the E164 Format. For ex.
+1892393573894
</FormDescription>
</div>
<FormControl>
<Switch
checked={e164}
disabled={ih}
onCheckedChange={() => setE164(!e164)}
/>
<Switch checked={ih} onCheckedChange={() => setIh(!ih)} />
</FormControl>
</FormItem>
)}
Expand Down
6 changes: 0 additions & 6 deletions frontend/neosync-api-client/mgmt/v1alpha1/transformer_pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1642,11 +1642,6 @@ export class GenerateStreetAddress extends Message<GenerateStreetAddress> {
* @generated from message mgmt.v1alpha1.GenerateStringPhone
*/
export class GenerateStringPhone extends Message<GenerateStringPhone> {
/**
* @generated from field: bool e164_format = 1;
*/
e164Format = false;

/**
* @generated from field: bool include_hyphens = 2;
*/
Expand All @@ -1660,7 +1655,6 @@ export class GenerateStringPhone extends Message<GenerateStringPhone> {
static readonly runtime: typeof proto3 = proto3;
static readonly typeName = "mgmt.v1alpha1.GenerateStringPhone";
static readonly fields: FieldList = proto3.util.newFieldList(() => [
{ no: 1, name: "e164_format", kind: "scalar", T: 8 /* ScalarType.BOOL */ },
{ no: 2, name: "include_hyphens", kind: "scalar", T: 8 /* ScalarType.BOOL */ },
]);

Expand Down
47 changes: 8 additions & 39 deletions worker/internal/benthos/transformers/generate_string_phone.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package transformers

import (
"errors"
"fmt"
"strconv"

Expand All @@ -13,7 +12,6 @@ import (
func init() {

spec := bloblang.NewPluginSpec().
Param(bloblang.NewBoolParam("e164_format")).
Param(bloblang.NewBoolParam("include_hyphens"))

err := bloblang.RegisterFunctionV2("generate_string_phone", spec, func(args *bloblang.ParsedParams) (bloblang.Function, error) {
Expand All @@ -23,13 +21,8 @@ func init() {
return nil, err
}

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

return func() (any, error) {
res, err := GenerateRandomPhoneNumber(e164, includeHyphens)
res, err := GenerateRandomPhoneNumber(includeHyphens)
return res, err
}, nil
})
Expand All @@ -41,21 +34,19 @@ func init() {
}

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

if e164Format && includeHyphens {
return "", errors.New("E164 phone numbers cannot have hyphens")
}
defaultPhoneLength := 10

if !includeHyphens && !e164Format {
res, err := GenerateRandomPhoneNumberNoHyphens()
if !includeHyphens {
res, err := GenerateRandomPhoneNumberNoHyphens(defaultPhoneLength)
if err != nil {
return "", err
}

return res, nil

} else if includeHyphens && !e164Format {
} else {
// only works with 10 digit-based phone numbers like in the US
res, err := GenerateRandomPhoneNumberHyphens()
if err != nil {
Expand All @@ -64,18 +55,7 @@ func GenerateRandomPhoneNumber(e164Format, includeHyphens bool) (string, error)

return res, nil

} else {

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

return res, nil

}

}

// generates a random phone number with hyphens and returns it as a string
Expand All @@ -95,22 +75,11 @@ func GenerateRandomPhoneNumberHyphens() (string, error) {
return fmt.Sprintf("%03d-%03d-%04d", areaCode, exchange, lineNumber), nil
}

// generates a random E164 phone number between 10 and 15 digits long and returns it as a string
func GenerateRandomE164FormatPhoneNumber() (string, error) {

val, err := transformer_utils.GenerateRandomInt(10)
if err != nil {
return "", nil
}
return fmt.Sprintf("+%d", val), nil

}

// generates a random phone number of length 10 and returns it as a string
func GenerateRandomPhoneNumberNoHyphens() (string, error) {
func GenerateRandomPhoneNumberNoHyphens(length int) (string, error) {

// returns a phone number with no hyphens
val, err := transformer_utils.GenerateRandomInt(10)
val, err := transformer_utils.GenerateRandomInt(length)
if err != nil {
return "", err
}
Expand Down
25 changes: 4 additions & 21 deletions worker/internal/benthos/transformers/generate_string_phone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,25 @@ import (

var testStringPhone = "6183849282"
var testStringPhoneHyphens = "618-384-9282"
var e164TestValue = "+2393573894"

func Test_GeneratePhoneNumberE164Format(t *testing.T) {

res, err := GenerateRandomPhoneNumber(true, false)

assert.NoError(t, err)
assert.Equal(t, ValidateE164(res), ValidateE164(e164TestValue))
assert.Equal(t, len(e164TestValue), len(res), "The length of the output phone number should be the same as the input phone number")
}

func Test_GeneratePhoneNumberHyphens(t *testing.T) {

res, err := GenerateRandomPhoneNumber(false, true)
res, err := GenerateRandomPhoneNumber(true)

assert.NoError(t, err)
assert.Equal(t, len(testStringPhoneHyphens), len(res), "The length of the output phone number should be the same as the input phone number")
}

func Test_GeneratePhoneNumberNoHyphensNoE164(t *testing.T) {
func Test_GeneratePhoneNumberNoHyphens(t *testing.T) {

res, err := GenerateRandomPhoneNumber(false, false)
res, err := GenerateRandomPhoneNumber(false)

assert.NoError(t, err)
assert.Equal(t, len(testStringPhone), len(res), "The length of the output phone number should be the same as the input phone number")
}

func Test_GeneratePhoneNumberHyphensE164(t *testing.T) {

_, err := GenerateRandomPhoneNumber(true, true)

assert.Error(t, err)
}

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

Expand Down
3 changes: 1 addition & 2 deletions worker/pkg/workflows/datasync/activities/activities.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,9 +637,8 @@ func computeMutationFunction(col *mgmtv1alpha1.JobMapping) (string, error) {
case "street_address":
return "generates_street_address()", nil
case "generate_string_phone":
ef := col.Transformer.Config.GetGenerateStringPhoneConfig().E164Format
ih := col.Transformer.Config.GetGenerateStringPhoneConfig().IncludeHyphens
return fmt.Sprintf("generate_string_phone(e164_format:%t,include_hyphens:%t)", ef, ih), nil
return fmt.Sprintf("generate_string_phone(include_hyphens:%t)", ih), nil

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L641 was not covered by tests
case "generate_string":
length := col.Transformer.Config.GetGenerateStringConfig().Length
return fmt.Sprintf(`generate_string(length:%d)`, length), nil
Expand Down

0 comments on commit 91a22cb

Please sign in to comment.