From b9668b119e462da9e3ec708421dc8f036271feeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20B=C4=99za?= Date: Fri, 14 Jul 2023 17:55:57 +0200 Subject: [PATCH] feat: add std.objectRemoveKey Upstream issue: https://github.com/google/go-jsonnet/pull/686 --- crates/jrsonnet-stdlib/src/lib.rs | 1 + crates/jrsonnet-stdlib/src/objects.rs | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/jrsonnet-stdlib/src/lib.rs b/crates/jrsonnet-stdlib/src/lib.rs index 686387d5..75ade27f 100644 --- a/crates/jrsonnet-stdlib/src/lib.rs +++ b/crates/jrsonnet-stdlib/src/lib.rs @@ -139,6 +139,7 @@ pub fn stdlib_uncached(settings: Rc>) -> ObjValue { // 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), diff --git a/crates/jrsonnet-stdlib/src/objects.rs b/crates/jrsonnet-stdlib/src/objects.rs index 25f31fe4..41834cf3 100644 --- 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_fields_ex( 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() +}