Skip to content

Commit

Permalink
Merge pull request #45 from owent/dev
Browse files Browse the repository at this point in the history
优化整数类型的验证,不允许浮点数转整数
  • Loading branch information
owent authored Sep 4, 2024
2 parents 67e9eb9 + f32f018 commit 7159fc3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unrelease

1. 优化整数类型的验证,不允许浮点数转整数

## 2.18.2

1. 修复 2.18.0-2.18.1 版本中映射可选字段的错误。
Expand Down
39 changes: 34 additions & 5 deletions src/org/xresloader/core/data/vfy/DataVerifyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,14 @@ static public double getAndVerifyToDouble(List<DataVerifyImpl> verifyEngine, Str
}
}

String message = String.format("Convert %s for %s with %s %s failed, check data failed.", val,
path, getValidatorWord(verifyEngine), collectValidatorNames(verifyEngine));
String message;
if (verifyEngine == null || verifyEngine.isEmpty()) {
message = String.format("Convert %s for %s, check data failed.", val,
path);
} else {
message = String.format("Convert %s for %s with %s %s failed, check data failed.", val,
path, getValidatorWord(verifyEngine), collectValidatorNames(verifyEngine));
}
if (ProgramOptions.getInstance().enableDataValidator) {
throw new ConvException(message);
} else {
Expand Down Expand Up @@ -346,8 +352,14 @@ static public String getAndVerifyToString(List<DataVerifyImpl> verifyEngine, Str
}
}

String message = String.format("Convert %s for %s with %s %s failed, check data failed.", val,
path, getValidatorWord(verifyEngine), collectValidatorNames(verifyEngine));
String message;
if (verifyEngine == null || verifyEngine.isEmpty()) {
message = String.format("Convert %s for %s failed, check data failed.", val,
path);
} else {
message = String.format("Convert %s for %s with %s %s failed, check data failed.", val,
path, getValidatorWord(verifyEngine), collectValidatorNames(verifyEngine));
}
if (ProgramOptions.getInstance().enableDataValidator) {
throw new ConvException(message);
} else {
Expand All @@ -358,7 +370,24 @@ static public String getAndVerifyToString(List<DataVerifyImpl> verifyEngine, Str

static public long getAndVerifyToLong(List<DataVerifyImpl> verifyEngine, String path, String val)
throws ConvException {
return Math.round(getAndVerifyToDouble(verifyEngine, path, val));
double value = getAndVerifyToDouble(verifyEngine, path, val);
long ret = Math.round(value);
if (Math.abs(value - (double) ret) > 1e-6) {
String message;
if (verifyEngine == null || verifyEngine.isEmpty()) {
message = String.format("Convert %s for %s failed, not a integer.", val,
path);
} else {
message = String.format("Convert %s for %s with %s %s failed, not a integer.", val,
path, getValidatorWord(verifyEngine), collectValidatorNames(verifyEngine));
}
if (ProgramOptions.getInstance().enableDataValidator) {
throw new ConvException(message);
} else {
ProgramOptions.getLoger().warn(message);
}
}
return ret;
}

static public class ValidatorTokens {
Expand Down

0 comments on commit 7159fc3

Please sign in to comment.