diff --git a/worker/internal/benthos/transformers/random_float.go b/worker/internal/benthos/transformers/random_float.go index 7e6c838d3e..47b9b2791a 100644 --- a/worker/internal/benthos/transformers/random_float.go +++ b/worker/internal/benthos/transformers/random_float.go @@ -9,9 +9,6 @@ import ( _ "github.com/benthosdev/benthos/v4/public/components/io" ) -const defaultLenBeforeDecimals = 2 -const defaultLenAfterDecimals = 3 - func init() { spec := bloblang.NewPluginSpec(). @@ -38,8 +35,13 @@ func init() { } return bloblang.Float64Method(func(i float64) (any, error) { - res, err := ProcessRandomFloat(i, preserveLength, digitsBeforeDecimal, digitsAfterDecimal) - return res, err + if preserveLength { + res, err := GenerateRandomFloatPreserveLength(i, preserveLength) + return res, err + } else { + res, err := GenerateRandomFloatWithDefinedLength(digitsBeforeDecimal, digitsAfterDecimal) + return res, err + } }), nil }) @@ -49,90 +51,87 @@ func init() { } -// main transformer logic goes here -func ProcessRandomFloat(i float64, preserveLength bool, digitsBeforeDecimal, digitsAfterDecimal int64) (float64, error) { +func GenerateRandomFloatPreserveLength(i float64, preserveLength bool) (float64, error) { var returnValue float64 fLen := GetFloatLength(i) - if digitsBeforeDecimal < 0 || digitsAfterDecimal < 0 { - return 0.0, fmt.Errorf("digitsBefore and digitsAfter must be non-negative") + bd, err := GenerateRandomInt(int64(fLen.DigitsBeforeDecimalLength)) + if err != nil { + return 0, fmt.Errorf("unable to generate a random before digits integer") } - if preserveLength { - - bd, err := GenerateRandomInt(int64(fLen.DigitsBeforeDecimalLength)) - if err != nil { - return 0, fmt.Errorf("unable to generate a random before digits integer") - } - - ad, err := GenerateRandomInt(int64(fLen.DigitsAfterDecimalLength)) + ad, err := GenerateRandomInt(int64(fLen.DigitsAfterDecimalLength)) - for { - if !LastDigitAZero(ad) { - break // Exit the loop when i is greater than or equal to 5 - } - ad, err = GenerateRandomInt(int64(fLen.DigitsAfterDecimalLength)) - - if err != nil { - return 0, fmt.Errorf("unable to generate a random int64 to convert to a float") - } + for { + if !isLastDigitAZero(ad) { + break // Exit the loop when i is greater than or equal to 5 } + ad, err = GenerateRandomInt(int64(fLen.DigitsAfterDecimalLength)) if err != nil { - return 0, fmt.Errorf("unable to generate a random after digits integer") + return 0, fmt.Errorf("unable to generate a random int64 to convert to a float") } + } - combinedStr := fmt.Sprintf("%d.%d", bd, ad) + if err != nil { + return 0, fmt.Errorf("unable to generate a random after digits integer") + } - result, err := strconv.ParseFloat(combinedStr, 64) - if err != nil { - return 0, fmt.Errorf("unable to convert string to float") - } + combinedStr := fmt.Sprintf("%d.%d", bd, ad) - returnValue = result - } else { + result, err := strconv.ParseFloat(combinedStr, 64) + if err != nil { + return 0, fmt.Errorf("unable to convert string to float") + } - bd, err := GenerateRandomInt(int64(defaultLenBeforeDecimals)) - if err != nil { - return 0, fmt.Errorf("unable to generate a random before digits integer") - } + returnValue = result - ad, err := GenerateRandomInt(int64(defaultLenAfterDecimals)) + return returnValue, nil +} - // generate a new number if it ends in a zero so that the trailing zero doesn't get stripped and return - // a value that is shorter than what the user asks for. This happens in the when we convert the string to a float64 - for { - if !LastDigitAZero(ad) { - break // Exit the loop when i is greater than or equal to 5 - } - ad, err = GenerateRandomInt(int64(fLen.DigitsAfterDecimalLength)) +func GenerateRandomFloatWithDefinedLength(digitsBeforeDecimal, digitsAfterDecimal int64) (float64, error) { - if err != nil { - return 0, fmt.Errorf("unable to generate a random int64 to convert to a float") - } - } + var returnValue float64 - if err != nil { - return 0, fmt.Errorf("unable to generate a random after digits integer") + bd, err := GenerateRandomInt(digitsBeforeDecimal) + if err != nil { + return 0, fmt.Errorf("unable to generate a random before digits integer") + } + + ad, err := GenerateRandomInt(digitsAfterDecimal) + + // generate a new number if it ends in a zero so that the trailing zero doesn't get stripped and return + // a value that is shorter than what the user asks for. This happens in when we convert the string to a float64 + for { + if !isLastDigitAZero(ad) { + break // Exit the loop when i is greater than or equal to 5 } + ad, err = GenerateRandomInt(digitsAfterDecimal) if err != nil { - return 0, fmt.Errorf("unable to generate a random string with length") + return 0, fmt.Errorf("unable to generate a random int64 to convert to a float") } + } - combinedStr := fmt.Sprintf("%d.%d", bd, ad) + if err != nil { + return 0, fmt.Errorf("unable to generate a random after digits integer") + } - result, err := strconv.ParseFloat(combinedStr, 64) - if err != nil { - return 0, fmt.Errorf("unable to convert string to float") - } + if err != nil { + return 0, fmt.Errorf("unable to generate a random string with length") + } - returnValue = result + combinedStr := fmt.Sprintf("%d.%d", bd, ad) + result, err := strconv.ParseFloat(combinedStr, 64) + if err != nil { + return 0, fmt.Errorf("unable to convert string to float") } + returnValue = result + return returnValue, nil } @@ -153,7 +152,7 @@ func GetFloatLength(i float64) *FloatLength { } } -func LastDigitAZero(n int64) bool { +func isLastDigitAZero(n int64) bool { // Convert the int64 to a string str := strconv.FormatInt(n, 10) diff --git a/worker/internal/benthos/transformers/random_float_test.go b/worker/internal/benthos/transformers/random_float_test.go index 1769f10ac9..09f3aa7926 100644 --- a/worker/internal/benthos/transformers/random_float_test.go +++ b/worker/internal/benthos/transformers/random_float_test.go @@ -12,7 +12,7 @@ func TestProcessRandomFloatPreserveLength(t *testing.T) { val := float64(6754.3543) expectedLength := 8 - res, err := ProcessRandomFloat(val, true, int64(3), int64(3)) + res, err := GenerateRandomFloatPreserveLength(val, true) actual := GetFloatLength(res).DigitsBeforeDecimalLength + GetFloatLength(res).DigitsAfterDecimalLength @@ -21,18 +21,16 @@ func TestProcessRandomFloatPreserveLength(t *testing.T) { } -//nolint -// func TestProcessRandomFloatPreserveLengthFalse(t *testing.T) { +func TestProcessRandomFloatPreserveLengthFalse(t *testing.T) { -// val := float64(6754.3543) -// expectedLength := 5 + expectedLength := 6 -// res, err := ProcessRandomFloat(val, false, int64(3), int64(3)) + res, err := GenerateRandomFloatWithDefinedLength(int64(3), int64(3)) -// actual := GetFloatLength(res).DigitsAfterDecimalLength + GetFloatLength(res).DigitsBeforeDecimalLength -// assert.NoError(t, err) -// assert.Equal(t, actual, expectedLength, "The output Float needs to be the same length as the input Float") -// } + actual := GetFloatLength(res).DigitsAfterDecimalLength + GetFloatLength(res).DigitsBeforeDecimalLength + assert.NoError(t, err) + assert.Equal(t, actual, expectedLength, "The length of the output float needs to match the digits before + the digits after") +} func TestRandomFloatTransformer(t *testing.T) { mapping := `root = this.randomfloattransformer(true, 2,3)`