1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-08 11:36:03 +01:00

Allow for standard nlohmann JSON serializers to take separate XP features

I realized that we can actually do this thing, even though it is not
what nlohmann expects at all, because the extra parameter has a default
argument so nlohmann doesn't need to care. Sneaky!
This commit is contained in:
John Ericson 2025-10-16 15:49:47 -04:00
parent d87a06af7a
commit 1c02dd5b9c
5 changed files with 35 additions and 32 deletions

View file

@ -66,23 +66,17 @@ TEST_F(DynDerivationTest, BadATerm_oldVersionDynDeps)
FormatError);
}
#define MAKE_OUTPUT_JSON_TEST_P(FIXTURE) \
TEST_P(FIXTURE, from_json) \
{ \
const auto & [name, expected] = GetParam(); \
/* Don't use readJsonTest because we want to check experimental \
features. */ \
readTest(Path{"output-"} + name + ".json", [&](const auto & encoded_) { \
json j = json::parse(encoded_); \
DerivationOutput got = DerivationOutput::fromJSON(j, mockXpSettings); \
ASSERT_EQ(got, expected); \
}); \
} \
\
TEST_P(FIXTURE, to_json) \
{ \
const auto & [name, value] = GetParam(); \
writeJsonTest("output-" + name, value); \
#define MAKE_OUTPUT_JSON_TEST_P(FIXTURE) \
TEST_P(FIXTURE, from_json) \
{ \
const auto & [name, expected] = GetParam(); \
readJsonTest(Path{"output-"} + name, expected, mockXpSettings); \
} \
\
TEST_P(FIXTURE, to_json) \
{ \
const auto & [name, value] = GetParam(); \
writeJsonTest("output-" + name, value); \
}
struct DerivationOutputJsonTest : DerivationTest,
@ -193,13 +187,7 @@ INSTANTIATE_TEST_SUITE_P(
TEST_P(FIXTURE, from_json) \
{ \
const auto & drv = GetParam(); \
/* Don't use readJsonTest because we want to check experimental \
features. */ \
readTest(drv.name + ".json", [&](const auto & encoded_) { \
auto encoded = json::parse(encoded_); \
Derivation got = Derivation::fromJSON(encoded, mockXpSettings); \
ASSERT_EQ(got, drv); \
}); \
readJsonTest(drv.name, drv, mockXpSettings); \
} \
\
TEST_P(FIXTURE, to_json) \

View file

@ -1496,9 +1496,10 @@ namespace nlohmann {
using namespace nix;
DerivationOutput adl_serializer<DerivationOutput>::from_json(const json & json)
DerivationOutput
adl_serializer<DerivationOutput>::from_json(const json & json, const ExperimentalFeatureSettings & xpSettings)
{
return DerivationOutput::fromJSON(json);
return DerivationOutput::fromJSON(json, xpSettings);
}
void adl_serializer<DerivationOutput>::to_json(json & json, const DerivationOutput & c)
@ -1506,9 +1507,9 @@ void adl_serializer<DerivationOutput>::to_json(json & json, const DerivationOutp
json = c.toJSON();
}
Derivation adl_serializer<Derivation>::from_json(const json & json)
Derivation adl_serializer<Derivation>::from_json(const json & json, const ExperimentalFeatureSettings & xpSettings)
{
return Derivation::fromJSON(json);
return Derivation::fromJSON(json, xpSettings);
}
void adl_serializer<Derivation>::to_json(json & json, const Derivation & c)

View file

@ -537,5 +537,5 @@ std::string hashPlaceholder(const OutputNameView outputName);
} // namespace nix
JSON_IMPL(nix::DerivationOutput)
JSON_IMPL(nix::Derivation)
JSON_IMPL_WITH_XP_FEATURES(nix::DerivationOutput)
JSON_IMPL_WITH_XP_FEATURES(nix::Derivation)

View file

@ -24,12 +24,12 @@ struct JsonCharacterizationTest : virtual CharacterizationTest
* @param test hook that takes the contents of the file and does the
* actual work
*/
void readJsonTest(PathView testStem, const T & expected)
void readJsonTest(PathView testStem, const T & expected, auto... args)
{
using namespace nlohmann;
readTest(Path{testStem} + ".json", [&](const auto & encodedRaw) {
auto encoded = json::parse(encodedRaw);
T decoded = adl_serializer<T>::from_json(encoded);
T decoded = adl_serializer<T>::from_json(encoded, args...);
ASSERT_EQ(decoded, expected);
});
}

View file

@ -3,6 +3,8 @@
#include <nlohmann/json_fwd.hpp>
#include "nix/util/experimental-features.hh"
// Following https://github.com/nlohmann/json#how-can-i-use-get-for-non-default-constructiblenon-copyable-types
#define JSON_IMPL(TYPE) \
namespace nlohmann { \
@ -14,3 +16,15 @@
static void to_json(json & json, const TYPE & t); \
}; \
}
#define JSON_IMPL_WITH_XP_FEATURES(TYPE) \
namespace nlohmann { \
using namespace nix; \
template<> \
struct adl_serializer<TYPE> \
{ \
static TYPE \
from_json(const json & json, const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings); \
static void to_json(json & json, const TYPE & t); \
}; \
}