1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-12-16 14:01:05 +01:00

nlohmann::json instance and JSON Schema for Hash

Improving and codifying our experimental JSON interfacing.

Co-Authored-By: Robert Hensing <robert@roberthensing.nl>
This commit is contained in:
John Ericson 2025-09-13 08:32:26 -04:00
parent f05d240222
commit 5e7ee808de
16 changed files with 252 additions and 24 deletions

View file

@ -13,6 +13,7 @@
#include "nix/util/split.hh"
#include "nix/util/base-n.hh"
#include "nix/util/base-nix-32.hh"
#include "nix/util/json-utils.hh"
#include <sys/types.h>
#include <sys/stat.h>
@ -141,9 +142,13 @@ static HashFormat baseFromSize(std::string_view rest, HashAlgorithm algo)
*
* @param rest the string view to parse. Must not include any `<algo>(:|-)` prefix.
*/
static Hash parseLowLevel(std::string_view rest, HashAlgorithm algo, DecodeNamePair pair)
static Hash parseLowLevel(
std::string_view rest,
HashAlgorithm algo,
DecodeNamePair pair,
const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings)
{
Hash res{algo};
Hash res{algo, xpSettings};
std::string d;
try {
d = pair.decode(rest);
@ -244,9 +249,10 @@ Hash Hash::parseNonSRIUnprefixed(std::string_view s, HashAlgorithm algo)
return parseExplicitFormatUnprefixed(s, algo, baseFromSize(s, algo));
}
Hash Hash::parseExplicitFormatUnprefixed(std::string_view s, HashAlgorithm algo, HashFormat format)
Hash Hash::parseExplicitFormatUnprefixed(
std::string_view s, HashAlgorithm algo, HashFormat format, const ExperimentalFeatureSettings & xpSettings)
{
return parseLowLevel(s, algo, baseExplicit(format));
return parseLowLevel(s, algo, baseExplicit(format), xpSettings);
}
Hash Hash::random(HashAlgorithm algo)
@ -446,10 +452,12 @@ std::string_view printHashFormat(HashFormat HashFormat)
}
}
std::optional<HashAlgorithm> parseHashAlgoOpt(std::string_view s)
std::optional<HashAlgorithm> parseHashAlgoOpt(std::string_view s, const ExperimentalFeatureSettings & xpSettings)
{
if (s == "blake3")
if (s == "blake3") {
xpSettings.require(Xp::BLAKE3Hashes);
return HashAlgorithm::BLAKE3;
}
if (s == "md5")
return HashAlgorithm::MD5;
if (s == "sha1")
@ -461,9 +469,9 @@ std::optional<HashAlgorithm> parseHashAlgoOpt(std::string_view s)
return std::nullopt;
}
HashAlgorithm parseHashAlgo(std::string_view s)
HashAlgorithm parseHashAlgo(std::string_view s, const ExperimentalFeatureSettings & xpSettings)
{
auto opt_h = parseHashAlgoOpt(s);
auto opt_h = parseHashAlgoOpt(s, xpSettings);
if (opt_h)
return *opt_h;
else
@ -491,3 +499,27 @@ std::string_view printHashAlgo(HashAlgorithm ha)
}
} // namespace nix
namespace nlohmann {
using namespace nix;
Hash adl_serializer<Hash>::from_json(const json & json, const ExperimentalFeatureSettings & xpSettings)
{
auto & obj = getObject(json);
auto algo = parseHashAlgo(getString(valueAt(obj, "algorithm")), xpSettings);
auto format = parseHashFormat(getString(valueAt(obj, "format")));
auto & hashS = getString(valueAt(obj, "hash"));
return Hash::parseExplicitFormatUnprefixed(hashS, algo, format, xpSettings);
}
void adl_serializer<Hash>::to_json(json & json, const Hash & hash)
{
json = {
{"format", printHashFormat(HashFormat::Base64)},
{"algorithm", printHashAlgo(hash.algo)},
{"hash", hash.to_string(HashFormat::Base64, false)},
};
}
} // namespace nlohmann