Skip to content

Commit

Permalink
fetchClosure: Refactor: replace enableRewriting
Browse files Browse the repository at this point in the history
A single variable is nice and self-contained.
  • Loading branch information
roberth committed Jun 5, 2023
1 parent 051ed21 commit fe47475
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/libexpr/primops/fetchClosure.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,15 @@ static void runFetchClosureWithInputAddressedPath(EvalState & state, const PosId
state.mkStorePathString(fromPath, v);
}

typedef std::optional<StorePath> StorePathOrGap;

static void prim_fetchClosure(EvalState & state, const PosIdx pos, Value * * args, Value & v)
{
state.forceAttrs(*args[0], pos, "while evaluating the argument passed to builtins.fetchClosure");

std::optional<std::string> fromStoreUrl;
std::optional<StorePath> fromPath;
bool enableRewriting = false;
std::optional<StorePath> toPath;
std::optional<StorePathOrGap> toPath;
std::optional<bool> inputAddressedMaybe;

for (auto & attr : *args[0]->attrs) {
Expand All @@ -123,8 +124,11 @@ static void prim_fetchClosure(EvalState & state, const PosIdx pos, Value * * arg

else if (attrName == "toPath") {
state.forceValue(*attr.value, attr.pos);
enableRewriting = true;
if (attr.value->type() != nString || attr.value->string.s != std::string("")) {
bool isEmptyString = attr.value->type() == nString && attr.value->string.s == std::string("");
if (isEmptyString) {
toPath = StorePathOrGap {};
}
else {
NixStringContext context;
toPath = state.coerceToStorePath(attr.pos, *attr.value, context, attrHint());
}
Expand Down Expand Up @@ -160,7 +164,6 @@ static void prim_fetchClosure(EvalState & state, const PosIdx pos, Value * * arg
"toPath"),
.errPos = state.positions[pos]
});
assert(!enableRewriting);
}

if (!fromStoreUrl)
Expand All @@ -187,7 +190,7 @@ static void prim_fetchClosure(EvalState & state, const PosIdx pos, Value * * arg

auto fromStore = openStore(parsedURL.to_string());

if (enableRewriting)
if (toPath)
runFetchClosureWithRewrite(state, pos, *fromStore, *fromPath, *toPath, v);
else if (inputAddressed)
runFetchClosureWithInputAddressedPath(state, pos, *fromStore, *fromPath, v);
Expand Down

0 comments on commit fe47475

Please sign in to comment.