git.delta.rocks / jrsonnet / refs/commits / 4ab075dbe65c

difftreelog

fix std function argument names

Yaroslav Bolyukin2023-08-10parent: #cfa49ab.patch.diff
in: master

6 files changed

modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -255,9 +255,9 @@
 }
 
 #[builtin]
-pub fn builtin_remove_at(arr: ArrValue, index: usize) -> Result<ArrValue> {
-	let newArrLeft = arr.clone().slice(None, Some(index), None);
-	let newArrRight = arr.slice(Some(index + 1), None, None);
+pub fn builtin_remove_at(arr: ArrValue, at: usize) -> Result<ArrValue> {
+	let newArrLeft = arr.clone().slice(None, Some(at), None);
+	let newArrRight = arr.slice(Some(at + 1), None, None);
 
 	Ok(ArrValue::extended(
 		newArrLeft.unwrap_or(ArrValue::empty()),
modifiedcrates/jrsonnet-stdlib/src/hash.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/hash.rs
+++ b/crates/jrsonnet-stdlib/src/hash.rs
@@ -6,25 +6,25 @@
 }
 
 #[builtin]
-pub fn builtin_sha256(s: IStr) -> String {
+pub fn builtin_sha256(str: IStr) -> String {
 	use sha2::digest::Digest;
-	format!("{:x}", sha2::Sha256::digest(s.as_bytes()))
+	format!("{:x}", sha2::Sha256::digest(str.as_bytes()))
 }
 
 #[builtin]
-pub fn builtin_sha512(s: IStr) -> String {
+pub fn builtin_sha512(str: IStr) -> String {
 	use sha2::digest::Digest;
-	format!("{:x}", sha2::Sha512::digest(s.as_bytes()))
+	format!("{:x}", sha2::Sha512::digest(str.as_bytes()))
 }
 
 #[builtin]
-pub fn builtin_sha1(s: IStr) -> String {
+pub fn builtin_sha1(str: IStr) -> String {
 	use sha1::digest::Digest;
-	format!("{:x}", sha1::Sha1::digest(s.as_bytes()))
+	format!("{:x}", sha1::Sha1::digest(str.as_bytes()))
 }
 
 #[builtin]
-pub fn builtin_sha3(s: IStr) -> String {
+pub fn builtin_sha3(str: IStr) -> String {
 	use sha3::digest::Digest;
-	format!("{:x}", sha3::Sha3_512::digest(s.as_bytes()))
+	format!("{:x}", sha3::Sha3_512::digest(str.as_bytes()))
 }
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -80,7 +80,7 @@
 		("any", builtin_any::INST),
 		("all", builtin_all::INST),
 		("member", builtin_member::INST),
-		("contains", builtin_member::INST),
+		("contains", builtin_contains::INST),
 		("count", builtin_count::INST),
 		("avg", builtin_avg::INST),
 		("removeAt", builtin_remove_at::INST),
modifiedcrates/jrsonnet-stdlib/src/misc.rsdiffbeforeafterboth
before · crates/jrsonnet-stdlib/src/misc.rs
1use std::{cell::RefCell, rc::Rc};23use jrsonnet_evaluator::{4	error::{ErrorKind::*, Result},5	function::{builtin, ArgLike, CallLocation, FuncVal},6	manifest::JsonFormat,7	throw,8	typed::{Either2, Either4},9	val::{equals, ArrValue},10	Context, Either, IStr, ObjValue, 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(fields(27	settings: Rc<RefCell<Settings>>,28))]29pub fn builtin_ext_var(this: &builtin_ext_var, ctx: Context, x: IStr) -> Result<Val> {30	let ctx = ctx.state().create_default_context(extvar_source(&x, ""));31	this.settings32		.borrow()33		.ext_vars34		.get(&x)35		.cloned()36		.ok_or_else(|| UndefinedExternalVariable(x))?37		.evaluate_arg(ctx, true)?38		.evaluate()39}4041#[builtin(fields(42	settings: Rc<RefCell<Settings>>,43))]44pub fn builtin_native(this: &builtin_native, x: IStr) -> Val {45	this.settings46		.borrow()47		.ext_natives48		.get(&x)49		.cloned()50		.map_or(Val::Null, |v| Val::Func(FuncVal::Builtin(v)))51}5253#[builtin(fields(54	settings: Rc<RefCell<Settings>>,55))]56pub fn builtin_trace(57	this: &builtin_trace,58	loc: CallLocation,59	str: Val,60	rest: Thunk<Val>,61) -> Result<Val> {62	this.settings.borrow().trace_printer.print_trace(63		loc,64		match str {65			Val::Str(s) => s.into_flat(),66			Val::Func(f) => format!("{f:?}").into(),67			v => v68				.manifest(JsonFormat::std_to_json(69					String::from("  "),70					"\n",71					": ",72					#[cfg(feature = "exp-preserve-order")]73					true,74				))?75				.into(),76		},77	);78	rest.evaluate()79}8081#[allow(clippy::comparison_chain)]82#[builtin]83pub fn builtin_starts_with(a: Either![IStr, ArrValue], b: Either![IStr, ArrValue]) -> Result<bool> {84	Ok(match (a, b) {85		(Either2::A(a), Either2::A(b)) => a.starts_with(b.as_str()),86		(Either2::B(a), Either2::B(b)) => {87			if b.len() > a.len() {88				return Ok(false);89			} else if b.len() == a.len() {90				return equals(&Val::Arr(a), &Val::Arr(b));91			} else {92				for (a, b) in a.iter().take(b.len()).zip(b.iter()) {93					let a = a?;94					let b = b?;95					if !equals(&a, &b)? {96						return Ok(false);97					}98				}99				true100			}101		}102		_ => throw!("both arguments should be of the same type"),103	})104}105106#[allow(clippy::comparison_chain)]107#[builtin]108pub fn builtin_ends_with(a: Either![IStr, ArrValue], b: Either![IStr, ArrValue]) -> Result<bool> {109	Ok(match (a, b) {110		(Either2::A(a), Either2::A(b)) => a.ends_with(b.as_str()),111		(Either2::B(a), Either2::B(b)) => {112			if b.len() > a.len() {113				return Ok(false);114			} else if b.len() == a.len() {115				return equals(&Val::Arr(a), &Val::Arr(b));116			} else {117				let a_len = a.len();118				for (a, b) in a.iter().skip(a_len - b.len()).zip(b.iter()) {119					let a = a?;120					let b = b?;121					if !equals(&a, &b)? {122						return Ok(false);123					}124				}125				true126			}127		}128		_ => throw!("both arguments should be of the same type"),129	})130}
modifiedcrates/jrsonnet-stdlib/src/strings.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/strings.rs
+++ b/crates/jrsonnet-stdlib/src/strings.rs
@@ -33,8 +33,8 @@
 }
 
 #[builtin]
-pub fn builtin_equals_ignore_case(x: String, y: String) -> bool {
-	x.to_ascii_lowercase() == y.to_ascii_lowercase()
+pub fn builtin_equals_ignore_case(str1: String, str2: String) -> bool {
+	str1.to_ascii_lowercase() == str2.to_ascii_lowercase()
 }
 
 #[builtin]
modifiedtests/suite/std_param_names.jsonnetdiffbeforeafterboth
--- a/tests/suite/std_param_names.jsonnet
+++ b/tests/suite/std_param_names.jsonnet
@@ -126,8 +126,33 @@
     decodeUTF8: ['arr'],
 
     sum: ['arr'],
+    avg: ['arr', 'onEmpty'],
+    minArray: ['arr', 'keyF', 'onEmpty'],
+    maxArray: ['arr', 'keyF', 'onEmpty'],
+    remove: ['arr', 'elem'],
+    contains: ['arr', 'elem'],
+    removeAt: ['arr', 'at'],
+
+    equalsIgnoreCase: ['str1', 'str2'],
+    isEmpty: ['str'],
+
     xor: ['x', 'y'],
+    xnor: ['x', 'y'],
+    isInteger: ['x'],
+    isDecimal: ['x'],
+    isEven: ['x'],
+    isOdd: ['x'],
+    round: ['x'],
 
+    sha1: ['str'],
+    sha256: ['str'],
+    sha512: ['str'],
+    sha3: ['str'],
+
+    objectKeysValues: ['o'],
+    objectKeysValuesAll: ['o'],
+    objectRemoveKey: ['obj', 'key'],
+
     // C++ jsonnet undocumented
     __compare: ['v1', 'v2'],
     __compare_array: ['arr1', 'arr2'],
@@ -138,3 +163,9 @@
 };
 
 std.all(std.map(function(key) assertNames(key, names[key]), std.objectFields(names)))
+&& std.all([
+    assert std.objectHasAll(names, key): ('function "%s" is not defined in names'
+        % key); true,
+    for key in std.objectFieldsAll(std)
+    if key != 'thisFile'
+])