Merge pull request #15270 from stonezdj/21jun05_fix_rest_int_parse_fail

Fix invalid syntax errors when int value is convert to scientific notation
This commit is contained in:
stonezdj(Daojun Zhang) 2021-07-11 21:00:06 +08:00 committed by GitHub
commit 7b84f4e137
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -90,12 +90,12 @@ type IntType struct {
}
func (t *IntType) validate(str string) error {
_, err := strconv.Atoi(str)
_, err := parseInt(str)
return err
}
func (t *IntType) get(str string) (interface{}, error) {
return strconv.Atoi(str)
return parseInt(str)
}
// PortType ...
@ -237,3 +237,17 @@ func parseInt64(str string) (int64, error) {
return 0, fmt.Errorf("invalid int64 string: %s", str)
}
func parseInt(str string) (int, error) {
val, err := strconv.ParseInt(str, 10, 32)
if err == nil {
return int(val), nil
}
fval, err := strconv.ParseFloat(str, 32)
if err == nil && fval == math.Trunc(fval) {
return int(fval), nil
}
return 0, fmt.Errorf("invalid int string: %s", str)
}