git.delta.rocks / jrsonnet / refs/commits / 0ae36baea489

difftreelog

perf move mapWithKey to native

Yaroslav Bolyukin2024-05-19parent: #57f709a.patch.diff
in: master

3 files changed

modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -6,7 +6,7 @@
 	runtime_error,
 	typed::{BoundedI32, BoundedUsize, Either2, NativeFn, Typed},
 	val::{equals, ArrValue, IndexableVal},
-	Either, IStr, ObjValueBuilder, Result, ResultExt, Thunk, Val,
+	Either, IStr, ObjValue, ObjValueBuilder, Result, ResultExt, Thunk, Val,
 };
 
 pub fn eval_on_empty(on_empty: Option<Thunk<Val>>) -> Result<Val> {
@@ -68,6 +68,16 @@
 }
 
 #[builtin]
+pub fn builtin_map_with_key(func: FuncVal, obj: ObjValue) -> Result<ObjValue> {
+	let mut out = ObjValueBuilder::new();
+	for (k, v) in obj.iter() {
+		let v = v?;
+		out.field(k).value(func.evaluate_simple(&(v,), false)?);
+	}
+	Ok(out.build())
+}
+
+#[builtin]
 pub fn builtin_flatmap(
 	func: NativeFn<((Either![String, Val],), Val)>,
 	arr: IndexableVal,
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -79,6 +79,7 @@
 		("slice", builtin_slice::INST),
 		("map", builtin_map::INST),
 		("mapWithIndex", builtin_map_with_index::INST),
+		("mapWithKey", builtin_map_with_key::INST),
 		("flatMap", builtin_flatmap::INST),
 		("filter", builtin_filter::INST),
 		("foldl", builtin_foldl::INST),
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
before · crates/jrsonnet-stdlib/src/std.jsonnet
1{2  local std = self,34  thisFile:: error 'std.thisFile is deprecated, to enable its support in jrsonnet - recompile it with "legacy-this-file" support.\nThis will slow down stdlib caching a bit, though',56  mapWithKey(func, obj)::7    if !std.isFunction(func) then8      error ('std.mapWithKey first param must be function, got ' + std.type(func))9    else if !std.isObject(obj) then10      error ('std.mapWithKey second param must be object, got ' + std.type(obj))11    else12      { [k]: func(k, obj[k]) for k in std.objectFields(obj) },13}