mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-25 23:36:16 +02:00
* New builtin functions builtins.{hasAttr, getAttr} to check for
attribute existence and to return an attribute from an attribute set, respectively. Example: `hasAttr "foo" {foo = 1;}'. They differ from the `?' and `.' operators in that the attribute name is an arbitrary expression. (NIX-61)
This commit is contained in:
parent
666babbbfa
commit
8a1ab709a4
3 changed files with 27 additions and 0 deletions
|
@ -703,6 +703,20 @@ static Expr primCurrentTime(EvalState & state, const ATermVector & args)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static Expr primGetAttr(EvalState & state, const ATermVector & args)
|
||||||
|
{
|
||||||
|
string attr = evalString(state, args[0]);
|
||||||
|
return evalExpr(state, makeSelect(args[1], toATerm(attr)));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static Expr primHasAttr(EvalState & state, const ATermVector & args)
|
||||||
|
{
|
||||||
|
string attr = evalString(state, args[0]);
|
||||||
|
return evalExpr(state, makeOpHasAttr(args[1], toATerm(attr)));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static Expr primRemoveAttrs(EvalState & state, const ATermVector & args)
|
static Expr primRemoveAttrs(EvalState & state, const ATermVector & args)
|
||||||
{
|
{
|
||||||
ATermMap attrs(128); /* !!! */
|
ATermMap attrs(128); /* !!! */
|
||||||
|
@ -749,6 +763,8 @@ void EvalState::addPrimOps()
|
||||||
addPrimOp("abort", 1, primAbort);
|
addPrimOp("abort", 1, primAbort);
|
||||||
|
|
||||||
addPrimOp("map", 2, primMap);
|
addPrimOp("map", 2, primMap);
|
||||||
|
addPrimOp("__getAttr", 2, primGetAttr);
|
||||||
|
addPrimOp("__hasAttr", 2, primHasAttr);
|
||||||
addPrimOp("removeAttrs", 2, primRemoveAttrs);
|
addPrimOp("removeAttrs", 2, primRemoveAttrs);
|
||||||
addPrimOp("relativise", 2, primRelativise);
|
addPrimOp("relativise", 2, primRelativise);
|
||||||
}
|
}
|
||||||
|
|
1
tests/lang/eval-okay-attrs2.exp
Normal file
1
tests/lang/eval-okay-attrs2.exp
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Int(987)
|
10
tests/lang/eval-okay-attrs2.nix
Normal file
10
tests/lang/eval-okay-attrs2.nix
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
let {
|
||||||
|
as = { x = 123; y = 456; } // { z = 789; } // { z = 987; };
|
||||||
|
|
||||||
|
A = "a";
|
||||||
|
Z = "z";
|
||||||
|
|
||||||
|
body = if builtins.hasAttr A as
|
||||||
|
then builtins.getAttr A as
|
||||||
|
else assert builtins.hasAttr Z as; builtins.getAttr Z as;
|
||||||
|
}
|
Loading…
Reference in a new issue