Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix loading large integers from JSON files #334

Merged
merged 3 commits into from Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion mapper.go
Expand Up @@ -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)
}
Expand Down
15 changes: 15 additions & 0 deletions mapper_test.go
Expand Up @@ -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:""`
Expand Down