diff options
author | William Carroll <wpcarro@gmail.com> | 2022-09-06T21·20-0700 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2022-09-19T16·29+0000 |
commit | 61cce49a8a8ddb7b7c4783d08946091544305d5b (patch) | |
tree | 5ea15298f0cfdf071a4a66692296565d34f0d762 | |
parent | 059f4b964fc957692063db27bccf70e65e330c77 (diff) |
feat(tvix/eval): Support builtins.getAttr r/4928
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
-rw-r--r-- | tvix/eval/src/builtins/mod.rs | 11 | ||||
-rw-r--r-- | tvix/eval/src/tests/tvix_tests/eval-okay-builtins-getattr.exp | 1 | ||||
-rw-r--r-- | tvix/eval/src/tests/tvix_tests/eval-okay-builtins-getattr.nix | 6 |
3 files changed, 18 insertions, 0 deletions
diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index f6af16f96f9d..9714c96d05d8 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -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)) }), diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-getattr.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-getattr.exp new file mode 100644 index 000000000000..89fa6c681064 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-getattr.exp @@ -0,0 +1 @@ +[ 1 2 3 { bar = { baz = 3; }; } ] diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-getattr.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-getattr.nix new file mode 100644 index 000000000000..87a2adbcd3dd --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-getattr.nix @@ -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; }; }; }) +] |