feat(tvix/eval): Support builtins.getAttr

Support looking-up values from attrsets by their keys.

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

View file

@ -131,6 +131,17 @@ fn pure_builtins() -> Vec<Builtin> {
}
}
}),
Builtin::new("getAttr", &[true, true], |args, _| {
let k = args[0].to_str()?;
let xs = args[1].to_attrs()?;
match xs.select(k.as_str()) {
Some(x) => Ok(x.clone()),
None => Err(ErrorKind::AttributeNotFound {
name: k.to_string(),
}),
}
}),
Builtin::new("length", &[true], |args, _| {
Ok(Value::Integer(args[0].to_list()?.len() as i64))
}),

View file

@ -0,0 +1 @@
[ 1 2 3 { bar = { baz = 3; }; } ]

View file

@ -0,0 +1,6 @@
[
(builtins.getAttr "foo" { foo = 1; bar = 2; baz = 3; })
(builtins.getAttr "bar" { foo = 1; bar = 2; baz = 3; })
(builtins.getAttr "baz" { foo = 1; bar = 2; baz = 3; })
(builtins.getAttr "foo" { foo = { bar = { baz = 3; }; }; })
]