From 3f45f6191d44f0abdab75f86827a8f26ff9c496f Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Thu, 13 Oct 2022 00:04:55 -0400 Subject: feat(tvix/eval): Implement builtins.intersectAttrs Change-Id: Iaba9bcfa19f283cd0c1931be2f211e2528a1a940 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6998 Tested-by: BuildkiteCI Reviewed-by: tazjin Reviewed-by: kanepyork --- tvix/eval/src/builtins/mod.rs | 15 +++++++++++++++ .../src/tests/tvix_tests/eval-okay-intersectattrs.exp | 1 + .../src/tests/tvix_tests/eval-okay-intersectattrs.nix | 3 +++ 3 files changed, 19 insertions(+) create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-intersectattrs.exp create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-intersectattrs.nix (limited to 'tvix/eval') diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index 0082d36d57..03466af65f 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -309,6 +309,21 @@ fn pure_builtins() -> Vec { None => Err(ErrorKind::IndexOutOfBounds { index: 0 }), } }), + Builtin::new( + "intersectAttrs", + &[true, true], + |args: Vec, _: &mut VM| { + let mut res = BTreeMap::new(); + let attrs1 = args[0].to_attrs()?; + let attrs2 = args[1].to_attrs()?; + for (k, v) in attrs2.iter() { + if attrs1.contains(k) { + res.insert(k.clone(), v.clone()); + } + } + Ok(Value::attrs(NixAttrs::from_map(res))) + }, + ), // For `is*` predicates we force manually, as Value::force also unwraps any Thunks Builtin::new("isAttrs", &[false], |args: Vec, vm: &mut VM| { let value = args[0].force(vm)?; diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-intersectattrs.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-intersectattrs.exp new file mode 100644 index 0000000000..25001b211f --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-intersectattrs.exp @@ -0,0 +1 @@ +{ a = 100; b = 200; } diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-intersectattrs.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-intersectattrs.nix new file mode 100644 index 0000000000..3534132ed4 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-intersectattrs.nix @@ -0,0 +1,3 @@ +builtins.intersectAttrs + { a = 1; b = 2; c = 3; } + { a = 100; b = 200; d = 5; } -- cgit 1.4.1