From c62bf25854a00f09783ceead8033602712d24d84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sever=20B=C4=83ne=C8=99iu?= Date: Wed, 21 Sep 2022 17:10:58 -0700 Subject: [PATCH] Fix loading large integers from JSON files (#334) --- mapper.go | 5 ++++- mapper_test.go | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/mapper.go b/mapper.go index e94ae76..6d36b15 100644 --- a/mapper.go +++ b/mapper.go @@ -363,9 +363,12 @@ func intDecoder(bits int) MapperFunc { // nolint: dupl case string: sv = v - case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64: + case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: sv = fmt.Sprintf("%v", v) + case float32, float64: + sv = fmt.Sprintf("%0.f", v) + default: return fmt.Errorf("expected an int but got %q (%T)", t, t.Value) } diff --git a/mapper_test.go b/mapper_test.go index 4909758..804ac39 100644 --- a/mapper_test.go +++ b/mapper_test.go @@ -400,6 +400,21 @@ func TestNumbers(t *testing.T) { }) } +func TestJSONLargeNumber(t *testing.T) { + // https://github.com/alecthomas/kong/pull/334 + const n = 1000000 + var cli struct { + N int64 + } + json := fmt.Sprintf(`{"n": %d}`, n) + r, err := kong.JSON(strings.NewReader(json)) + assert.NoError(t, err) + parser := mustNew(t, &cli, kong.Resolvers(r)) + _, err = parser.Parse([]string{}) + assert.NoError(t, err) + assert.Equal(t, n, cli.N) +} + func TestFileMapper(t *testing.T) { type CLI struct { File *os.File `arg:""`