Skip to content

Commit a2c6f38

Browse files
committed
Remove now-redundant methods for JSON on Derivation
1 parent 1c02dd5 commit a2c6f38

File tree

6 files changed

+67
-101
lines changed

6 files changed

+67
-101
lines changed

src/libstore-tests/derivation-advanced-attrs.cc

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
namespace nix {
1616

17-
using nlohmann::json;
17+
using namespace nlohmann;
1818

1919
class DerivationAdvancedAttrsTest : public CharacterizationTest, public LibStoreTest
2020
{
@@ -51,44 +51,44 @@ using BothFixtures = ::testing::Types<DerivationAdvancedAttrsTest, CaDerivationA
5151

5252
TYPED_TEST_SUITE(DerivationAdvancedAttrsBothTest, BothFixtures);
5353

54-
#define TEST_ATERM_JSON(STEM, NAME) \
55-
TYPED_TEST(DerivationAdvancedAttrsBothTest, Derivation_##STEM##_from_json) \
56-
{ \
57-
this->readTest(NAME ".json", [&](const auto & encoded_) { \
58-
auto encoded = json::parse(encoded_); \
59-
/* Use DRV file instead of C++ literal as source of truth. */ \
60-
auto aterm = readFile(this->goldenMaster(NAME ".drv")); \
61-
auto expected = parseDerivation(*this->store, std::move(aterm), NAME, this->mockXpSettings); \
62-
Derivation got = Derivation::fromJSON(encoded, this->mockXpSettings); \
63-
EXPECT_EQ(got, expected); \
64-
}); \
65-
} \
66-
\
67-
TYPED_TEST(DerivationAdvancedAttrsBothTest, Derivation_##STEM##_to_json) \
68-
{ \
69-
this->writeTest( \
70-
NAME ".json", \
71-
[&]() -> json { \
72-
/* Use DRV file instead of C++ literal as source of truth. */ \
73-
auto aterm = readFile(this->goldenMaster(NAME ".drv")); \
74-
return parseDerivation(*this->store, std::move(aterm), NAME, this->mockXpSettings).toJSON(); \
75-
}, \
76-
[](const auto & file) { return json::parse(readFile(file)); }, \
77-
[](const auto & file, const auto & got) { return writeFile(file, got.dump(2) + "\n"); }); \
78-
} \
79-
\
80-
TYPED_TEST(DerivationAdvancedAttrsBothTest, Derivation_##STEM##_from_aterm) \
81-
{ \
82-
this->readTest(NAME ".drv", [&](auto encoded) { \
83-
/* Use JSON file instead of C++ literal as source of truth. */ \
84-
auto json = json::parse(readFile(this->goldenMaster(NAME ".json"))); \
85-
auto expected = Derivation::fromJSON(json, this->mockXpSettings); \
86-
auto got = parseDerivation(*this->store, std::move(encoded), NAME, this->mockXpSettings); \
87-
EXPECT_EQ(got.toJSON(), expected.toJSON()); \
88-
EXPECT_EQ(got, expected); \
89-
}); \
90-
} \
91-
\
54+
#define TEST_ATERM_JSON(STEM, NAME) \
55+
TYPED_TEST(DerivationAdvancedAttrsBothTest, Derivation_##STEM##_from_json) \
56+
{ \
57+
this->readTest(NAME ".json", [&](const auto & encoded_) { \
58+
auto encoded = json::parse(encoded_); \
59+
/* Use DRV file instead of C++ literal as source of truth. */ \
60+
auto aterm = readFile(this->goldenMaster(NAME ".drv")); \
61+
auto expected = parseDerivation(*this->store, std::move(aterm), NAME, this->mockXpSettings); \
62+
Derivation got = adl_serializer<Derivation>::from_json(encoded, this->mockXpSettings); \
63+
EXPECT_EQ(got, expected); \
64+
}); \
65+
} \
66+
\
67+
TYPED_TEST(DerivationAdvancedAttrsBothTest, Derivation_##STEM##_to_json) \
68+
{ \
69+
this->writeTest( \
70+
NAME ".json", \
71+
[&]() -> json { \
72+
/* Use DRV file instead of C++ literal as source of truth. */ \
73+
auto aterm = readFile(this->goldenMaster(NAME ".drv")); \
74+
return parseDerivation(*this->store, std::move(aterm), NAME, this->mockXpSettings); \
75+
}, \
76+
[](const auto & file) { return json::parse(readFile(file)); }, \
77+
[](const auto & file, const auto & got) { return writeFile(file, got.dump(2) + "\n"); }); \
78+
} \
79+
\
80+
TYPED_TEST(DerivationAdvancedAttrsBothTest, Derivation_##STEM##_from_aterm) \
81+
{ \
82+
this->readTest(NAME ".drv", [&](auto encoded) { \
83+
/* Use JSON file instead of C++ literal as source of truth. */ \
84+
auto j = json::parse(readFile(this->goldenMaster(NAME ".json"))); \
85+
auto expected = adl_serializer<Derivation>::from_json(j, this->mockXpSettings); \
86+
auto got = parseDerivation(*this->store, std::move(encoded), NAME, this->mockXpSettings); \
87+
EXPECT_EQ(static_cast<json>(got), static_cast<json>(expected)); \
88+
EXPECT_EQ(got, expected); \
89+
}); \
90+
} \
91+
\
9292
/* No corresponding write test, because we need to read the drv to write the json file */
9393

9494
TEST_ATERM_JSON(advancedAttributes, "advanced-attributes-defaults");

src/libstore-tests/derivation.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ INSTANTIATE_TEST_SUITE_P(
201201
const auto & drv = GetParam(); \
202202
readTest(drv.name + ".drv", [&](auto encoded) { \
203203
auto got = parseDerivation(*store, std::move(encoded), drv.name, mockXpSettings); \
204-
ASSERT_EQ(got.toJSON(), drv.toJSON()); \
204+
using nlohmann::json; \
205+
ASSERT_EQ(static_cast<json>(got), static_cast<json>(drv)); \
205206
ASSERT_EQ(got, drv); \
206207
}); \
207208
} \

src/libstore/derivations.cc

Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,9 +1261,15 @@ void Derivation::checkInvariants(Store & store, const StorePath & drvPath) const
12611261

12621262
const Hash impureOutputHash = hashString(HashAlgorithm::SHA256, "impure");
12631263

1264-
nlohmann::json DerivationOutput::toJSON() const
1264+
} // namespace nix
1265+
1266+
namespace nlohmann {
1267+
1268+
using namespace nix;
1269+
1270+
void adl_serializer<DerivationOutput>::to_json(json & res, const DerivationOutput & o)
12651271
{
1266-
nlohmann::json res = nlohmann::json::object();
1272+
res = nlohmann::json::object();
12671273
std::visit(
12681274
overloaded{
12691275
[&](const DerivationOutput::InputAddressed & doi) { res["path"] = doi.path; },
@@ -1289,12 +1295,11 @@ nlohmann::json DerivationOutput::toJSON() const
12891295
res["impure"] = true;
12901296
},
12911297
},
1292-
raw);
1293-
return res;
1298+
o.raw);
12941299
}
12951300

12961301
DerivationOutput
1297-
DerivationOutput::fromJSON(const nlohmann::json & _json, const ExperimentalFeatureSettings & xpSettings)
1302+
adl_serializer<DerivationOutput>::from_json(const json & _json, const ExperimentalFeatureSettings & xpSettings)
12981303
{
12991304
std::set<std::string_view> keys;
13001305
auto & json = getObject(_json);
@@ -1362,26 +1367,26 @@ DerivationOutput::fromJSON(const nlohmann::json & _json, const ExperimentalFeatu
13621367
}
13631368
}
13641369

1365-
nlohmann::json Derivation::toJSON() const
1370+
void adl_serializer<Derivation>::to_json(json & res, const Derivation & d)
13661371
{
1367-
nlohmann::json res = nlohmann::json::object();
1372+
res = nlohmann::json::object();
13681373

1369-
res["name"] = name;
1374+
res["name"] = d.name;
13701375

13711376
res["version"] = 3;
13721377

13731378
{
13741379
nlohmann::json & outputsObj = res["outputs"];
13751380
outputsObj = nlohmann::json::object();
1376-
for (auto & [outputName, output] : outputs) {
1381+
for (auto & [outputName, output] : d.outputs) {
13771382
outputsObj[outputName] = output;
13781383
}
13791384
}
13801385

13811386
{
13821387
auto & inputsList = res["inputSrcs"];
13831388
inputsList = nlohmann::json ::array();
1384-
for (auto & input : inputSrcs)
1389+
for (auto & input : d.inputSrcs)
13851390
inputsList.emplace_back(input);
13861391
}
13871392

@@ -1401,24 +1406,22 @@ nlohmann::json Derivation::toJSON() const
14011406
{
14021407
auto & inputDrvsObj = res["inputDrvs"];
14031408
inputDrvsObj = nlohmann::json::object();
1404-
for (auto & [inputDrv, inputNode] : inputDrvs.map) {
1409+
for (auto & [inputDrv, inputNode] : d.inputDrvs.map) {
14051410
inputDrvsObj[inputDrv.to_string()] = doInput(inputNode);
14061411
}
14071412
}
14081413
}
14091414

1410-
res["system"] = platform;
1411-
res["builder"] = builder;
1412-
res["args"] = args;
1413-
res["env"] = env;
1415+
res["system"] = d.platform;
1416+
res["builder"] = d.builder;
1417+
res["args"] = d.args;
1418+
res["env"] = d.env;
14141419

1415-
if (structuredAttrs)
1416-
res["structuredAttrs"] = structuredAttrs->structuredAttrs;
1417-
1418-
return res;
1420+
if (d.structuredAttrs)
1421+
res["structuredAttrs"] = d.structuredAttrs->structuredAttrs;
14191422
}
14201423

1421-
Derivation Derivation::fromJSON(const nlohmann::json & _json, const ExperimentalFeatureSettings & xpSettings)
1424+
Derivation adl_serializer<Derivation>::from_json(const json & _json, const ExperimentalFeatureSettings & xpSettings)
14221425
{
14231426
using nlohmann::detail::value_t;
14241427

@@ -1434,7 +1437,7 @@ Derivation Derivation::fromJSON(const nlohmann::json & _json, const Experimental
14341437
try {
14351438
auto outputs = getObject(valueAt(json, "outputs"));
14361439
for (auto & [outputName, output] : outputs) {
1437-
res.outputs.insert_or_assign(outputName, DerivationOutput::fromJSON(output, xpSettings));
1440+
res.outputs.insert_or_assign(outputName, adl_serializer<DerivationOutput>::from_json(output, xpSettings));
14381441
}
14391442
} catch (Error & e) {
14401443
e.addTrace({}, "while reading key 'outputs'");
@@ -1490,31 +1493,4 @@ Derivation Derivation::fromJSON(const nlohmann::json & _json, const Experimental
14901493
return res;
14911494
}
14921495

1493-
} // namespace nix
1494-
1495-
namespace nlohmann {
1496-
1497-
using namespace nix;
1498-
1499-
DerivationOutput
1500-
adl_serializer<DerivationOutput>::from_json(const json & json, const ExperimentalFeatureSettings & xpSettings)
1501-
{
1502-
return DerivationOutput::fromJSON(json, xpSettings);
1503-
}
1504-
1505-
void adl_serializer<DerivationOutput>::to_json(json & json, const DerivationOutput & c)
1506-
{
1507-
json = c.toJSON();
1508-
}
1509-
1510-
Derivation adl_serializer<Derivation>::from_json(const json & json, const ExperimentalFeatureSettings & xpSettings)
1511-
{
1512-
return Derivation::fromJSON(json, xpSettings);
1513-
}
1514-
1515-
void adl_serializer<Derivation>::to_json(json & json, const Derivation & c)
1516-
{
1517-
json = c.toJSON();
1518-
}
1519-
15201496
} // namespace nlohmann

src/libstore/include/nix/store/derivations.hh

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,6 @@ struct DerivationOutput
134134
*/
135135
std::optional<StorePath>
136136
path(const StoreDirConfig & store, std::string_view drvName, OutputNameView outputName) const;
137-
138-
nlohmann::json toJSON() const;
139-
/**
140-
* @param xpSettings Stop-gap to avoid globals during unit tests.
141-
*/
142-
static DerivationOutput
143-
fromJSON(const nlohmann::json & json, const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
144137
};
145138

146139
typedef std::map<std::string, DerivationOutput> DerivationOutputs;
@@ -390,10 +383,6 @@ struct Derivation : BasicDerivation
390383
{
391384
}
392385

393-
nlohmann::json toJSON() const;
394-
static Derivation
395-
fromJSON(const nlohmann::json & json, const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
396-
397386
bool operator==(const Derivation &) const = default;
398387
// TODO libc++ 16 (used by darwin) missing `std::map::operator <=>`, can't do yet.
399388
// auto operator <=> (const Derivation &) const = default;

src/nix/derivation-add.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct CmdAddDerivation : MixDryRun, StoreCommand
3333
{
3434
auto json = nlohmann::json::parse(drainFD(STDIN_FILENO));
3535

36-
auto drv = Derivation::fromJSON(json);
36+
auto drv = static_cast<Derivation>(json);
3737

3838
auto drvPath = writeDerivation(*store, drv, NoRepair, /* read only */ dryRun);
3939

src/nix/derivation-show.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ struct CmdShowDerivation : InstallablesCommand, MixPrintJSON
5858
if (!drvPath.isDerivation())
5959
continue;
6060

61-
jsonRoot[drvPath.to_string()] = store->readDerivation(drvPath).toJSON();
61+
jsonRoot[drvPath.to_string()] = store->readDerivation(drvPath);
6262
}
6363
printJSON(jsonRoot);
6464
}

0 commit comments

Comments
 (0)