mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-13 20:15:57 +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 value, err interface{}
|
||||||
var inQuotes = len(argument) > 0 && argument[0] == '"'
|
var inQuotes = len(argument) > 0 && argument[0] == '"'
|
||||||
if !inQuotes {
|
if !inQuotes {
|
||||||
value, err = strconv.ParseFloat(argument, 64)
|
intValue, intErr := strconv.ParseInt(argument, 10, 64)
|
||||||
if err == nil {
|
floatValue, floatErr := strconv.ParseFloat(argument, 64)
|
||||||
return value
|
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)
|
value, err = strconv.ParseBool(argument)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -16,6 +16,7 @@ var parseValueTests = []struct {
|
|||||||
{"3.4", 3.4, "number"},
|
{"3.4", 3.4, "number"},
|
||||||
{"\"3.4\"", "3.4", "number as string"},
|
{"\"3.4\"", "3.4", "number as string"},
|
||||||
{"", "", "empty string"},
|
{"", "", "empty string"},
|
||||||
|
{"1212121", int64(1212121), "big number"},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseValue(t *testing.T) {
|
func TestParseValue(t *testing.T) {
|
||||||
|
@ -35,6 +35,14 @@ func TestNewYamlArray(t *testing.T) {
|
|||||||
formattedResult)
|
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) {
|
func TestNewYaml_WithScript(t *testing.T) {
|
||||||
writeScript = "examples/instruction_sample.yaml"
|
writeScript = "examples/instruction_sample.yaml"
|
||||||
expectedResult := `b:
|
expectedResult := `b:
|
||||||
|
Loading…
Reference in New Issue
Block a user