From 4f31fd70df200c389fdf78bf0fbdc3fa01160048 Mon Sep 17 00:00:00 2001 From: Evis Drenova Date: Mon, 20 Nov 2023 21:52:09 -0800 Subject: [PATCH] updated random string transformer and tests --- .../benthos/transformers/phone_test.go | 4 +- .../benthos/transformers/random_float_test.go | 2 +- .../benthos/transformers/random_int_test.go | 2 +- .../benthos/transformers/random_string.go | 76 ++++++++++++++----- .../transformers/random_string_test.go | 22 ++++-- .../benthos/transformers/uuid_test.go | 2 +- 6 files changed, 77 insertions(+), 31 deletions(-) diff --git a/worker/internal/benthos/transformers/phone_test.go b/worker/internal/benthos/transformers/phone_test.go index 74cdb97301..15406d96e5 100644 --- a/worker/internal/benthos/transformers/phone_test.go +++ b/worker/internal/benthos/transformers/phone_test.go @@ -106,7 +106,7 @@ func Test_PhoneNumberTransformerWithValue(t *testing.T) { ex, err := bloblang.Parse(mapping) assert.NoError(t, err, "failed to parse the phone transformer") - res, err := ex.Query(testVal) + res, err := ex.Query(nil) assert.NoError(t, err) assert.Len(t, res.(string), len(testVal), "Generated phone number must be the same length as the input phone number") @@ -119,7 +119,7 @@ func Test_PhoneNumberTransformerWithNoValue(t *testing.T) { testVal := "6183849282" - res, err := ex.Query(testVal) + res, err := ex.Query(nil) assert.NoError(t, err) assert.Len(t, res.(string), len(testVal), "Generated phone number must be the same length as the input phone number") diff --git a/worker/internal/benthos/transformers/random_float_test.go b/worker/internal/benthos/transformers/random_float_test.go index b5ca9a6096..c6b25c299f 100644 --- a/worker/internal/benthos/transformers/random_float_test.go +++ b/worker/internal/benthos/transformers/random_float_test.go @@ -48,7 +48,7 @@ func TestRandomFloatTransformerWithValue(t *testing.T) { ex, err := bloblang.Parse(mapping) assert.NoError(t, err, "failed to parse the random float transformer") - res, err := ex.Query(testVal) + res, err := ex.Query(nil) assert.NoError(t, err) assert.Equal(t, GetFloatLength(testVal), GetFloatLength(res.(float64))) // Generated Float must be the same length as the input Float" diff --git a/worker/internal/benthos/transformers/random_int_test.go b/worker/internal/benthos/transformers/random_int_test.go index 091a40db51..60184e6e42 100644 --- a/worker/internal/benthos/transformers/random_int_test.go +++ b/worker/internal/benthos/transformers/random_int_test.go @@ -61,7 +61,7 @@ func TestRandomIntTransformerWithValue(t *testing.T) { ex, err := bloblang.Parse(mapping) assert.NoError(t, err, "failed to parse the random int transformer") - res, err := ex.Query(testVal) + res, err := ex.Query(nil) assert.NoError(t, err) assert.Equal(t, transformer_utils.GetIntLength(testVal), transformer_utils.GetIntLength(res.(int64))) // Generated int must be the same length as the input int" diff --git a/worker/internal/benthos/transformers/random_string.go b/worker/internal/benthos/transformers/random_string.go index 30ec78196b..8776ea3235 100644 --- a/worker/internal/benthos/transformers/random_string.go +++ b/worker/internal/benthos/transformers/random_string.go @@ -13,30 +13,51 @@ const defaultStrLength = 10 func init() { spec := bloblang.NewPluginSpec(). - Param(bloblang.NewBoolParam("preserve_length")). - Param(bloblang.NewInt64Param("str_length")) + Param(bloblang.NewStringParam("value").Optional()). + Param(bloblang.NewBoolParam("preserve_length").Optional()). + Param(bloblang.NewInt64Param("str_length").Optional()) // register the plugin - err := bloblang.RegisterMethodV2("randomstringtransformer", spec, func(args *bloblang.ParsedParams) (bloblang.Method, error) { + err := bloblang.RegisterFunctionV2("randomstringtransformer", spec, func(args *bloblang.ParsedParams) (bloblang.Function, error) { - preserveLength, err := args.GetBool("preserve_length") + valuePtr, err := args.GetOptionalString("value") if err != nil { return nil, err } - strLength, err := args.GetInt64("str_length") + var value string + if valuePtr != nil { + value = *valuePtr + } + + preserveLengthPtr, err := args.GetOptionalBool("preserve_length") + if err != nil { + return nil, err + } + + var preserveLength bool + if preserveLengthPtr != nil { + preserveLength = *preserveLengthPtr + } + + strLengthPtr, err := args.GetOptionalInt64("str_length") if err != nil { return nil, err } + var strLength int64 + if strLengthPtr != nil { + strLength = *strLengthPtr + } + if err != nil { return nil, fmt.Errorf("unable to convert the string case to a defined enum value") } - return bloblang.StringMethod(func(s string) (any, error) { - res, err := GenerateRandomString(s, preserveLength, strLength) + return func() (any, error) { + res, err := GenerateRandomString(value, preserveLength, strLength) return res, err - }), nil + }, nil }) if err != nil { @@ -46,33 +67,46 @@ func init() { } // main transformer logic goes here -func GenerateRandomString(s string, preserveLength bool, strLength int64) (string, error) { +func GenerateRandomString(value string, preserveLength bool, strLength int64) (string, error) { var returnValue string if preserveLength && strLength > 0 { return "", fmt.Errorf("preserve length and int length params cannot both be true") } - if preserveLength { + if value != "" { - val, err := transformer_utils.GenerateRandomStringWithLength(int64(len(s))) + if preserveLength { - if err != nil { - return "", fmt.Errorf("unable to generate a random string with length") - } + val, err := transformer_utils.GenerateRandomStringWithLength(int64(len(value))) - returnValue = val + if err != nil { + return "", fmt.Errorf("unable to generate a random string with length") + } - } else if strLength > 0 { + returnValue = val - val, err := transformer_utils.GenerateRandomStringWithLength(strLength) + } else if strLength > 0 { - if err != nil { - return "", fmt.Errorf("unable to generate a random string with length") - } + val, err := transformer_utils.GenerateRandomStringWithLength(strLength) - returnValue = val + if err != nil { + return "", fmt.Errorf("unable to generate a random string with length") + } + returnValue = val + + } else { + + val, err := transformer_utils.GenerateRandomStringWithLength(defaultStrLength) + + if err != nil { + return "", fmt.Errorf("unable to generate a random string with length") + } + + returnValue = val + + } } else { val, err := transformer_utils.GenerateRandomStringWithLength(defaultStrLength) diff --git a/worker/internal/benthos/transformers/random_string_test.go b/worker/internal/benthos/transformers/random_string_test.go index 87279c2544..b79b560bb8 100644 --- a/worker/internal/benthos/transformers/random_string_test.go +++ b/worker/internal/benthos/transformers/random_string_test.go @@ -1,6 +1,7 @@ package neosync_transformers import ( + "fmt" "testing" "github.com/benthosdev/benthos/v4/public/bloblang" @@ -42,16 +43,27 @@ func TestRandomStringPreserveLengthFalseStrLength(t *testing.T) { } -func TestRandomStringTransformer(t *testing.T) { - mapping := `root = this.randomstringtransformer(true, -1)` +func TestRandomStringTransformerWithValue(t *testing.T) { + testVal := "testte" + mapping := fmt.Sprintf(`root = randomstringtransformer(%q, false, 6)`, testVal) ex, err := bloblang.Parse(mapping) assert.NoError(t, err, "failed to parse the random string transformer") - testVal := "testte" - - res, err := ex.Query(testVal) + res, err := ex.Query(nil) assert.NoError(t, err) assert.Equal(t, len(testVal), len(res.(string)), "Generated string must be the same length as the input string") assert.IsType(t, res, testVal) } + +func TestRandomStringTransformerWithNoValue(t *testing.T) { + mapping := `root = randomstringtransformer()` + ex, err := bloblang.Parse(mapping) + assert.NoError(t, err, "failed to parse the random string transformer") + + res, err := ex.Query(nil) + assert.NoError(t, err) + + assert.Equal(t, 10, len(res.(string)), "Generated string must be the same length as the input string") + assert.IsType(t, res, "") +} diff --git a/worker/internal/benthos/transformers/uuid_test.go b/worker/internal/benthos/transformers/uuid_test.go index ca9909cc15..6eeba4b48c 100644 --- a/worker/internal/benthos/transformers/uuid_test.go +++ b/worker/internal/benthos/transformers/uuid_test.go @@ -40,7 +40,7 @@ func TestUUIDTransformer(t *testing.T) { ex, err := bloblang.Parse(mapping) assert.NoError(t, err, "failed to parse the uuid transformer") - res, err := ex.Query("test") // input is ignored here + res, err := ex.Query(nil) // input is ignored here assert.NoError(t, err) assert.Len(t, res.(string), 36, "UUIDs with hyphens must have 36 characters")