From 040140dd1c3c11e3aa1ca486c3f3596cbe99c008 Mon Sep 17 00:00:00 2001
From: Eelco Dolstra <e.dolstra@tudelft.nl>
Date: Wed, 18 May 2005 17:19:21 +0000
Subject: [PATCH] * 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.

---
 src/libexpr/primops.cc          | 18 ++++++++++++++++++
 tests/lang/eval-fail-remove.nix |  5 +++++
 tests/lang/eval-okay-remove.exp |  1 +
 tests/lang/eval-okay-remove.nix |  5 +++++
 4 files changed, 29 insertions(+)
 create mode 100644 tests/lang/eval-fail-remove.nix
 create mode 100644 tests/lang/eval-okay-remove.exp
 create mode 100644 tests/lang/eval-okay-remove.nix

diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc
index e3dd2db18..b0986028e 100644
--- a/src/libexpr/primops.cc
+++ b/src/libexpr/primops.cc
@@ -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()
 {
     addPrimOp("true", 0, primTrue);
@@ -460,6 +477,7 @@ void EvalState::addPrimOps()
     addPrimOp("isNull", 1, primIsNull);
 
     addPrimOp("map", 2, primMap);
+    addPrimOp("removeAttrs", 2, primRemoveAttrs);
 }
 
 
diff --git a/tests/lang/eval-fail-remove.nix b/tests/lang/eval-fail-remove.nix
new file mode 100644
index 000000000..539e0eb0a
--- /dev/null
+++ b/tests/lang/eval-fail-remove.nix
@@ -0,0 +1,5 @@
+let {
+  attrs = {x = 123; y = 456;};
+
+  body = (removeAttrs attrs ["x"]).x;
+}
\ No newline at end of file
diff --git a/tests/lang/eval-okay-remove.exp b/tests/lang/eval-okay-remove.exp
new file mode 100644
index 000000000..e22e6b1b2
--- /dev/null
+++ b/tests/lang/eval-okay-remove.exp
@@ -0,0 +1 @@
+Int(456)
diff --git a/tests/lang/eval-okay-remove.nix b/tests/lang/eval-okay-remove.nix
new file mode 100644
index 000000000..4ad5ba897
--- /dev/null
+++ b/tests/lang/eval-okay-remove.nix
@@ -0,0 +1,5 @@
+let {
+  attrs = {x = 123; y = 456;};
+
+  body = (removeAttrs attrs ["x"]).y;
+}
\ No newline at end of file
-- 
GitLab