2006-07-26 18:05:15 +03:00
|
|
|
#include "attr-path.hh"
|
2012-03-05 23:04:40 +02:00
|
|
|
#include "eval-inline.hh"
|
2006-09-05 00:06:23 +03:00
|
|
|
#include "util.hh"
|
|
|
|
|
|
|
|
|
|
|
|
namespace nix {
|
2006-07-26 18:05:15 +03:00
|
|
|
|
|
|
|
|
2013-09-03 16:17:51 +03:00
|
|
|
Value * findAlongAttrPath(EvalState & state, const string & attrPath,
|
|
|
|
Bindings & autoArgs, Value & vIn)
|
2006-07-26 18:05:15 +03:00
|
|
|
{
|
2012-09-19 22:43:23 +03:00
|
|
|
Strings tokens = tokenizeString<Strings>(attrPath, ".");
|
2006-07-26 18:05:15 +03:00
|
|
|
|
|
|
|
Error attrError =
|
|
|
|
Error(format("attribute selection path `%1%' does not match expression") % attrPath);
|
|
|
|
|
|
|
|
string curPath;
|
2010-04-07 18:47:06 +03:00
|
|
|
|
2013-09-03 16:17:51 +03:00
|
|
|
Value * v = &vIn;
|
2013-09-02 17:29:15 +03:00
|
|
|
|
2010-04-07 18:47:06 +03:00
|
|
|
foreach (Strings::iterator, i, tokens) {
|
2006-07-26 18:05:15 +03:00
|
|
|
|
|
|
|
if (!curPath.empty()) curPath += ".";
|
|
|
|
curPath += *i;
|
|
|
|
|
|
|
|
/* Is *i an index (integer) or a normal attribute name? */
|
|
|
|
enum { apAttr, apIndex } apType = apAttr;
|
|
|
|
string attr = *i;
|
2013-09-02 17:39:17 +03:00
|
|
|
unsigned int attrIndex;
|
2006-07-26 18:05:15 +03:00
|
|
|
if (string2Int(attr, attrIndex)) apType = apIndex;
|
|
|
|
|
|
|
|
/* Evaluate the expression. */
|
2013-09-03 16:17:51 +03:00
|
|
|
Value * vNew = state.allocValue();
|
|
|
|
state.autoCallFunction(autoArgs, *v, *vNew);
|
|
|
|
v = vNew;
|
|
|
|
state.forceValue(*v);
|
2006-07-26 18:05:15 +03:00
|
|
|
|
|
|
|
/* It should evaluate to either an attribute set or an
|
|
|
|
expression, according to what is specified in the
|
|
|
|
attrPath. */
|
|
|
|
|
|
|
|
if (apType == apAttr) {
|
|
|
|
|
2013-09-03 16:17:51 +03:00
|
|
|
if (v->type != tAttrs)
|
2006-07-26 18:05:15 +03:00
|
|
|
throw TypeError(
|
|
|
|
format("the expression selected by the selection path `%1%' should be an attribute set but is %2%")
|
2013-09-03 16:17:51 +03:00
|
|
|
% curPath % showType(*v));
|
2006-07-26 18:05:15 +03:00
|
|
|
|
2013-09-03 16:17:51 +03:00
|
|
|
Bindings::iterator a = v->attrs->find(state.symbols.create(attr));
|
|
|
|
if (a == v->attrs->end())
|
2010-04-07 18:47:06 +03:00
|
|
|
throw Error(format("attribute `%1%' in selection path `%2%' not found") % attr % curPath);
|
2013-09-03 16:17:51 +03:00
|
|
|
v = &*a->value;
|
2006-07-26 18:05:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (apType == apIndex) {
|
|
|
|
|
2013-09-03 16:17:51 +03:00
|
|
|
if (v->type != tList)
|
2006-07-26 18:05:15 +03:00
|
|
|
throw TypeError(
|
|
|
|
format("the expression selected by the selection path `%1%' should be a list but is %2%")
|
2013-09-03 16:17:51 +03:00
|
|
|
% curPath % showType(*v));
|
2006-07-26 18:05:15 +03:00
|
|
|
|
2013-09-03 16:17:51 +03:00
|
|
|
if (attrIndex >= v->list.length)
|
2010-04-07 18:47:06 +03:00
|
|
|
throw Error(format("list index %1% in selection path `%2%' is out of range") % attrIndex % curPath);
|
|
|
|
|
2013-09-03 16:17:51 +03:00
|
|
|
v = v->list.elems[attrIndex];
|
2006-07-26 18:05:15 +03:00
|
|
|
}
|
2013-09-02 17:29:15 +03:00
|
|
|
|
2006-07-26 18:05:15 +03:00
|
|
|
}
|
2013-09-03 16:17:51 +03:00
|
|
|
|
|
|
|
return v;
|
2006-07-26 18:05:15 +03:00
|
|
|
}
|
2006-09-05 00:06:23 +03:00
|
|
|
|
2013-09-02 17:29:15 +03:00
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
}
|