From b9668b119e462da9e3ec708421dc8f036271feeb Mon Sep 17 00:00:00 2001 From: Paweł Bęza Date: Fri, 14 Jul 2023 15:55:57 +0000 Subject: [PATCH] feat: add std.objectRemoveKey Upstream issue: https://github.com/google/go-jsonnet/pull/686 --- --- a/crates/jrsonnet-stdlib/src/lib.rs +++ b/crates/jrsonnet-stdlib/src/lib.rs @@ -139,6 +139,7 @@ // Objects ("objectFieldsEx", builtin_object_fields_ex::INST), ("objectHasEx", builtin_object_has_ex::INST), + ("objectRemoveKey", builtin_object_remove_key::INST), // Manifest ("escapeStringJson", builtin_escape_string_json::INST), ("manifestJsonEx", builtin_manifest_json_ex::INST), --- a/crates/jrsonnet-stdlib/src/objects.rs +++ b/crates/jrsonnet-stdlib/src/objects.rs @@ -1,9 +1,10 @@ use jrsonnet_evaluator::{ function::builtin, val::{StrValue, Val}, - IStr, ObjValue, + IStr, ObjValue, ObjValueBuilder, }; + #[builtin] pub fn builtin_object_fields_ex( obj: ObjValue, @@ -27,3 +28,16 @@ pub fn builtin_object_has_ex(obj: ObjValue, fname: IStr, hidden: bool) -> bool { obj.has_field_ex(fname, hidden) } + +#[builtin] +pub fn builtin_object_remove_key(obj: ObjValue, key: IStr) -> ObjValue { + let mut new_obj = ObjValueBuilder::with_capacity(obj.len() - 1); + for (k, v) in obj.iter() { + if k == key { + continue + } + new_obj.member(k).value_unchecked(v.unwrap()) + } + + new_obj.build() +} -- gitstuff