mirror of
https://github.com/NixOS/nix.git
synced 2025-11-08 19:46:02 +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:
parent
d87a06af7a
commit
1c02dd5b9c
5 changed files with 35 additions and 32 deletions
|
|
@ -66,23 +66,17 @@ TEST_F(DynDerivationTest, BadATerm_oldVersionDynDeps)
|
||||||
FormatError);
|
FormatError);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define MAKE_OUTPUT_JSON_TEST_P(FIXTURE) \
|
#define MAKE_OUTPUT_JSON_TEST_P(FIXTURE) \
|
||||||
TEST_P(FIXTURE, from_json) \
|
TEST_P(FIXTURE, from_json) \
|
||||||
{ \
|
{ \
|
||||||
const auto & [name, expected] = GetParam(); \
|
const auto & [name, expected] = GetParam(); \
|
||||||
/* Don't use readJsonTest because we want to check experimental \
|
readJsonTest(Path{"output-"} + name, expected, mockXpSettings); \
|
||||||
features. */ \
|
} \
|
||||||
readTest(Path{"output-"} + name + ".json", [&](const auto & encoded_) { \
|
\
|
||||||
json j = json::parse(encoded_); \
|
TEST_P(FIXTURE, to_json) \
|
||||||
DerivationOutput got = DerivationOutput::fromJSON(j, mockXpSettings); \
|
{ \
|
||||||
ASSERT_EQ(got, expected); \
|
const auto & [name, value] = GetParam(); \
|
||||||
}); \
|
writeJsonTest("output-" + name, value); \
|
||||||
} \
|
|
||||||
\
|
|
||||||
TEST_P(FIXTURE, to_json) \
|
|
||||||
{ \
|
|
||||||
const auto & [name, value] = GetParam(); \
|
|
||||||
writeJsonTest("output-" + name, value); \
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DerivationOutputJsonTest : DerivationTest,
|
struct DerivationOutputJsonTest : DerivationTest,
|
||||||
|
|
@ -193,13 +187,7 @@ INSTANTIATE_TEST_SUITE_P(
|
||||||
TEST_P(FIXTURE, from_json) \
|
TEST_P(FIXTURE, from_json) \
|
||||||
{ \
|
{ \
|
||||||
const auto & drv = GetParam(); \
|
const auto & drv = GetParam(); \
|
||||||
/* Don't use readJsonTest because we want to check experimental \
|
readJsonTest(drv.name, drv, mockXpSettings); \
|
||||||
features. */ \
|
|
||||||
readTest(drv.name + ".json", [&](const auto & encoded_) { \
|
|
||||||
auto encoded = json::parse(encoded_); \
|
|
||||||
Derivation got = Derivation::fromJSON(encoded, mockXpSettings); \
|
|
||||||
ASSERT_EQ(got, drv); \
|
|
||||||
}); \
|
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
TEST_P(FIXTURE, to_json) \
|
TEST_P(FIXTURE, to_json) \
|
||||||
|
|
|
||||||
|
|
@ -1496,9 +1496,10 @@ namespace nlohmann {
|
||||||
|
|
||||||
using namespace nix;
|
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)
|
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();
|
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)
|
void adl_serializer<Derivation>::to_json(json & json, const Derivation & c)
|
||||||
|
|
|
||||||
|
|
@ -537,5 +537,5 @@ std::string hashPlaceholder(const OutputNameView outputName);
|
||||||
|
|
||||||
} // namespace nix
|
} // namespace nix
|
||||||
|
|
||||||
JSON_IMPL(nix::DerivationOutput)
|
JSON_IMPL_WITH_XP_FEATURES(nix::DerivationOutput)
|
||||||
JSON_IMPL(nix::Derivation)
|
JSON_IMPL_WITH_XP_FEATURES(nix::Derivation)
|
||||||
|
|
|
||||||
|
|
@ -24,12 +24,12 @@ struct JsonCharacterizationTest : virtual CharacterizationTest
|
||||||
* @param test hook that takes the contents of the file and does the
|
* @param test hook that takes the contents of the file and does the
|
||||||
* actual work
|
* actual work
|
||||||
*/
|
*/
|
||||||
void readJsonTest(PathView testStem, const T & expected)
|
void readJsonTest(PathView testStem, const T & expected, auto... args)
|
||||||
{
|
{
|
||||||
using namespace nlohmann;
|
using namespace nlohmann;
|
||||||
readTest(Path{testStem} + ".json", [&](const auto & encodedRaw) {
|
readTest(Path{testStem} + ".json", [&](const auto & encodedRaw) {
|
||||||
auto encoded = json::parse(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);
|
ASSERT_EQ(decoded, expected);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,8 @@
|
||||||
|
|
||||||
#include <nlohmann/json_fwd.hpp>
|
#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
|
// Following https://github.com/nlohmann/json#how-can-i-use-get-for-non-default-constructiblenon-copyable-types
|
||||||
#define JSON_IMPL(TYPE) \
|
#define JSON_IMPL(TYPE) \
|
||||||
namespace nlohmann { \
|
namespace nlohmann { \
|
||||||
|
|
@ -14,3 +16,15 @@
|
||||||
static void to_json(json & json, const TYPE & t); \
|
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); \
|
||||||
|
}; \
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue