about summary refs log tree commit diff
path: root/tvix
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-08-24T14·41+0300
committertazjin <tazjin@tvl.su>2022-09-02T12·59+0000
commit86b21f9c330a6cb92bf62f26f7d066c75f1cefd6 (patch)
treea94324ad28c835988c611e168f1e1747e738d32b /tvix
parentaeea772b1cc73f8f0ec30ad133b6e45fe1b6b0dc (diff)
feat(tvix/eval): implement builtins.catAttrs r/4596
Change-Id: Idf92ac82438fbfcf7b2f6e058830e4744637d8c6
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6262
Tested-by: BuildkiteCI
Reviewed-by: sterni <sternenseemann@systemli.org>
Diffstat (limited to 'tvix')
-rw-r--r--tvix/eval/src/builtins/mod.rs15
-rw-r--r--tvix/eval/src/value/list.rs4
2 files changed, 18 insertions, 1 deletions
diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs
index ce048e22be0d..11e0dc8d9fa7 100644
--- a/tvix/eval/src/builtins/mod.rs
+++ b/tvix/eval/src/builtins/mod.rs
@@ -10,7 +10,7 @@ use std::{
 
 use crate::{
     errors::ErrorKind,
-    value::{Builtin, NixAttrs, NixString, Value},
+    value::{Builtin, NixAttrs, NixList, NixString, Value},
 };
 
 fn pure_builtins() -> Vec<Builtin> {
@@ -20,6 +20,19 @@ fn pure_builtins() -> Vec<Builtin> {
                 ErrorKind::Abort(args.pop().unwrap().to_string()?.as_str().to_owned()).into(),
             );
         }),
+        Builtin::new("catAttrs", 2, |mut args| {
+            let list = args.pop().unwrap().to_list()?;
+            let key = args.pop().unwrap().to_string()?;
+            let mut output = vec![];
+
+            for set in list.into_iter() {
+                if let Some(value) = set.to_attrs()?.select(key.as_str()) {
+                    output.push(value.clone());
+                }
+            }
+
+            Ok(Value::List(NixList::construct(output.len(), output)))
+        }),
         Builtin::new("isAttrs", 1, |args| {
             Ok(Value::Bool(matches!(args[0], Value::Attrs(_))))
         }),
diff --git a/tvix/eval/src/value/list.rs b/tvix/eval/src/value/list.rs
index 31f223fce599..77776c4dce62 100644
--- a/tvix/eval/src/value/list.rs
+++ b/tvix/eval/src/value/list.rs
@@ -38,4 +38,8 @@ impl NixList {
 
         NixList(stack_slice)
     }
+
+    pub fn into_iter(self) -> std::vec::IntoIter<Value> {
+        self.0.into_iter()
+    }
 }