#314 Fix the big int changing into float

This commit is contained in:
Ryan SIU 2020-01-10 15:44:36 +08:00 committed by Mike Farah
parent 3e83ff7ac8
commit 547787f3ae
3 changed files with 19 additions and 3 deletions

View File

@ -18,9 +18,16 @@ func (v *valueParser) ParseValue(argument string) interface{} {
var value, err interface{}
var inQuotes = len(argument) > 0 && argument[0] == '"'
if !inQuotes {
value, err = strconv.ParseFloat(argument, 64)
if err == nil {
return value
intValue, intErr := strconv.ParseInt(argument, 10, 64)
floatValue, floatErr := strconv.ParseFloat(argument, 64)
if intErr == nil && floatErr == nil {
if int64(floatValue) == intValue {
return intValue
}
return floatValue
} else if floatErr == nil {
// In case cannot parse the int due to large precision
return floatValue
}
value, err = strconv.ParseBool(argument)
if err == nil {

View File

@ -16,6 +16,7 @@ var parseValueTests = []struct {
{"3.4", 3.4, "number"},
{"\"3.4\"", "3.4", "number as string"},
{"", "", "empty string"},
{"1212121", int64(1212121), "big number"},
}
func TestParseValue(t *testing.T) {

View File

@ -35,6 +35,14 @@ func TestNewYamlArray(t *testing.T) {
formattedResult)
}
func TestNewYamlBigInt(t *testing.T) {
result, _ := newYaml([]string{"b", "1212121"})
formattedResult := fmt.Sprintf("%v", result)
test.AssertResult(t,
"[{b 1212121}]",
formattedResult)
}
func TestNewYaml_WithScript(t *testing.T) {
writeScript = "examples/instruction_sample.yaml"
expectedResult := `b: