Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libnixt: mangle submodule options to it's declaration #518

Merged
merged 3 commits into from
Jun 2, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 45 additions & 12 deletions libnixt/lib/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,43 @@

using namespace nixt;

namespace {

/// \brief Check if the value \p V is a submodule.
bool isSubmodule(nix::EvalState &State, nix::Value &V) {
try {
nix::Value &Type = selectAttr(State, V, State.sType);
if (checkField(State, Type, "name", "submodule")) {
return true;
}
} catch (nix::TypeError &) {
// The value is not an attrset, thus it definitely cannot be a submodule.
return false;
} catch (nix::AttrPathNotFound &) {
// The value has no "type" field.
return false;
}
return false;
}

/// \brief Do proper operations to get options declaration on submodule type.
nix::Value getSubOptions(nix::EvalState &State, nix::Value &Type) {
// Mangle the value if only V.type.name == "submodule"
inclyc marked this conversation as resolved.
Show resolved Hide resolved
// If "V" is an option, and the type is "submodule".
// For example, programs.nixvim has all options nested into this attrpath.
nix::Value &GetSubOptions =
selectAttr(State, Type, State.symbols.create("getSubOptions"));

nix::Value EmptyList;
EmptyList.mkList(0);
// Invoke "GetSubOptions"
nix::Value VResult;
State.callFunction(GetSubOptions, EmptyList, VResult, nix::noPos);
return VResult;
}

} // namespace

std::optional<nix::Value> nixt::getField(nix::EvalState &State, nix::Value &V,
std::string_view Field) {
State.forceValue(V, nix::noPos);
Expand Down Expand Up @@ -113,6 +150,12 @@ nix::Value &nixt::selectAttrPath(nix::EvalState &State, nix::Value &V,
nix::Value nixt::selectOptions(nix::EvalState &State, nix::Value &V,
std::vector<nix::Symbol>::const_iterator Begin,
std::vector<nix::Symbol>::const_iterator End) {
// Always try to mangle the value if it is a submodule
if (isSubmodule(State, V)) {
nix::Value &Type = selectAttr(State, V, State.sType);
V = getSubOptions(State, Type);
}

if (Begin == End)
return V;

Expand All @@ -131,18 +174,8 @@ nix::Value nixt::selectOptions(nix::EvalState &State, nix::Value &V,
selectAttr(State, NestedTypes, State.symbols.create("elemType"));

if (checkField(State, ElemType, "name", "submodule")) {
// Current iterator may be ommited, and V becomes "V.getSubOptions []"
nix::Value &GetSubOptions =
selectAttr(State, ElemType, State.symbols.create("getSubOptions"));

nix::Value EmptyList;
EmptyList.mkList(0);

// Invoke "GetSubOptions"
nix::Value Next;
State.callFunction(GetSubOptions, EmptyList, Next, nix::noPos);

return selectOptions(State, Next, ++Begin, End);
nix::Value ElemOptions = getSubOptions(State, ElemType);
return selectOptions(State, ElemOptions, ++Begin, End);
}
}
}
Expand Down
Loading