Skip to content

Commit

Permalink
better version of fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kapishmalik authored and tommysitu committed Nov 30, 2024
1 parent dacd2be commit 16dd451
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
32 changes: 29 additions & 3 deletions core/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,14 @@ 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
bigInt := new(big.Int)
if _, ok := bigInt.SetString(result, 10); ok {
result = fmt.Sprint(bigInt)
// 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
}
}

// convert to array data if applicable
Expand All @@ -419,6 +424,27 @@ 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
15 changes: 10 additions & 5 deletions core/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,17 +237,22 @@ func Test_CopyMap(t *testing.T) {
func Test_JsonPathMethod_WithBigFloatingNumber(t *testing.T) {

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

func Test_JsonPathMethod_WithBigIntegerNumber(t *testing.T) {

RegisterTestingT(t)
res := jsonPath(PrepareJsonPathQuery("$.registrant"),
`{"registrant":"13495985898986869898697879879987978978"}`)
Expect(res).To(Equal("13495985898986869898697879879987978978"))
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) {
Expand Down

0 comments on commit 16dd451

Please sign in to comment.