git.delta.rocks / jrsonnet / refs/commits / f009c169a67c

difftreelog

perf move std.assertEqual, std.find to native

Yaroslav Bolyukin2024-06-18parent: #541910c.patch.diff
in: master

4 files changed

modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -276,6 +276,18 @@
 }
 
 #[builtin]
+pub fn builtin_find(value: Val, arr: ArrValue) -> Result<Vec<usize>> {
+	let mut out = Vec::new();
+	for (i, ele) in arr.iter().enumerate() {
+		let ele = ele?;
+		if equals(&ele, &value)? {
+			out.push(i);
+		}
+	}
+	Ok(out)
+}
+
+#[builtin]
 pub fn builtin_contains(arr: IndexableVal, elem: Val) -> Result<bool> {
 	builtin_member(arr, elem)
 }
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -91,6 +91,7 @@
 		("any", builtin_any::INST),
 		("all", builtin_all::INST),
 		("member", builtin_member::INST),
+		("find", builtin_find::INST),
 		("contains", builtin_contains::INST),
 		("count", builtin_count::INST),
 		("avg", builtin_avg::INST),
@@ -213,6 +214,7 @@
 		("get", builtin_get::INST),
 		("startsWith", builtin_starts_with::INST),
 		("endsWith", builtin_ends_with::INST),
+		("assertEqual", builtin_assert_equal::INST),
 		// Sets
 		("setMember", builtin_set_member::INST),
 		("setInter", builtin_set_inter::INST),
modifiedcrates/jrsonnet-stdlib/src/misc.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/misc.rs
+++ b/crates/jrsonnet-stdlib/src/misc.rs
@@ -7,7 +7,7 @@
 	manifest::JsonFormat,
 	typed::{Either2, Either4},
 	val::{equals, ArrValue},
-	Context, Either, IStr, ObjValue, Thunk, Val,
+	Context, Either, IStr, ObjValue, ResultExt, Thunk, Val,
 };
 
 use crate::{extvar_source, Settings};
@@ -141,3 +141,14 @@
 		_ => bail!("both arguments should be of the same type"),
 	})
 }
+
+#[builtin]
+pub fn builtin_assert_equal(a: Val, b: Val) -> Result<bool> {
+	if equals(&a, &b)? {
+		return Ok(true);
+	}
+	let format = JsonFormat::std_to_json("  ".to_owned(), "\n", ": ");
+	let a = a.manifest(&format).description("<a> manifestification")?;
+	let b = b.manifest(&format).description("<b> manifestification")?;
+	bail!("assertion failed: A != B\nA: {a}\nB: {b}")
+}
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) },1314  assertEqual(a, b)::15    if a == b then16      true17    else18      error 'Assertion failed. ' + a + ' != ' + b,1920  mergePatch(target, patch)::21    if std.isObject(patch) then22      local target_object =23        if std.isObject(target) then target else {};2425      local target_fields =26        if std.isObject(target_object) then std.objectFields(target_object) else [];2728      local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];29      local both_fields = std.setUnion(target_fields, std.objectFields(patch));3031      {32        [k]:33          if !std.objectHas(patch, k) then34            target_object[k]35          else if !std.objectHas(target_object, k) then36            std.mergePatch(null, patch[k]) tailstrict37          else38            std.mergePatch(target_object[k], patch[k]) tailstrict39        for k in std.setDiff(both_fields, null_fields)40      }41    else42      patch,4344  resolvePath(f, r)::45    local arr = std.split(f, '/');46    std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),4748  find(value, arr)::49    if !std.isArray(arr) then50      error 'find second parameter should be an array, got ' + std.type(arr)51    else52      std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),53}