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

difftreelog

perf move deepJoin to native

Yaroslav Bolyukin2024-06-18parent: #0e22242.patch.diff
in: master

3 files changed

modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -204,10 +204,31 @@
 pub fn builtin_lines(arr: ArrValue) -> Result<IndexableVal> {
 	builtin_join(
 		IndexableVal::Str("\n".into()),
-		ArrValue::extended(arr, ArrValue::eager(vec![Val::string("")])).into(),
+		ArrValue::extended(arr, ArrValue::eager(vec![Val::string("")])),
 	)
 }
 
+pub fn deep_join_inner(out: &mut String, arr: IndexableVal) -> Result<()> {
+	use std::fmt::Write;
+	match arr {
+		IndexableVal::Str(s) => write!(out, "{s}").expect("no error"),
+		IndexableVal::Arr(arr) => {
+			for ele in arr.iter() {
+				let indexable = IndexableVal::from_untyped(ele?)?;
+				deep_join_inner(out, indexable)?;
+			}
+		}
+	}
+	Ok(())
+}
+
+#[builtin]
+pub fn builtin_deep_join(arr: IndexableVal) -> Result<String> {
+	let mut out = String::new();
+	deep_join_inner(&mut out, arr)?;
+	Ok(out)
+}
+
 #[builtin]
 pub fn builtin_reverse(arr: ArrValue) -> ArrValue {
 	arr.reversed()
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -86,6 +86,7 @@
 		("range", builtin_range::INST),
 		("join", builtin_join::INST),
 		("lines", builtin_lines::INST),
+		("deepJoin", builtin_deep_join::INST),
 		("reverse", builtin_reverse::INST),
 		("any", builtin_any::INST),
 		("all", builtin_all::INST),
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  deepJoin(arr)::15    if std.isString(arr) then16      arr17    else if std.isArray(arr) then18      std.join('', [std.deepJoin(x) for x in arr])19    else20      error 'Expected string or array, got %s' % std.type(arr),2122  assertEqual(a, b)::23    if a == b then24      true25    else26      error 'Assertion failed. ' + a + ' != ' + b,2728  mergePatch(target, patch)::29    if std.isObject(patch) then30      local target_object =31        if std.isObject(target) then target else {};3233      local target_fields =34        if std.isObject(target_object) then std.objectFields(target_object) else [];3536      local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];37      local both_fields = std.setUnion(target_fields, std.objectFields(patch));3839      {40        [k]:41          if !std.objectHas(patch, k) then42            target_object[k]43          else if !std.objectHas(target_object, k) then44            std.mergePatch(null, patch[k]) tailstrict45          else46            std.mergePatch(target_object[k], patch[k]) tailstrict47        for k in std.setDiff(both_fields, null_fields)48      }49    else50      patch,5152  resolvePath(f, r)::53    local arr = std.split(f, '/');54    std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),5556  find(value, arr)::57    if !std.isArray(arr) then58      error 'find second parameter should be an array, got ' + std.type(arr)59    else60      std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),61}