Skip to content

Commit

Permalink
Style
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Jul 12, 2021
1 parent e06c272 commit 02dff9e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
28 changes: 13 additions & 15 deletions src/libstore/parsed-derivations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ bool ParsedDerivation::substitutesAllowed() const
}

static std::regex shVarName("[A-Za-z_][A-Za-z0-9_]*");

std::optional<nlohmann::json> ParsedDerivation::prepareStructuredAttrs(Store & store, const StorePathSet & inputPaths)
{
auto structuredAttrs = getStructuredAttrs();
Expand All @@ -135,9 +136,8 @@ std::optional<nlohmann::json> ParsedDerivation::prepareStructuredAttrs(Store & s

/* Add an "outputs" object containing the output paths. */
nlohmann::json outputs;
for (auto & i : drv.outputs) {
for (auto & i : drv.outputs)
outputs[i.first] = hashPlaceholder(i.first);
}
json["outputs"] = outputs;

/* Handle exportReferencesGraph. */
Expand Down Expand Up @@ -165,7 +165,7 @@ std::optional<nlohmann::json> ParsedDerivation::prepareStructuredAttrs(Store & s
namely, strings, integers, nulls, Booleans, and arrays and
objects consisting entirely of those values. (So nested
arrays or objects are not supported.) */
std::string writeStructuredAttrsShell(nlohmann::json & json)
std::string writeStructuredAttrsShell(const nlohmann::json & json)
{

auto handleSimpleType = [](const nlohmann::json & value) -> std::optional<std::string> {
Expand All @@ -189,42 +189,40 @@ std::string writeStructuredAttrsShell(nlohmann::json & json)

std::string jsonSh;

for (auto i = json.begin(); i != json.end(); ++i) {

if (!std::regex_match(i.key(), shVarName)) continue;
for (auto & [key, value] : json.items()) {

auto & value = i.value();
if (!std::regex_match(key, shVarName)) continue;

auto s = handleSimpleType(value);
if (s)
jsonSh += fmt("declare %s=%s\n", i.key(), *s);
jsonSh += fmt("declare %s=%s\n", key, *s);

else if (value.is_array()) {
std::string s2;
bool good = true;

for (auto i = value.begin(); i != value.end(); ++i) {
auto s3 = handleSimpleType(i.value());
for (auto & value2 : value) {
auto s3 = handleSimpleType(value2);
if (!s3) { good = false; break; }
s2 += *s3; s2 += ' ';
}

if (good)
jsonSh += fmt("declare -a %s=(%s)\n", i.key(), s2);
jsonSh += fmt("declare -a %s=(%s)\n", key, s2);
}

else if (value.is_object()) {
std::string s2;
bool good = true;

for (auto i = value.begin(); i != value.end(); ++i) {
auto s3 = handleSimpleType(i.value());
for (auto & [key2, value2] : value.items()) {
auto s3 = handleSimpleType(value2);
if (!s3) { good = false; break; }
s2 += fmt("[%s]=%s ", shellEscape(i.key()), *s3);
s2 += fmt("[%s]=%s ", shellEscape(key2), *s3);
}

if (good)
jsonSh += fmt("declare -A %s=(%s)\n", i.key(), s2);
jsonSh += fmt("declare -A %s=(%s)\n", key, s2);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libstore/parsed-derivations.hh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ public:
std::optional<nlohmann::json> prepareStructuredAttrs(Store & store, const StorePathSet & inputPaths);
};

std::string writeStructuredAttrsShell(nlohmann::json & json);
std::string writeStructuredAttrsShell(const nlohmann::json & json);

}

0 comments on commit 02dff9e

Please sign in to comment.