diff --git a/pkg/yqlib/value_parser.go b/pkg/yqlib/value_parser.go index d08da020..0fb515f9 100644 --- a/pkg/yqlib/value_parser.go +++ b/pkg/yqlib/value_parser.go @@ -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 { diff --git a/pkg/yqlib/value_parser_test.go b/pkg/yqlib/value_parser_test.go index 2246b398..1533b2be 100644 --- a/pkg/yqlib/value_parser_test.go +++ b/pkg/yqlib/value_parser_test.go @@ -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) { diff --git a/yq_test.go b/yq_test.go index 05c78a94..2379adb9 100644 --- a/yq_test.go +++ b/yq_test.go @@ -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: