From bb939d37727f2a046c96dc6ca4a8b2ea8b85531f Mon Sep 17 00:00:00 2001 From: HaeNoe Date: Sun, 14 Apr 2024 22:35:51 +0200 Subject: [PATCH 1/3] change implementation of `optionalValueAt` --- src/libutil/json-utils.cc | 10 ++++------ src/libutil/json-utils.hh | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/libutil/json-utils.cc b/src/libutil/json-utils.cc index 7a7264a9a..1b911bf75 100644 --- a/src/libutil/json-utils.cc +++ b/src/libutil/json-utils.cc @@ -30,14 +30,12 @@ const nlohmann::json & valueAt( return map.at(key); } -std::optional optionalValueAt(const nlohmann::json & value, const std::string & key) +std::optional optionalValueAt(const nlohmann::json::object_t & map, const std::string & key) { - try { - auto & v = valueAt(value, key); - return v.get(); - } catch (...) { + if (!map.contains(key)) return std::nullopt; - } + + return std::optional { map.at(key) }; } diff --git a/src/libutil/json-utils.hh b/src/libutil/json-utils.hh index 2024624f4..08c98cc8c 100644 --- a/src/libutil/json-utils.hh +++ b/src/libutil/json-utils.hh @@ -23,7 +23,7 @@ const nlohmann::json & valueAt( const nlohmann::json::object_t & map, const std::string & key); -std::optional optionalValueAt(const nlohmann::json & value, const std::string & key); +std::optional optionalValueAt(const nlohmann::json::object_t & value, const std::string & key); /** * Downcast the json object, failing with a nice error if the conversion fails. From ff4c286e8006e3d13da67076e973badf78edf22b Mon Sep 17 00:00:00 2001 From: HaeNoe Date: Sun, 14 Apr 2024 22:36:03 +0200 Subject: [PATCH 2/3] add tests for `optionalValueAt` --- tests/unit/libutil/json-utils.cc | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/unit/libutil/json-utils.cc b/tests/unit/libutil/json-utils.cc index ffa667806..ec653fff5 100644 --- a/tests/unit/libutil/json-utils.cc +++ b/tests/unit/libutil/json-utils.cc @@ -160,4 +160,16 @@ TEST(getBoolean, wrongAssertions) { ASSERT_THROW(getBoolean(valueAt(json, "int")), Error); } +TEST(optionalValueAt, existing) { + auto json = R"({ "string": "ssh-rsa" })"_json; + + ASSERT_EQ(optionalValueAt(json, "string"), std::optional { "ssh-rsa" }); +} + +TEST(optionalValueAt, empty) { + auto json = R"({})"_json; + + ASSERT_EQ(optionalValueAt(json, "string2"), std::nullopt); +} + } /* namespace nix */ From e3fed2ebcf5d5b701f7c4a2d368006e20dfd4f8b Mon Sep 17 00:00:00 2001 From: HaeNoe Date: Sun, 14 Apr 2024 22:37:08 +0200 Subject: [PATCH 3/3] update `fetchers::PublicKey` json (de)serialization --- src/libfetchers/fetchers.cc | 18 ++++++++++++++++++ src/libfetchers/fetchers.hh | 4 +++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/libfetchers/fetchers.cc b/src/libfetchers/fetchers.cc index 483796f0b..a06d931db 100644 --- a/src/libfetchers/fetchers.cc +++ b/src/libfetchers/fetchers.cc @@ -3,6 +3,7 @@ #include "input-accessor.hh" #include "source-path.hh" #include "fetch-to-store.hh" +#include "json-utils.hh" #include @@ -412,3 +413,20 @@ std::string publicKeys_to_string(const std::vector& publicKeys) } } + +namespace nlohmann { + +using namespace nix; + +fetchers::PublicKey adl_serializer::from_json(const json & json) { + auto type = optionalValueAt(json, "type").value_or("ssh-ed25519"); + auto key = valueAt(json, "key"); + return fetchers::PublicKey { getString(type), getString(key) }; +} + +void adl_serializer::to_json(json & json, fetchers::PublicKey p) { + json["type"] = p.type; + json["key"] = p.key; +} + +} diff --git a/src/libfetchers/fetchers.hh b/src/libfetchers/fetchers.hh index cd11f9eae..bb21c68cc 100644 --- a/src/libfetchers/fetchers.hh +++ b/src/libfetchers/fetchers.hh @@ -4,6 +4,7 @@ #include "types.hh" #include "hash.hh" #include "canon-path.hh" +#include "json-impls.hh" #include "attrs.hh" #include "url.hh" @@ -230,8 +231,9 @@ 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&); } + +JSON_IMPL(fetchers::PublicKey)