1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-12-13 12:31:04 +01:00

Use types to show that structured attrs are always JSON objects

Before we just had partial code accessing it. Now, we use
`nlohmann::json::object_t`, which is a `std::map`, to enforce this by
construction.
This commit is contained in:
John Ericson 2025-10-25 13:22:59 -04:00
parent bef3c37cb2
commit 7e53afd8b9
6 changed files with 22 additions and 20 deletions

View file

@ -33,7 +33,8 @@ std::optional<StructuredAttrs> StructuredAttrs::tryExtract(StringPairs & env)
std::pair<std::string_view, std::string> StructuredAttrs::unparse() const
{
return {envVarName, structuredAttrs.dump()};
// TODO don't copy the JSON object just to dump it.
return {envVarName, static_cast<nlohmann::json>(structuredAttrs).dump()};
}
void StructuredAttrs::checkKeyNotInUse(const StringPairs & env)
@ -97,7 +98,7 @@ static nlohmann::json pathInfoToJSON(Store & store, const StorePathSet & storePa
return jsonList;
}
nlohmann::json StructuredAttrs::prepareStructuredAttrs(
nlohmann::json::object_t StructuredAttrs::prepareStructuredAttrs(
Store & store,
const DerivationOptions & drvOptions,
const StorePathSet & inputPaths,
@ -120,7 +121,7 @@ nlohmann::json StructuredAttrs::prepareStructuredAttrs(
return json;
}
std::string StructuredAttrs::writeShell(const nlohmann::json & json)
std::string StructuredAttrs::writeShell(const nlohmann::json::object_t & json)
{
auto handleSimpleType = [](const nlohmann::json & value) -> std::optional<std::string> {
@ -144,7 +145,7 @@ std::string StructuredAttrs::writeShell(const nlohmann::json & json)
std::string jsonSh;
for (auto & [key, value] : json.items()) {
for (auto & [key, value] : json) {
if (!std::regex_match(key, shVarName))
continue;