Skip to content

Commit

Permalink
NEOS-283:refactor random float trsansformer (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
evisdrenova authored Nov 2, 2023
1 parent 192e443 commit 18df678
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
4 changes: 1 addition & 3 deletions worker/internal/benthos/transformers/random_bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ import (

func TestProcessRandomBool(t *testing.T) {

testVal := false // check bool gen

res, err := GenerateRandomBool()

assert.NoError(t, err)
assert.IsType(t, res, testVal)
assert.IsType(t, res, false)

}

Expand Down
16 changes: 2 additions & 14 deletions worker/internal/benthos/transformers/random_float.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func GenerateRandomFloatPreserveLength(i float64, preserveLength bool) (float64,
ad, err := transformer_utils.GenerateRandomInt(int64(fLen.DigitsAfterDecimalLength))

for {
if !isLastDigitAZero(ad) {
if !transformer_utils.IsLastDigitZero(ad) {
break // Exit the loop when i is greater than or equal to 5
}
ad, err = transformer_utils.GenerateRandomInt(int64(fLen.DigitsAfterDecimalLength))
Expand Down Expand Up @@ -106,7 +106,7 @@ func GenerateRandomFloatWithDefinedLength(digitsBeforeDecimal, digitsAfterDecima
// 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) {
if !transformer_utils.IsLastDigitZero(ad) {
break // Exit the loop when i is greater than or equal to 5
}
ad, err = transformer_utils.GenerateRandomInt(digitsAfterDecimal)
Expand Down Expand Up @@ -152,15 +152,3 @@ func GetFloatLength(i float64) *FloatLength {
DigitsAfterDecimalLength: len(parsed[1]),
}
}

func isLastDigitAZero(n int64) bool {
// Convert the int64 to a string
str := strconv.FormatInt(n, 10)

// Check if the string is empty or if the last character is '0'
if len(str) > 0 && str[len(str)-1] == '0' {
return true
}

return false
}
12 changes: 12 additions & 0 deletions worker/internal/benthos/transformers/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,15 @@ func GetIntLength(i int64) int64 {

return length
}

func IsLastDigitZero(n int64) bool {
// Convert the int64 to a string
str := strconv.FormatInt(n, 10)

// Check if the string is empty or if the last character is '0'
if len(str) > 0 && str[len(str)-1] == '0' {
return true
}

return false
}
16 changes: 16 additions & 0 deletions worker/internal/benthos/transformers/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,19 @@ func TestGetIntLegth(t *testing.T) {

assert.Equal(t, int64(expected), val, "The calculated length should match the expected length.")
}

func TestIsLastDigitZeroTrue(t *testing.T) {

value := int64(954670)

res := IsLastDigitZero(value)
assert.Equal(t, res, true, "The last digit is zero.")
}

func TestIsLastDigitZeroFalse(t *testing.T) {

value := int64(23546789)

res := IsLastDigitZero(value)
assert.Equal(t, res, false, "The last digit is not zero.")
}

0 comments on commit 18df678

Please sign in to comment.