1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-25 19:01:16 +02: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

@ -147,9 +147,12 @@ std::vector<PublicKey> getPublicKeys(const Attrs & attrs)
{
std::vector<PublicKey> publicKeys;
if (attrs.contains("publicKeys")) {
nlohmann::json publicKeysJson = nlohmann::json::parse(getStrAttr(attrs, "publicKeys"));
ensureType(publicKeysJson, nlohmann::json::value_t::array);
publicKeys = publicKeysJson.get<std::vector<PublicKey>>();
auto pubKeysJson = nlohmann::json::parse(getStrAttr(attrs, "publicKeys"));
auto & pubKeys = getArray(pubKeysJson);
for (auto & key : pubKeys) {
publicKeys.push_back(key);
}
}
if (attrs.contains("publicKey"))
publicKeys.push_back(PublicKey{maybeGetStrAttr(attrs, "keytype").value_or("ssh-ed25519"),getStrAttr(attrs, "publicKey")});