From e8820cd3429962c89012d6a94c0411432dbcdc37 Mon Sep 17 00:00:00 2001 From: Sever Banesiu Date: Wed, 21 Sep 2022 15:56:06 -0700 Subject: [PATCH 1/3] Fix loading large integers from JSON files --- mapper.go | 5 ++++- mapper_test.go | 14 ++++++++++++++ 2 files changed, 18 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..09f575e 100644 --- a/mapper_test.go +++ b/mapper_test.go @@ -400,6 +400,20 @@ func TestNumbers(t *testing.T) { }) } +func TestJsonBigNumber(t *testing.T) { + 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:""` From becea4a592aa8f317fafc0a81cf5bf84c8c46683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sever=20B=C4=83ne=C8=99iu?= Date: Wed, 21 Sep 2022 16:12:24 -0700 Subject: [PATCH 2/3] Garden --- mapper_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mapper_test.go b/mapper_test.go index 09f575e..7ac860c 100644 --- a/mapper_test.go +++ b/mapper_test.go @@ -400,7 +400,8 @@ func TestNumbers(t *testing.T) { }) } -func TestJsonBigNumber(t *testing.T) { +func TestJSONLargeNumber(t *testing.T) { + // https://github.com/alecthomas/kong/pull/334/files const n = 1000000 var cli struct { N int64 From 4149776559eb159c75a21c5986affebc122a73bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sever=20B=C4=83ne=C8=99iu?= Date: Wed, 21 Sep 2022 16:13:57 -0700 Subject: [PATCH 3/3] Garden --- mapper_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mapper_test.go b/mapper_test.go index 7ac860c..804ac39 100644 --- a/mapper_test.go +++ b/mapper_test.go @@ -401,7 +401,7 @@ func TestNumbers(t *testing.T) { } func TestJSONLargeNumber(t *testing.T) { - // https://github.com/alecthomas/kong/pull/334/files + // https://github.com/alecthomas/kong/pull/334 const n = 1000000 var cli struct { N int64