feat(tvix/eval): Implement builtins.removeAttrs
Change-Id: I28910991a0108436a42ac7bf3458f9180a44154e Reviewed-on: https://cl.tvl.fyi/c/depot/+/6928 Reviewed-by: Adam Joseph <adam@westernsemico.com> Reviewed-by: tazjin <tazjin@tvl.su> Tested-by: BuildkiteCI
This commit is contained in:
parent
562c50fadd
commit
4283f0139a
4 changed files with 31 additions and 1 deletions
|
@ -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<Builtin> {
|
|||
&[false, false],
|
||||
|args: Vec<Value>, vm: &mut VM| arithmetic_op!(&*args[0].force(vm)?, &*args[1].force(vm)?, *),
|
||||
),
|
||||
Builtin::new(
|
||||
"removeAttrs",
|
||||
&[true, true],
|
||||
|args: Vec<Value>, _: &mut VM| {
|
||||
let attrs = args[0].to_attrs()?;
|
||||
let keys = args[1]
|
||||
.to_list()?
|
||||
.into_iter()
|
||||
.map(|v| v.to_str())
|
||||
.collect::<Result<HashSet<_>, _>>()?;
|
||||
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<Value>, _: &mut VM| {
|
||||
let s = args[0].to_str()?;
|
||||
let s = VersionPartsIter::new(s.as_str());
|
||||
|
|
5
tvix/eval/src/tests/tvix_tests/eval-fail-remove.nix
Normal file
5
tvix/eval/src/tests/tvix_tests/eval-fail-remove.nix
Normal file
|
@ -0,0 +1,5 @@
|
|||
let {
|
||||
attrs = {x = 123; y = 456;};
|
||||
|
||||
body = (removeAttrs attrs ["x"]).x;
|
||||
}
|
1
tvix/eval/src/tests/tvix_tests/eval-okay-remove.exp
Normal file
1
tvix/eval/src/tests/tvix_tests/eval-okay-remove.exp
Normal file
|
@ -0,0 +1 @@
|
|||
456
|
5
tvix/eval/src/tests/tvix_tests/eval-okay-remove.nix
Normal file
5
tvix/eval/src/tests/tvix_tests/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