Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "bugfix - resolve issue #1155" #1157

Merged
merged 3 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 5 additions & 30 deletions core/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
log "github.com/sirupsen/logrus"
"io/ioutil"
"k8s.io/client-go/util/jsonpath"
"math/big"
"math/rand"
"net/http"
"net/url"
Expand Down Expand Up @@ -402,14 +401,11 @@ func jsonPath(query, toMatch string) interface{} {

// Jsonpath library converts large int into a string with scientific notion, the following
// reverts that process to avoid mismatching when using the jsonpath result for csv data lookup
// Handle large integers in scientific notation by converting back to big.Int
if isScientific(result) {
// If result is in scientific notation, try converting to a big.Int
bigInt := new(big.Int)
bigInt, success := bigIntFromString(result)
if success {
result = bigInt.String() // Convert back to string representation of the big integer
}
floatResult, err := strconv.ParseFloat(result, 64)
// if the string is a float and a whole number
if err == nil && floatResult == float64(int64(floatResult)) {
intResult := int(floatResult)
result = strconv.Itoa(intResult)
}

// convert to array data if applicable
Expand All @@ -424,27 +420,6 @@ func jsonPath(query, toMatch string) interface{} {
return arrayData
}

// isScientific checks if a string is in scientific notation (e.g., "1.349599e+37")
func isScientific(value string) bool {
return strings.Contains(value, "e") || strings.Contains(value, "E")
}

// bigIntFromString converts a string representing a number (potentially in scientific notation) to big.Int
func bigIntFromString(value string) (*big.Int, bool) {
// Parse the string as a big.Float to handle scientific notation
flt := new(big.Float)
flt, _, err := big.ParseFloat(value, 10, 0, big.ToNearestEven)
if err != nil {
return nil, false
}

// Convert the big.Float to big.Int (rounding down)
bigInt := new(big.Int)
flt.Int(bigInt)

return bigInt, true
}

func xPath(query, toMatch string) string {
result, err := XpathExecution(query, toMatch)
if err != nil {
Expand Down
21 changes: 0 additions & 21 deletions core/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,27 +234,6 @@ func Test_CopyMap(t *testing.T) {
Expect(newMap["second"]).To(Equal("2"))
}

func Test_JsonPathMethod_WithBigFloatingNumber(t *testing.T) {

RegisterTestingT(t)
res := jsonPath("$.registrant", `{"registrant":"13495985898986869898697879879987978978.12345566777"}`)
Expect(res).To(Equal("13495985898986869898697879879987978978.12345566777"))
}

func Test_JsonPathMethod_WithBigIntegerNumber(t *testing.T) {

RegisterTestingT(t)
res := jsonPath("$.registrant", `{"registrant":5553686208582}`)
Expect(res).To(Equal(5553686208582))
}

func Test_JsonPathMethod_WithWordContainingEe(t *testing.T) {

RegisterTestingT(t)
res := jsonPath("$.registrant", `{"registrant":"ETest"}`)
Expect(res).To(Equal("ETest"))
}

func Test_Identical_ReturnsTrue_WithExactlySameArray(t *testing.T) {
RegisterTestingT(t)
first := [2]string{"q1", "q2"}
Expand Down
Loading