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

Introduce AttrPath type

This is basically an alias for std::vector<Symbol>.
This commit is contained in:
Eelco Dolstra 2025-12-05 13:41:59 +01:00
parent 294e68a3f6
commit 20fc54c00d
9 changed files with 64 additions and 55 deletions

View file

@ -1,5 +1,6 @@
#include "nix/expr/attr-path.hh"
#include "nix/expr/eval-inline.hh"
#include "nix/util/strings-inline.hh"
namespace nix {
@ -30,14 +31,24 @@ static Strings parseAttrPath(std::string_view s)
return res;
}
std::vector<Symbol> parseAttrPath(EvalState & state, std::string_view s)
AttrPath AttrPath::parse(EvalState & state, std::string_view s)
{
std::vector<Symbol> res;
AttrPath res;
for (auto & a : parseAttrPath(s))
res.push_back(state.symbols.create(a));
return res;
}
std::string AttrPath::to_string(EvalState & state) const
{
return dropEmptyInitThenConcatStringsSep(".", state.symbols.resolve({*this}));
}
std::vector<SymbolStr> AttrPath::resolve(EvalState & state) const
{
return state.symbols.resolve({*this});
}
std::pair<Value *, PosIdx>
findAlongAttrPath(EvalState & state, const std::string & attrPath, Bindings & autoArgs, Value & vIn)
{

View file

@ -364,7 +364,7 @@ void AttrCursor::fetchCachedValue()
throw CachedEvalError(parent->first, parent->second);
}
std::vector<Symbol> AttrCursor::getAttrPath() const
AttrPath AttrCursor::getAttrPath() const
{
if (parent) {
auto attrPath = parent->first->getAttrPath();
@ -374,7 +374,7 @@ std::vector<Symbol> AttrCursor::getAttrPath() const
return {};
}
std::vector<Symbol> AttrCursor::getAttrPath(Symbol name) const
AttrPath AttrCursor::getAttrPath(Symbol name) const
{
auto attrPath = getAttrPath();
attrPath.push_back(name);
@ -383,12 +383,12 @@ std::vector<Symbol> AttrCursor::getAttrPath(Symbol name) const
std::string AttrCursor::getAttrPathStr() const
{
return dropEmptyInitThenConcatStringsSep(".", root->state.symbols.resolve(getAttrPath()));
return getAttrPath().to_string(root->state);
}
std::string AttrCursor::getAttrPathStr(Symbol name) const
{
return dropEmptyInitThenConcatStringsSep(".", root->state.symbols.resolve(getAttrPath(name)));
return getAttrPath(name).to_string(root->state);
}
Value & AttrCursor::forceValue()
@ -511,7 +511,7 @@ ref<AttrCursor> AttrCursor::getAttr(std::string_view name)
return getAttr(root->state.symbols.create(name));
}
OrSuggestions<ref<AttrCursor>> AttrCursor::findAlongAttrPath(const std::vector<Symbol> & attrPath)
OrSuggestions<ref<AttrCursor>> AttrCursor::findAlongAttrPath(const AttrPath & attrPath)
{
auto res = shared_from_this();
for (auto & attr : attrPath) {

View file

@ -19,6 +19,15 @@ findAlongAttrPath(EvalState & state, const std::string & attrPath, Bindings & au
*/
std::pair<SourcePath, uint32_t> findPackageFilename(EvalState & state, Value & v, std::string what);
std::vector<Symbol> parseAttrPath(EvalState & state, std::string_view s);
struct AttrPath : std::vector<Symbol>
{
using std::vector<Symbol>::vector;
static AttrPath parse(EvalState & state, std::string_view s);
std::string to_string(EvalState & state) const;
std::vector<SymbolStr> resolve(EvalState & state) const;
};
} // namespace nix

View file

@ -4,6 +4,7 @@
#include "nix/util/sync.hh"
#include "nix/util/hash.hh"
#include "nix/expr/eval.hh"
#include "nix/expr/attr-path.hh"
#include <functional>
#include <variant>
@ -124,9 +125,9 @@ public:
Value * value = nullptr,
std::optional<std::pair<AttrId, AttrValue>> && cachedValue = {});
std::vector<Symbol> getAttrPath() const;
AttrPath getAttrPath() const;
std::vector<Symbol> getAttrPath(Symbol name) const;
AttrPath getAttrPath(Symbol name) const;
std::string getAttrPathStr() const;
@ -146,7 +147,7 @@ public:
* Get an attribute along a chain of attrsets. Note that this does
* not auto-call functors or functions.
*/
OrSuggestions<ref<AttrCursor>> findAlongAttrPath(const std::vector<Symbol> & attrPath);
OrSuggestions<ref<AttrCursor>> findAlongAttrPath(const AttrPath & attrPath);
std::string getString();

View file

@ -290,7 +290,7 @@ public:
return Symbol(*symbols.insert(SymbolStr::Key{store, s, buffer}).first);
}
std::vector<SymbolStr> resolve(const std::vector<Symbol> & symbols) const
std::vector<SymbolStr> resolve(const std::span<const Symbol> & symbols) const
{
std::vector<SymbolStr> result;
result.reserve(symbols.size());