1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-15 23:12:44 +01:00

Merge pull request #14307 from NixOS/json-schema-hash

`nlohmann::json` instance and JSON Schema for `Hash`
This commit is contained in:
John Ericson 2025-10-21 06:03:20 +00:00 committed by GitHub
commit ef8218f2e3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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

View file

@ -5,6 +5,7 @@
#include "nix/util/types.hh"
#include "nix/util/serialise.hh"
#include "nix/util/file-system.hh"
#include "nix/util/json-impls.hh"
namespace nix {
@ -97,7 +98,11 @@ struct Hash
* @param explicitFormat cannot be SRI, but must be one of the
* "bases".
*/
static Hash parseExplicitFormatUnprefixed(std::string_view s, HashAlgorithm algo, HashFormat explicitFormat);
static Hash parseExplicitFormatUnprefixed(
std::string_view s,
HashAlgorithm algo,
HashFormat explicitFormat,
const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
static Hash parseSRI(std::string_view original);
@ -188,12 +193,14 @@ std::string_view printHashFormat(HashFormat hashFormat);
/**
* Parse a string representing a hash algorithm.
*/
HashAlgorithm parseHashAlgo(std::string_view s);
HashAlgorithm
parseHashAlgo(std::string_view s, const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
/**
* Will return nothing on parse error
*/
std::optional<HashAlgorithm> parseHashAlgoOpt(std::string_view s);
std::optional<HashAlgorithm>
parseHashAlgoOpt(std::string_view s, const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
/**
* And the reverse.
@ -221,6 +228,10 @@ public:
HashResult currentHash();
};
template<>
struct json_avoids_null<Hash> : std::true_type
{};
} // namespace nix
template<>
@ -241,3 +252,5 @@ inline std::size_t hash_value(const Hash & hash)
}
} // namespace nix
JSON_IMPL_WITH_XP_FEATURES(Hash)