Skip to content

Commit

Permalink
feat: add std.objectRemoveKey
Browse files Browse the repository at this point in the history
Upstream issue: google/go-jsonnet#686
  • Loading branch information
pawelbeza committed Jul 15, 2023
1 parent 3e814e5 commit b9668b1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions crates/jrsonnet-stdlib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ pub fn stdlib_uncached(settings: Rc<RefCell<Settings>>) -> 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),
Expand Down
16 changes: 15 additions & 1 deletion crates/jrsonnet-stdlib/src/objects.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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()
}

0 comments on commit b9668b1

Please sign in to comment.