Fix loading large integers from JSON files (#334)

This commit is contained in:
Sever Băneșiu
2022-09-21 17:10:58 -07:00
committed by GitHub
parent 8b826182d8
commit c62bf25854
2 changed files with 19 additions and 1 deletions
+4 -1
View File
@@ -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)
}
+15
View File
@@ -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:""`