--- 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>) -> Result { @@ -68,6 +68,16 @@ } #[builtin] +pub fn builtin_map_with_key(func: FuncVal, obj: ObjValue) -> Result { + 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, --- 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), --- a/crates/jrsonnet-stdlib/src/std.jsonnet +++ b/crates/jrsonnet-stdlib/src/std.jsonnet @@ -2,12 +2,4 @@ local std = self, 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', - - mapWithKey(func, obj):: - if !std.isFunction(func) then - error ('std.mapWithKey first param must be function, got ' + std.type(func)) - else if !std.isObject(obj) then - error ('std.mapWithKey second param must be object, got ' + std.type(obj)) - else - { [k]: func(k, obj[k]) for k in std.objectFields(obj) }, }