Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
Handling large integers (#202)
Browse files Browse the repository at this point in the history
* Handling large integers

Signed-off-by: Prafulla Mahindrakar <[email protected]>
  • Loading branch information
pmahindrakar-oss authored Jul 30, 2021
1 parent 02f2066 commit 32129a0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
9 changes: 8 additions & 1 deletion clients/go/coreutils/literals.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package coreutils
import (
"encoding/json"
"fmt"
"math"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -492,7 +493,13 @@ func MakeLiteralForType(t *core.LiteralType, v interface{}) (*core.Literal, erro
if v == nil {
strValue = ""
}

// Note this is to support large integers which by default when passed from an unmarshalled json will be
// converted to float64 and printed as exponential format by Sprintf.
// eg : 8888888 get converted to 8.888888e+06 and which causes strconv.ParseInt to fail
// Inorder to avoid this we explicitly add this check.
if f, ok := v.(float64); ok && math.Trunc(f) == f {
strValue = fmt.Sprintf("%.0f", math.Trunc(f))
}
if newT.Simple == core.SimpleType_STRUCT {
if _, isValueStringType := v.(string); !isValueStringType {
byteValue, err := json.Marshal(v)
Expand Down
26 changes: 26 additions & 0 deletions clients/go/coreutils/literals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package coreutils
import (
"fmt"
"reflect"
"strconv"
"testing"
"time"

Expand All @@ -16,6 +17,7 @@ import (

"github.com/golang/protobuf/ptypes"
structpb "github.com/golang/protobuf/ptypes/struct"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -391,6 +393,30 @@ func TestMakeLiteralForType(t *testing.T) {
assert.Equal(t, expectedVal, actualVal)
})

t.Run("IntegerComingInAsFloatOverFlow", func(t *testing.T) {
var literalType = &core.LiteralType{Type: &core.LiteralType_Simple{Simple: core.SimpleType_INTEGER}}
_, err := MakeLiteralForType(literalType, 8.888888e+19)
assert.NotNil(t, err)
numError := &strconv.NumError{
Func: "ParseInt",
Num: "88888880000000000000",
Err: fmt.Errorf("value out of range"),
}
parseIntError := errors.WithMessage(numError, "failed to parse integer value")
assert.Equal(t, errors.WithStack(parseIntError).Error(), err.Error())
})

t.Run("IntegerComingInAsFloat", func(t *testing.T) {
var literalType = &core.LiteralType{Type: &core.LiteralType_Simple{Simple: core.SimpleType_INTEGER}}
val, err := MakeLiteralForType(literalType, 8.888888e+18)
assert.NoError(t, err)
literalVal := &core.Literal{Value: &core.Literal_Scalar{Scalar: &core.Scalar{
Value: &core.Scalar_Primitive{Primitive: &core.Primitive{Value: &core.Primitive_Integer{Integer: 8.888888e+18}}}}}}
expectedVal, _ := ExtractFromLiteral(literalVal)
actualVal, _ := ExtractFromLiteral(val)
assert.Equal(t, expectedVal, actualVal)
})

t.Run("SimpleFloat", func(t *testing.T) {
var literalType = &core.LiteralType{Type: &core.LiteralType_Simple{Simple: core.SimpleType_FLOAT}}
val, err := MakeLiteralForType(literalType, 1)
Expand Down

0 comments on commit 32129a0

Please sign in to comment.