Skip to content

Commit

Permalink
refactor uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
evisdrenova committed Nov 3, 2023
1 parent 2f6f457 commit 1bb3843
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
41 changes: 22 additions & 19 deletions worker/internal/benthos/transformers/uuid.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package neosync_transformers

import (
"fmt"
"strings"

"github.com/google/uuid"

"github.com/benthosdev/benthos/v4/public/bloblang"
_ "github.com/benthosdev/benthos/v4/public/components/io"
"github.com/bxcodec/faker/v4"
)

func init() {
Expand All @@ -12,38 +16,35 @@ func init() {
Param(bloblang.NewBoolParam("include_hyphen"))

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

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

/*we set this to a string method because even though we want to
ignore the input uuid string (because we're changing it)
benthos still makes us handle it or it throws an error that is expecting a string input
so we just ignore it and don't pass it into our ProcessUuid function*/
return bloblang.StringMethod(func(b string) (any, error) {
res, err := ProcessUuid(include_hyphen)
return res, err
}), nil
})
return func() (any, error) {

val, err := GenerateUuid(include_hyphen)

if err != nil {
return false, fmt.Errorf("unable to generate random utc timestamp")
}
return val, nil
}, nil
})
if err != nil {
panic(err)
}

}

// main transformer logic goes here
func ProcessUuid(include_hyphen bool) (string, error) {

var returnValue string
func GenerateUuid(include_hyphen bool) (string, error) {

if include_hyphen {

// generate uuid with hyphens
returnValue = faker.UUIDHyphenated()
return uuid.NewString(), nil

} else {

Expand All @@ -52,8 +53,10 @@ func ProcessUuid(include_hyphen bool) (string, error) {
convert the UUID with no hyphens to having hyphens
so this is more useful for string columns or other dbs that won't do the automatic
conversion if you want don't want your UUIDs to have hyphens on purpose */
returnValue = faker.UUIDDigit()
}

return returnValue, nil
newUUID := uuid.New()
uuidWithHyphens := newUUID.String()
return strings.ReplaceAll(uuidWithHyphens, "-", ""), nil

}
}
6 changes: 3 additions & 3 deletions worker/internal/benthos/transformers/uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func TestProcessUuidPreserveHyphhensTrue(t *testing.T) {

res, err := ProcessUuid(true)
res, err := GenerateUuid(true)

assert.NoError(t, err)
assert.True(t, strings.Contains(res, "-"))
Expand All @@ -21,7 +21,7 @@ func TestProcessUuidPreserveHyphhensTrue(t *testing.T) {

func TestProcessUuidPreserveHyphhensFalse(t *testing.T) {

res, err := ProcessUuid(false)
res, err := GenerateUuid(false)

assert.NoError(t, err)
assert.True(t, isValidUuid(res), "The UUID should have the right format and be valid")
Expand All @@ -36,7 +36,7 @@ func isValidUuid(uuidString string) bool {
}

func TestUUIDTransformer(t *testing.T) {
mapping := `root = this.uuidtransformer(true)`
mapping := `root = uuidtransformer(true)`
ex, err := bloblang.Parse(mapping)
assert.NoError(t, err, "failed to parse the uuid transformer")

Expand Down
2 changes: 1 addition & 1 deletion worker/pkg/workflows/datasync/activities/activities.go
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,7 @@ func computeMutationFunction(col *mgmtv1alpha1.JobMapping) (string, error) {
return fmt.Sprintf("this.%s.intphonetransformer(%t)", col.Column, pl), nil
case "uuid":
ih := col.Transformer.Config.GetUuidConfig().IncludeHyphen
return fmt.Sprintf("this.%s.uuidtransformer(%t)", col.Column, ih), nil
return fmt.Sprintf("uuidtransformer(%t)", ih), nil
case "null":
return "transformernull()", nil
case "random_bool":
Expand Down

0 comments on commit 1bb3843

Please sign in to comment.