Skip to content

Commit c62bf25

Browse files
authored
Fix loading large integers from JSON files (#334)
1 parent 8b82618 commit c62bf25

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

mapper.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,12 @@ func intDecoder(bits int) MapperFunc { // nolint: dupl
363363
case string:
364364
sv = v
365365

366-
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64:
366+
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
367367
sv = fmt.Sprintf("%v", v)
368368

369+
case float32, float64:
370+
sv = fmt.Sprintf("%0.f", v)
371+
369372
default:
370373
return fmt.Errorf("expected an int but got %q (%T)", t, t.Value)
371374
}

mapper_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,21 @@ func TestNumbers(t *testing.T) {
400400
})
401401
}
402402

403+
func TestJSONLargeNumber(t *testing.T) {
404+
// https://github.com/alecthomas/kong/pull/334
405+
const n = 1000000
406+
var cli struct {
407+
N int64
408+
}
409+
json := fmt.Sprintf(`{"n": %d}`, n)
410+
r, err := kong.JSON(strings.NewReader(json))
411+
assert.NoError(t, err)
412+
parser := mustNew(t, &cli, kong.Resolvers(r))
413+
_, err = parser.Parse([]string{})
414+
assert.NoError(t, err)
415+
assert.Equal(t, n, cli.N)
416+
}
417+
403418
func TestFileMapper(t *testing.T) {
404419
type CLI struct {
405420
File *os.File `arg:""`

0 commit comments

Comments
 (0)