difftreelog
perf move std.assertEqual, std.find to native
in: master
4 files changed
crates/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)
}
crates/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),
crates/jrsonnet-stdlib/src/misc.rsdiffbeforeafterboth1use 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, 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}crates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -11,12 +11,6 @@
else
{ [k]: func(k, obj[k]) for k in std.objectFields(obj) },
- assertEqual(a, b)::
- if a == b then
- true
- else
- error 'Assertion failed. ' + a + ' != ' + b,
-
mergePatch(target, patch)::
if std.isObject(patch) then
local target_object =
@@ -44,10 +38,4 @@
resolvePath(f, r)::
local arr = std.split(f, '/');
std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),
-
- find(value, arr)::
- if !std.isArray(arr) then
- error 'find second parameter should be an array, got ' + std.type(arr)
- else
- std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),
}