Skip to content

Commit dacd2be

Browse files
kapishmaliktommysitu
authored andcommitted
handling for big int
1 parent fa9da57 commit dacd2be

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

core/util/util.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
log "github.com/sirupsen/logrus"
1515
"io/ioutil"
1616
"k8s.io/client-go/util/jsonpath"
17+
"math/big"
1718
"math/rand"
1819
"net/http"
1920
"net/url"
@@ -399,14 +400,12 @@ func jsonPath(query, toMatch string) interface{} {
399400
return ""
400401
}
401402

402-
//// Jsonpath library converts large int into a string with scientific notion, the following
403-
//// reverts that process to avoid mismatching when using the jsonpath result for csv data lookup
404-
//floatResult, err := strconv.ParseFloat(result, 64)
405-
//// if the string is a float and a whole number
406-
//if err == nil && floatResult == float64(int64(floatResult)) {
407-
// intResult := int(floatResult)
408-
// result = strconv.Itoa(intResult)
409-
//}
403+
// Jsonpath library converts large int into a string with scientific notion, the following
404+
// 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)
408+
}
410409

411410
// convert to array data if applicable
412411
var data interface{}

core/util/util_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,22 @@ func Test_CopyMap(t *testing.T) {
234234
Expect(newMap["second"]).To(Equal("2"))
235235
}
236236

237+
func Test_JsonPathMethod_WithBigFloatingNumber(t *testing.T) {
238+
239+
RegisterTestingT(t)
240+
res := jsonPath(PrepareJsonPathQuery("$.registrant"),
241+
`{"registrant":"13495985898986869898697879879987978978.12345566777"}`)
242+
Expect(res).To(Equal("13495985898986869898697879879987978978.12345566777"))
243+
}
244+
245+
func Test_JsonPathMethod_WithBigIntegerNumber(t *testing.T) {
246+
247+
RegisterTestingT(t)
248+
res := jsonPath(PrepareJsonPathQuery("$.registrant"),
249+
`{"registrant":"13495985898986869898697879879987978978"}`)
250+
Expect(res).To(Equal("13495985898986869898697879879987978978"))
251+
}
252+
237253
func Test_Identical_ReturnsTrue_WithExactlySameArray(t *testing.T) {
238254
RegisterTestingT(t)
239255
first := [2]string{"q1", "q2"}

0 commit comments

Comments
 (0)