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

83 json dne exception #84

Merged
merged 4 commits into from
Aug 14, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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