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:
parent
bf86b939f8
commit
50cb14fcf9
11 changed files with 315 additions and 69 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue