Skip to content

Commit 16dd451

Browse files
kapishmaliktommysitu
authored andcommitted
better version of fix
1 parent dacd2be commit 16dd451

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

core/util/util.go

+29-3
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,14 @@ func jsonPath(query, toMatch string) interface{} {
402402

403403
// Jsonpath library converts large int into a string with scientific notion, the following
404404
// reverts that process to avoid mismatching when using the jsonpath result for csv data lookup
405-
bigInt := new(big.Int)
406-
if _, ok := bigInt.SetString(result, 10); ok {
407-
result = fmt.Sprint(bigInt)
405+
// Handle large integers in scientific notation by converting back to big.Int
406+
if isScientific(result) {
407+
// If result is in scientific notation, try converting to a big.Int
408+
bigInt := new(big.Int)
409+
bigInt, success := bigIntFromString(result)
410+
if success {
411+
result = bigInt.String() // Convert back to string representation of the big integer
412+
}
408413
}
409414

410415
// convert to array data if applicable
@@ -419,6 +424,27 @@ func jsonPath(query, toMatch string) interface{} {
419424
return arrayData
420425
}
421426

427+
// isScientific checks if a string is in scientific notation (e.g., "1.349599e+37")
428+
func isScientific(value string) bool {
429+
return strings.Contains(value, "e") || strings.Contains(value, "E")
430+
}
431+
432+
// bigIntFromString converts a string representing a number (potentially in scientific notation) to big.Int
433+
func bigIntFromString(value string) (*big.Int, bool) {
434+
// Parse the string as a big.Float to handle scientific notation
435+
flt := new(big.Float)
436+
flt, _, err := big.ParseFloat(value, 10, 0, big.ToNearestEven)
437+
if err != nil {
438+
return nil, false
439+
}
440+
441+
// Convert the big.Float to big.Int (rounding down)
442+
bigInt := new(big.Int)
443+
flt.Int(bigInt)
444+
445+
return bigInt, true
446+
}
447+
422448
func xPath(query, toMatch string) string {
423449
result, err := XpathExecution(query, toMatch)
424450
if err != nil {

core/util/util_test.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -237,17 +237,22 @@ func Test_CopyMap(t *testing.T) {
237237
func Test_JsonPathMethod_WithBigFloatingNumber(t *testing.T) {
238238

239239
RegisterTestingT(t)
240-
res := jsonPath(PrepareJsonPathQuery("$.registrant"),
241-
`{"registrant":"13495985898986869898697879879987978978.12345566777"}`)
240+
res := jsonPath("$.registrant", `{"registrant":"13495985898986869898697879879987978978.12345566777"}`)
242241
Expect(res).To(Equal("13495985898986869898697879879987978978.12345566777"))
243242
}
244243

245244
func Test_JsonPathMethod_WithBigIntegerNumber(t *testing.T) {
246245

247246
RegisterTestingT(t)
248-
res := jsonPath(PrepareJsonPathQuery("$.registrant"),
249-
`{"registrant":"13495985898986869898697879879987978978"}`)
250-
Expect(res).To(Equal("13495985898986869898697879879987978978"))
247+
res := jsonPath("$.registrant", `{"registrant":5553686208582}`)
248+
Expect(res).To(Equal(5553686208582))
249+
}
250+
251+
func Test_JsonPathMethod_WithWordContainingEe(t *testing.T) {
252+
253+
RegisterTestingT(t)
254+
res := jsonPath("$.registrant", `{"registrant":"ETest"}`)
255+
Expect(res).To(Equal("ETest"))
251256
}
252257

253258
func Test_Identical_ReturnsTrue_WithExactlySameArray(t *testing.T) {

0 commit comments

Comments
 (0)