Skip to content

Commit

Permalink
Implement JSON (de)serialization for PublicKey
Browse files Browse the repository at this point in the history
  • Loading branch information
haenoe committed Apr 2, 2024
1 parent 5859930 commit 27a16f4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
19 changes: 19 additions & 0 deletions src/libfetchers/fetchers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,22 @@ std::string publicKeys_to_string(const std::vector<PublicKey>& publicKeys)
}

}

namespace nlohmann {

nix::fetchers::PublicKey adl_serializer<nix::fetchers::PublicKey>::from_json(const json & json)
{
auto & object = getObject(json);
auto & type = getString(optionalValueAt(object, "type").value_or("ssh-ed25519"));
auto & key = getString(valueAt(object, "key"));

return nix::fetchers::PublicKey { type, key };
}

void adl_serializer<nix::fetchers::PublicKey>::to_json(json & json, nix::fetchers::PublicKey t)
{
json["type"] = t.type;
json["key"] = t.key;
}

}
5 changes: 3 additions & 2 deletions src/libfetchers/fetchers.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <memory>
#include <nlohmann/json_fwd.hpp>
#include "json-impls.hh"

namespace nix { class Store; class StorePath; struct InputAccessor; }

Expand Down Expand Up @@ -213,8 +214,8 @@ struct PublicKey
std::string type = "ssh-ed25519";
std::string key;
};
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(PublicKey, type, key)

std::string publicKeys_to_string(const std::vector<PublicKey>&);

}

JSON_IMPL(nix::fetchers::PublicKey)
11 changes: 3 additions & 8 deletions src/libfetchers/git.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,9 @@ std::vector<PublicKey> getPublicKeys(const Attrs & attrs)
{
std::vector<PublicKey> publicKeys;
if (attrs.contains("publicKeys")) {
nlohmann::json publicKeysJson = nlohmann::json::parse(getStrAttr(attrs, "publicKeys"));
auto pubKeys = getArray(publicKeysJson);
publicKeys.clear();
for (auto jsonKey : pubKeys) {
auto keyObj = getObject(jsonKey);
auto type = getString(getNullable(keyObj, "type").value_or("ssh-ed25519"));
auto key = getString(valueAt(keyObj, "key"));
publicKeys.push_back({ type, key });
nlohmann::json publicKeysJson = getArray(nlohmann::json::parse(getStrAttr(attrs, "publicKeys")));
for (auto & jsonKey : publicKeysJson) {
publicKeys.push_back(jsonKey);
}
}
if (attrs.contains("publicKey"))
Expand Down

0 comments on commit 27a16f4

Please sign in to comment.