mirror of
https://github.com/NixOS/nix
synced 2025-07-07 06:01:48 +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
18
tests/unit/libfetchers/public-key.cc
Normal file
18
tests/unit/libfetchers/public-key.cc
Normal file
|
@ -0,0 +1,18 @@
|
|||
#include <gtest/gtest.h>
|
||||
#include "fetchers.hh"
|
||||
#include "json-utils.hh"
|
||||
|
||||
namespace nix {
|
||||
TEST(PublicKey, jsonSerialization) {
|
||||
auto json = nlohmann::json(fetchers::PublicKey { .key = "ABCDE" });
|
||||
|
||||
ASSERT_EQ(json, R"({ "key": "ABCDE", "type": "ssh-ed25519" })"_json);
|
||||
}
|
||||
TEST(PublicKey, jsonDeserialization) {
|
||||
auto pubKeyJson = R"({ "key": "ABCDE", "type": "ssh-ed25519" })"_json;
|
||||
fetchers::PublicKey pubKey = pubKeyJson;
|
||||
|
||||
ASSERT_EQ(pubKey.key, "ABCDE");
|
||||
ASSERT_EQ(pubKey.type, "ssh-ed25519");
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue