git.delta.rocks / jrsonnet / refs/commits / 18dc4db46973

difftreelog

fix experimental features build

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

3 files changed

modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -186,8 +186,12 @@
 	};
 
 	let tla = opts.tla.tla_opts()?;
-	#[allow(unused_mut)]
-	let mut val = apply_tla(s, &tla, val)?;
+	#[allow(
+		// It is not redundant/unused in exp-apply
+		unused_mut,
+		clippy::redundant_clone,
+	)]
+	let mut val = apply_tla(s.clone(), &tla, val)?;
 
 	#[cfg(feature = "exp-apply")]
 	for apply in opts.input.exp_apply {
modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -70,7 +70,13 @@
 #[builtin]
 pub fn builtin_map_with_key(func: FuncVal, obj: ObjValue) -> Result<ObjValue> {
 	let mut out = ObjValueBuilder::new();
-	for (k, v) in obj.iter() {
+	for (k, v) in obj.iter(
+		// Makes sense mapped object should be ordered the same way, should not break anything when the output is not ordered (the default).
+		// The thrown error might be different, but jsonnet
+		// does not specify the evaluation order.
+		#[cfg(feature = "exp-preserve-order")]
+		true,
+	) {
 		let v = v?;
 		out.field(k.clone())
 			.value(func.evaluate_simple(&(k, v), false)?);
modifiedcrates/jrsonnet-stdlib/src/misc.rsdiffbeforeafterboth
before · crates/jrsonnet-stdlib/src/misc.rs
1use std::{cell::RefCell, collections::BTreeSet, 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, ObjValueBuilder, 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}155156#[builtin]157pub fn builtin_merge_patch(target: Val, patch: Val) -> Result<Val> {158	let Some(patch) = patch.as_obj() else {159		return Ok(patch);160	};161	let Some(target) = target.as_obj() else {162		return Ok(Val::Obj(patch));163	};164	let target_fields = target.fields().into_iter().collect::<BTreeSet<IStr>>();165	let patch_fields = patch.fields().into_iter().collect::<BTreeSet<IStr>>();166167	let mut out = ObjValueBuilder::new();168	for field in target_fields.union(&patch_fields) {169		let Some(field_patch) = patch.get(field.clone())? else {170			out.field(field.clone()).value(target.get(field.clone())?.expect("we're iterating over fields union, if field is missing in patch - it exists in target"));171			continue;172		};173		if matches!(field_patch, Val::Null) {174			continue;175		}176		let Some(field_target) = target.get(field.clone())? else {177			out.field(field.clone()).value(field_patch);178			continue;179		};180		out.field(field.clone())181			.value(builtin_merge_patch(field_target, field_patch)?);182	}183	Ok(out.build().into())184}
after · crates/jrsonnet-stdlib/src/misc.rs
1use std::{cell::RefCell, collections::BTreeSet, 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, ObjValueBuilder, 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	// TODO: Use debug output format151	let format = JsonFormat::std_to_json(152		"  ".to_owned(),153		"\n",154		": ",155		#[cfg(feature = "exp-preserve-order")]156		true,157	);158	let a = a.manifest(&format).description("<a> manifestification")?;159	let b = b.manifest(&format).description("<b> manifestification")?;160	bail!("assertion failed: A != B\nA: {a}\nB: {b}")161}162163#[builtin]164pub fn builtin_merge_patch(target: Val, patch: Val) -> Result<Val> {165	let Some(patch) = patch.as_obj() else {166		return Ok(patch);167	};168	let Some(target) = target.as_obj() else {169		return Ok(Val::Obj(patch));170	};171	let target_fields = target172		.fields(173			// FIXME: Makes no sense to preserve order for BTreeSet, it would be better to use IndexSet here?174			// But IndexSet won't allow fast ordered union...175			// // Makes sense to preserve source ordering where possible.176			// // May affect evaluation order, but it is not specified by jsonnet spec.177			// #[cfg(feature = "exp-preserve-order")]178			// true,179			#[cfg(feature = "exp-preserve-order")]180			false,181		)182		.into_iter()183		.collect::<BTreeSet<IStr>>();184	let patch_fields = patch185		.fields(186			// No need to look at the patch field order, I think?187			// New fields (that will be appended at the end) will be alphabeticaly-ordered,188			// but it is fine for jsonpatch, I don't think people write jsonpatch in jsonnet,189			// when they can use mixins.190			#[cfg(feature = "exp-preserve-order")]191			false,192		)193		.into_iter()194		.collect::<BTreeSet<IStr>>();195196	let mut out = ObjValueBuilder::new();197	for field in target_fields.union(&patch_fields) {198		let Some(field_patch) = patch.get(field.clone())? else {199			out.field(field.clone()).value(target.get(field.clone())?.expect("we're iterating over fields union, if field is missing in patch - it exists in target"));200			continue;201		};202		if matches!(field_patch, Val::Null) {203			continue;204		}205		let Some(field_target) = target.get(field.clone())? else {206			out.field(field.clone()).value(field_patch);207			continue;208		};209		out.field(field.clone())210			.value(builtin_merge_patch(field_target, field_patch)?);211	}212	Ok(out.build().into())213}