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