Extract JSON type check. (#8677)

- Reuse it in `GetMissing`.
- Add test.
This commit is contained in:
Jiaming Yuan
2023-01-17 03:11:07 +08:00
committed by GitHub
parent 9f598efc3e
commit 43152657d4
3 changed files with 76 additions and 12 deletions

View File

@@ -164,7 +164,7 @@ inline float GetMissing(Json const &config) {
missing = get<Integer const>(j_missing);
} else {
missing = nan("");
LOG(FATAL) << "Invalid missing value: " << j_missing;
TypeCheck<Number, Integer>(j_missing, "missing");
}
return missing;
}
@@ -248,15 +248,6 @@ inline void GenerateFeatureMap(Learner const *learner,
void XGBBuildInfoDevice(Json* p_info);
template <typename JT>
void TypeCheck(Json const &value, StringView name) {
using T = std::remove_const_t<JT> const;
if (!IsA<T>(value)) {
LOG(FATAL) << "Incorrect type for: `" << name << "`, expecting: `" << T{}.TypeStr()
<< "`, got: `" << value.GetValue().TypeStr() << "`.";
}
}
template <typename JT>
auto const &RequiredArg(Json const &in, StringView key, StringView func) {
auto const &obj = get<Object const>(in);