Skip to content

Commit

Permalink
Merge pull request #84 from TGSAI/83_json_DNE_exception
Browse files Browse the repository at this point in the history
83 json dne exception
  • Loading branch information
BrianMichell authored Aug 14, 2024
2 parents 0377fda + 7f977b1 commit adf7aa0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
6 changes: 5 additions & 1 deletion mdio/dataset.h
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,11 @@ class Dataset {
}
}
)";
nlohmann::json base = nlohmann::json::parse(baseStr);
nlohmann::json base = nlohmann::json::parse(baseStr, nullptr, false);
if (base.is_discarded()) {
return absl::Status(absl::StatusCode::kInternal,
"Failed to parse base JSON.");
}
if (found >= 0) {
base["field"] = specJson["metadata"]["dtype"][found][0];
} else {
Expand Down
24 changes: 24 additions & 0 deletions mdio/dataset_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -529,4 +529,28 @@ TEST(Dataset, commitSlicedMetadata) {

EXPECT_TRUE(commitRes.status().ok()) << commitRes.status();
}

TEST(Dataset, openNonExistent) {
auto json_vars = GetToyExample();

auto datasetRes =
mdio::Dataset::from_json(json_vars, "zarrs/DNE", mdio::constants::kOpen);
ASSERT_FALSE(datasetRes.status().ok())
<< "Opened a non-existent dataset without error!";
}

TEST(Dataset, kCreateOverExisting) {
auto json_vars = GetToyExample();

auto datasetRes = mdio::Dataset::from_json(json_vars, "zarrs/acceptance",
mdio::constants::kCreateClean);
ASSERT_TRUE(datasetRes.status().ok()) << datasetRes.status();

datasetRes = mdio::Dataset::from_json(json_vars, "zarrs/acceptance",
mdio::constants::kCreate);
ASSERT_FALSE(datasetRes.status().ok())
<< "Created a dataset over an existing "
"one without error!";
}

} // namespace
6 changes: 5 additions & 1 deletion mdio/dataset_validator.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ bool contains(const std::unordered_set<std::string>& set,
* InvalidArgumentError if validation fails for any reason
*/
absl::Status validate_schema(nlohmann::json& spec /*NOLINT*/) {
nlohmann::json targetSchema = nlohmann::json::parse(kDatasetSchema);
nlohmann::json targetSchema =
nlohmann::json::parse(kDatasetSchema, nullptr, false);
if (targetSchema.is_discarded()) {
return absl::NotFoundError("Failed to load schema");
}

nlohmann::json_schema::json_validator validator(
nullptr, nlohmann::json_schema::default_string_format_check);
Expand Down
4 changes: 2 additions & 2 deletions mdio/variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,8 @@ Future<Variable<T, R, M>> OpenVariable(const nlohmann::json& json_store,
// go read the attributes return json ...
auto parse = [](const tensorstore::kvstore::ReadResult& kvs_read,
const ::nlohmann::json& spec) {
// FIXME - if attributes supplied then validate with values
auto attributes = nlohmann::json::parse(std::string(kvs_read.value));
auto attributes =
nlohmann::json::parse(std::string(kvs_read.value), nullptr, false);
return attributes;
};

Expand Down

0 comments on commit adf7aa0

Please sign in to comment.