feat(tvix/eval): Support builtins.hasAttr

See unit tests for examples :)

Change-Id: Ieec51d780a7762cc455ca03a9dc1648a0711924a
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6553
Reviewed-by: wpcarro <wpcarro@gmail.com>
Autosubmit: wpcarro <wpcarro@gmail.com>
Reviewed-by: sterni <sternenseemann@systemli.org>
Tested-by: BuildkiteCI
This commit is contained in:
William Carroll 2022-09-06 14:13:06 -07:00 committed by clbot
parent 5b165e7318
commit 059f4b964f
3 changed files with 16 additions and 0 deletions

View file

@ -134,6 +134,12 @@ fn pure_builtins() -> Vec<Builtin> {
Builtin::new("length", &[true], |args, _| {
Ok(Value::Integer(args[0].to_list()?.len() as i64))
}),
Builtin::new("hasAttr", &[true, true], |args, _| {
let k = args[0].to_str()?;
let xs = args[1].to_attrs()?;
Ok(Value::Bool(xs.contains(k.as_str())))
}),
Builtin::new("head", &[true], |args, _| match args[0].to_list()?.get(0) {
Some(x) => Ok(x.clone()),
None => Err(ErrorKind::IndexOutOfBounds { index: 0 }),

View file

@ -0,0 +1 @@
[ true true true false false true false ]

View file

@ -0,0 +1,9 @@
[
(builtins.hasAttr "foo" { foo = 1; bar = 2; baz = 3; })
(builtins.hasAttr "bar" { foo = 1; bar = 2; baz = 3; })
(builtins.hasAttr "baz" { foo = 1; bar = 2; baz = 3; })
(builtins.hasAttr "FOO" { foo = 1; bar = 2; baz = 3; })
(builtins.hasAttr "foo" {})
(builtins.hasAttr ("f" + "o" + "o") { foo = 1; })
(builtins.hasAttr ("b" + "a" + "r") { foo = 1; })
]