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

Handling large integers #202

Merged
merged 4 commits into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
pmahindrakar-oss marked this conversation as resolved.
Show resolved Hide resolved
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