From 4283f0139a82299c5240a25bb27ae84be3c99e13 Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Mon, 10 Oct 2022 14:48:05 -0400 Subject: feat(tvix/eval): Implement builtins.removeAttrs Change-Id: I28910991a0108436a42ac7bf3458f9180a44154e Reviewed-on: https://cl.tvl.fyi/c/depot/+/6928 Reviewed-by: Adam Joseph Reviewed-by: tazjin Tested-by: BuildkiteCI --- tvix/eval/src/builtins/mod.rs | 21 ++++++++++++++++++++- tvix/eval/src/tests/tvix_tests/eval-fail-remove.nix | 5 +++++ tvix/eval/src/tests/tvix_tests/eval-okay-remove.exp | 1 + tvix/eval/src/tests/tvix_tests/eval-okay-remove.nix | 5 +++++ 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tvix/eval/src/tests/tvix_tests/eval-fail-remove.nix create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-remove.exp create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-remove.nix diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index c603264c1e27..47e4eea63d90 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -4,7 +4,7 @@ //! available builtins in Nix. use std::cmp; -use std::collections::{BTreeMap, HashMap}; +use std::collections::{BTreeMap, HashMap, HashSet}; use std::path::PathBuf; use crate::{ @@ -362,6 +362,25 @@ fn pure_builtins() -> Vec { &[false, false], |args: Vec, vm: &mut VM| arithmetic_op!(&*args[0].force(vm)?, &*args[1].force(vm)?, *), ), + Builtin::new( + "removeAttrs", + &[true, true], + |args: Vec, _: &mut VM| { + let attrs = args[0].to_attrs()?; + let keys = args[1] + .to_list()? + .into_iter() + .map(|v| v.to_str()) + .collect::, _>>()?; + let mut res = BTreeMap::new(); + for (k, v) in attrs.iter() { + if !keys.contains(k) { + res.insert(k.clone(), v.clone()); + } + } + Ok(Value::attrs(NixAttrs::from_map(res))) + }, + ), Builtin::new("splitVersion", &[true], |args: Vec, _: &mut VM| { let s = args[0].to_str()?; let s = VersionPartsIter::new(s.as_str()); diff --git a/tvix/eval/src/tests/tvix_tests/eval-fail-remove.nix b/tvix/eval/src/tests/tvix_tests/eval-fail-remove.nix new file mode 100644 index 000000000000..539e0eb0a6f6 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/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/tvix/eval/src/tests/tvix_tests/eval-okay-remove.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-remove.exp new file mode 100644 index 000000000000..8d38505c1686 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-remove.exp @@ -0,0 +1 @@ +456 diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-remove.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-remove.nix new file mode 100644 index 000000000000..4ad5ba897fa7 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/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 -- cgit 1.4.1