mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
#314 Fix the big int changing into float
This commit is contained in:
parent
3e83ff7ac8
commit
547787f3ae
@ -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 {
|
||||
|
@ -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) {
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user