mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-29 09:06:15 +02:00
* Added a primop `removeAttrs' to remove attributes from a set, e.g.,
`removeAttrs attrs ["x", "y"]' returns the set `attrs' with the attributes named `x' and `y' removed. It is not an error for the named attributes to be missing from the input set.
This commit is contained in:
parent
109cde6706
commit
040140dd1c
4 changed files with 29 additions and 0 deletions
|
@ -444,6 +444,23 @@ static Expr primCurrentTime(EvalState & state, const ATermVector & args)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static Expr primRemoveAttrs(EvalState & state, const ATermVector & args)
|
||||||
|
{
|
||||||
|
ATermMap attrs;
|
||||||
|
queryAllAttrs(evalExpr(state, args[0]), attrs, true);
|
||||||
|
|
||||||
|
ATermList list;
|
||||||
|
if (!matchList(evalExpr(state, args[1]), list))
|
||||||
|
throw Error("`removeAttrs' expects a list as its second argument");
|
||||||
|
|
||||||
|
for (ATermIterator i(list); i; ++i)
|
||||||
|
/* It's not an error for *i not to exist. */
|
||||||
|
attrs.remove(evalString(state, *i));
|
||||||
|
|
||||||
|
return makeAttrs(attrs);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void EvalState::addPrimOps()
|
void EvalState::addPrimOps()
|
||||||
{
|
{
|
||||||
addPrimOp("true", 0, primTrue);
|
addPrimOp("true", 0, primTrue);
|
||||||
|
@ -460,6 +477,7 @@ void EvalState::addPrimOps()
|
||||||
addPrimOp("isNull", 1, primIsNull);
|
addPrimOp("isNull", 1, primIsNull);
|
||||||
|
|
||||||
addPrimOp("map", 2, primMap);
|
addPrimOp("map", 2, primMap);
|
||||||
|
addPrimOp("removeAttrs", 2, primRemoveAttrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
5
tests/lang/eval-fail-remove.nix
Normal file
5
tests/lang/eval-fail-remove.nix
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
let {
|
||||||
|
attrs = {x = 123; y = 456;};
|
||||||
|
|
||||||
|
body = (removeAttrs attrs ["x"]).x;
|
||||||
|
}
|
1
tests/lang/eval-okay-remove.exp
Normal file
1
tests/lang/eval-okay-remove.exp
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Int(456)
|
5
tests/lang/eval-okay-remove.nix
Normal file
5
tests/lang/eval-okay-remove.nix
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
let {
|
||||||
|
attrs = {x = 123; y = 456;};
|
||||||
|
|
||||||
|
body = (removeAttrs attrs ["x"]).y;
|
||||||
|
}
|
Loading…
Reference in a new issue