1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-16 15:32:43 +01:00

Improve checked json casting (#10087)

This introduces new utility functions to get elements from JSON — in an ergonomic way and with nice error messages if the expected type does not match.

Co-authored-by: John Ericson <John.Ericson@Obsidian.Systems>
This commit is contained in:
HaeNoe 2024-04-03 20:04:00 +02:00 committed by GitHub
parent bf86b939f8
commit 50cb14fcf9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 315 additions and 69 deletions

View file

@ -172,19 +172,18 @@ NarInfo NarInfo::fromJSON(
};
if (json.contains("url"))
res.url = ensureType(valueAt(json, "url"), value_t::string);
res.url = getString(valueAt(json, "url"));
if (json.contains("compression"))
res.compression = ensureType(valueAt(json, "compression"), value_t::string);
res.compression = getString(valueAt(json, "compression"));
if (json.contains("downloadHash"))
res.fileHash = Hash::parseAny(
static_cast<const std::string &>(
ensureType(valueAt(json, "downloadHash"), value_t::string)),
getString(valueAt(json, "downloadHash")),
std::nullopt);
if (json.contains("downloadSize"))
res.fileSize = ensureType(valueAt(json, "downloadSize"), value_t::number_integer);
res.fileSize = getInteger(valueAt(json, "downloadSize"));
return res;
}