fix for unsigned

This commit is contained in:
James Cor
2022-10-26 11:41:10 -07:00
parent 548a37b4fa
commit 1fb8ba7079
2 changed files with 14 additions and 3 deletions

View File

@@ -16,6 +16,7 @@ package actions
import (
"context"
"errors"
"io"
"math"
"strconv"
@@ -217,9 +218,19 @@ func leastPermissiveNumericType(strVal string, floatThreshold float64) (ti typei
// always parse as signed int
i, err := strconv.ParseInt(strVal, 10, 64)
// use string for out of range
if errors.Is(err, strconv.ErrRange) {
return typeinfo.StringDefaultType
}
if err != nil {
return typeinfo.UnknownType
}
// handle leading zero case
if len(strVal) > 1 && strVal[0] == '0' {
return typeinfo.StringDefaultType
}
if i >= math.MinInt32 && i <= math.MaxInt32 {
return typeinfo.Int32Type
} else {

View File

@@ -75,7 +75,7 @@ func TestLeastPermissiveNumericType(t *testing.T) {
floatThreshold float64
expType typeinfo.TypeInfo
}{
{"zero", "0", 0.0, typeinfo.Uint32Type},
{"zero", "0", 0.0, typeinfo.Int32Type},
{"zero float", "0.0", 0.0, typeinfo.Float32Type},
{"zero float with floatThreshold of 0.1", "0.0", 0.1, typeinfo.Int32Type},
{"negative float", "-1.3451234", 0.0, typeinfo.Float32Type},
@@ -85,8 +85,8 @@ func TestLeastPermissiveNumericType(t *testing.T) {
{"all zeroes", "0000", 0.0, typeinfo.StringDefaultType},
{"leading zeroes", "01", 0.0, typeinfo.StringDefaultType},
{"negative int", "-1234", 0.0, typeinfo.Int32Type},
{"fits in uint64 but not int64", strconv.FormatUint(math.MaxUint64, 10), 0.0, typeinfo.Uint64Type},
{"negative less than math.MinInt64", "-" + strconv.FormatUint(math.MaxUint64, 10), 0.0, typeinfo.UnknownType},
{"fits in uint64 but not int64", strconv.FormatUint(math.MaxUint64, 10), 0.0, typeinfo.StringDefaultType},
{"negative less than math.MinInt64", "-" + strconv.FormatUint(math.MaxUint64, 10), 0.0, typeinfo.StringDefaultType},
{"math.MinInt64", strconv.FormatInt(math.MinInt64, 10), 0.0, typeinfo.Int64Type},
}