From 059f4b964fc957692063db27bccf70e65e330c77 Mon Sep 17 00:00:00 2001 From: William Carroll Date: Tue, 6 Sep 2022 14:13:06 -0700 Subject: 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 Autosubmit: wpcarro Reviewed-by: sterni Tested-by: BuildkiteCI --- tvix/eval/src/builtins/mod.rs | 6 ++++++ tvix/eval/src/tests/tvix_tests/eval-okay-builtins-hasattr.exp | 1 + tvix/eval/src/tests/tvix_tests/eval-okay-builtins-hasattr.nix | 9 +++++++++ 3 files changed, 16 insertions(+) create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-builtins-hasattr.exp create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-builtins-hasattr.nix (limited to 'tvix') diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index a5ca2114e1f7..f6af16f96f9d 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -134,6 +134,12 @@ fn pure_builtins() -> Vec { 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 }), diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-hasattr.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-hasattr.exp new file mode 100644 index 000000000000..541fe347cbcd --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-hasattr.exp @@ -0,0 +1 @@ +[ true true true false false true false ] diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-hasattr.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-hasattr.nix new file mode 100644 index 000000000000..e87e186b641d --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-hasattr.nix @@ -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; }) +] -- cgit 1.4.1