Skip to content

Commit

Permalink
handling for big int
Browse files Browse the repository at this point in the history
  • Loading branch information
kapishmalik committed Nov 30, 2024
1 parent 3005874 commit 0580133
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
15 changes: 7 additions & 8 deletions core/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ 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 @@ -399,14 +400,12 @@ func jsonPath(query, toMatch string) interface{} {
return ""
}

//// 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
//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)
//}
// 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)
}

// convert to array data if applicable
var data interface{}
Expand Down
16 changes: 16 additions & 0 deletions core/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,22 @@ func Test_CopyMap(t *testing.T) {
Expect(newMap["second"]).To(Equal("2"))
}

func Test_JsonPathMethod_WithBigFloatingNumber(t *testing.T) {

RegisterTestingT(t)
res := jsonPath(PrepareJsonPathQuery("$.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"))
}

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

0 comments on commit 0580133

Please sign in to comment.