git.delta.rocks / jrsonnet / refs/commits / 71fb4e2830f5

difftreelog

perf move mergePatch to native

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

4 files changed

modifiedcrates/jrsonnet-evaluator/src/stdlib/format.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/stdlib/format.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/format.rs
@@ -609,7 +609,7 @@
 				tmp_out.push(
 					std::char::from_u32(n as u32)
 						.ok_or_else(|| InvalidUnicodeCodepointGot(n as u32))?,
-				)
+				);
 			}
 			Val::Str(s) => {
 				let s = s.into_flat();
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -215,6 +215,7 @@
 		("startsWith", builtin_starts_with::INST),
 		("endsWith", builtin_ends_with::INST),
 		("assertEqual", builtin_assert_equal::INST),
+		("mergePatch", builtin_merge_patch::INST),
 		// Sets
 		("setMember", builtin_set_member::INST),
 		("setInter", builtin_set_inter::INST),
modifiedcrates/jrsonnet-stdlib/src/misc.rsdiffbeforeafterboth
before · crates/jrsonnet-stdlib/src/misc.rs
1use std::{cell::RefCell, rc::Rc};23use jrsonnet_evaluator::{4	bail,5	error::{ErrorKind::*, Result},6	function::{builtin, ArgLike, CallLocation, FuncVal},7	manifest::JsonFormat,8	typed::{Either2, Either4},9	val::{equals, ArrValue},10	Context, Either, IStr, ObjValue, ResultExt, Thunk, Val,11};1213use crate::{extvar_source, Settings};1415#[builtin]16pub fn builtin_length(x: Either![IStr, ArrValue, ObjValue, FuncVal]) -> usize {17	use Either4::*;18	match x {19		A(x) => x.chars().count(),20		B(x) => x.len(),21		C(x) => x.len(),22		D(f) => f.params_len(),23	}24}2526#[builtin]27pub fn builtin_get(28	o: ObjValue,29	f: IStr,30	default: Option<Thunk<Val>>,31	#[default(true)] inc_hidden: bool,32) -> Result<Val> {33	let do_default = move || {34		let Some(default) = default else {35			return Ok(Val::Null);36		};37		default.evaluate()38	};39	// Happy path for invisible fields40	if !inc_hidden && !o.has_field_ex(f.clone(), false) {41		return do_default();42	}43	let Some(v) = o.get(f)? else {44		return do_default();45	};46	Ok(v)47}4849#[builtin(fields(50	settings: Rc<RefCell<Settings>>,51))]52pub fn builtin_ext_var(this: &builtin_ext_var, ctx: Context, x: IStr) -> Result<Val> {53	let ctx = ctx.state().create_default_context(extvar_source(&x, ""));54	this.settings55		.borrow()56		.ext_vars57		.get(&x)58		.cloned()59		.ok_or_else(|| UndefinedExternalVariable(x))?60		.evaluate_arg(ctx, true)?61		.evaluate()62}6364#[builtin(fields(65	settings: Rc<RefCell<Settings>>,66))]67pub fn builtin_native(this: &builtin_native, x: IStr) -> Val {68	this.settings69		.borrow()70		.ext_natives71		.get(&x)72		.cloned()73		.map_or(Val::Null, Val::Func)74}7576#[builtin(fields(77	settings: Rc<RefCell<Settings>>,78))]79pub fn builtin_trace(80	this: &builtin_trace,81	loc: CallLocation,82	str: Val,83	rest: Option<Thunk<Val>>,84) -> Result<Val> {85	this.settings.borrow().trace_printer.print_trace(86		loc,87		match &str {88			Val::Str(s) => s.clone().into_flat(),89			Val::Func(f) => format!("{f:?}").into(),90			v => v.manifest(JsonFormat::debug())?.into(),91		},92	);93	rest.map_or_else(|| Ok(str), |rest| rest.evaluate())94}9596#[allow(clippy::comparison_chain)]97#[builtin]98pub fn builtin_starts_with(a: Either![IStr, ArrValue], b: Either![IStr, ArrValue]) -> Result<bool> {99	Ok(match (a, b) {100		(Either2::A(a), Either2::A(b)) => a.starts_with(b.as_str()),101		(Either2::B(a), Either2::B(b)) => {102			if b.len() > a.len() {103				return Ok(false);104			} else if b.len() == a.len() {105				return equals(&Val::Arr(a), &Val::Arr(b));106			}107			for (a, b) in a.iter().take(b.len()).zip(b.iter()) {108				let a = a?;109				let b = b?;110				if !equals(&a, &b)? {111					return Ok(false);112				}113			}114			true115		}116		_ => bail!("both arguments should be of the same type"),117	})118}119120#[allow(clippy::comparison_chain)]121#[builtin]122pub fn builtin_ends_with(a: Either![IStr, ArrValue], b: Either![IStr, ArrValue]) -> Result<bool> {123	Ok(match (a, b) {124		(Either2::A(a), Either2::A(b)) => a.ends_with(b.as_str()),125		(Either2::B(a), Either2::B(b)) => {126			if b.len() > a.len() {127				return Ok(false);128			} else if b.len() == a.len() {129				return equals(&Val::Arr(a), &Val::Arr(b));130			}131			let a_len = a.len();132			for (a, b) in a.iter().skip(a_len - b.len()).zip(b.iter()) {133				let a = a?;134				let b = b?;135				if !equals(&a, &b)? {136					return Ok(false);137				}138			}139			true140		}141		_ => bail!("both arguments should be of the same type"),142	})143}144145#[builtin]146pub fn builtin_assert_equal(a: Val, b: Val) -> Result<bool> {147	if equals(&a, &b)? {148		return Ok(true);149	}150	let format = JsonFormat::std_to_json("  ".to_owned(), "\n", ": ");151	let a = a.manifest(&format).description("<a> manifestification")?;152	let b = b.manifest(&format).description("<b> manifestification")?;153	bail!("assertion failed: A != B\nA: {a}\nB: {b}")154}
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -11,30 +11,6 @@
     else
       { [k]: func(k, obj[k]) for k in std.objectFields(obj) },
 
-  mergePatch(target, patch)::
-    if std.isObject(patch) then
-      local target_object =
-        if std.isObject(target) then target else {};
-
-      local target_fields =
-        if std.isObject(target_object) then std.objectFields(target_object) else [];
-
-      local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];
-      local both_fields = std.setUnion(target_fields, std.objectFields(patch));
-
-      {
-        [k]:
-          if !std.objectHas(patch, k) then
-            target_object[k]
-          else if !std.objectHas(target_object, k) then
-            std.mergePatch(null, patch[k]) tailstrict
-          else
-            std.mergePatch(target_object[k], patch[k]) tailstrict
-        for k in std.setDiff(both_fields, null_fields)
-      }
-    else
-      patch,
-
   resolvePath(f, r)::
     local arr = std.split(f, '/');
     std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),