From 61cce49a8a8ddb7b7c4783d08946091544305d5b Mon Sep 17 00:00:00 2001 From: William Carroll Date: Tue, 6 Sep 2022 14:20:14 -0700 Subject: 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 Reviewed-by: sterni Autosubmit: wpcarro Tested-by: BuildkiteCI --- tvix/eval/src/builtins/mod.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'tvix/eval/src/builtins') 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::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)) }), -- cgit 1.4.1