difftreelog
perf move std.assertEqual, std.find to native
in: master
4 files changed
crates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth1#![allow(non_snake_case)]23use jrsonnet_evaluator::{4 bail,5 function::{builtin, FuncVal},6 runtime_error,7 typed::{BoundedI32, BoundedUsize, Either2, NativeFn, Typed},8 val::{equals, ArrValue, IndexableVal},9 Either, IStr, ObjValueBuilder, Result, ResultExt, Thunk, Val,10};1112pub fn eval_on_empty(on_empty: Option<Thunk<Val>>) -> Result<Val> {13 if let Some(on_empty) = on_empty {14 on_empty.evaluate()15 } else {16 bail!("expected non-empty array")17 }18}1920#[builtin]21pub fn builtin_make_array(sz: BoundedI32<0, { i32::MAX }>, func: FuncVal) -> Result<ArrValue> {22 if *sz == 0 {23 return Ok(ArrValue::empty());24 }25 func.evaluate_trivial().map_or_else(26 || Ok(ArrValue::range_exclusive(0, *sz).map(func)),27 |trivial| {28 let mut out = Vec::with_capacity(*sz as usize);29 for _ in 0..*sz {30 out.push(trivial.clone());31 }32 Ok(ArrValue::eager(out))33 },34 )35}3637#[builtin]38pub fn builtin_repeat(what: Either![IStr, ArrValue], count: usize) -> Result<Val> {39 Ok(match what {40 Either2::A(s) => Val::string(s.repeat(count)),41 Either2::B(arr) => Val::Arr(42 ArrValue::repeated(arr, count)43 .ok_or_else(|| runtime_error!("repeated length overflow"))?,44 ),45 })46}4748#[builtin]49pub fn builtin_slice(50 indexable: IndexableVal,51 index: Option<i32>,52 end: Option<i32>,53 step: Option<BoundedUsize<1, { i32::MAX as usize }>>,54) -> Result<Val> {55 indexable.slice(index, end, step).map(Val::from)56}5758#[builtin]59pub fn builtin_map(func: FuncVal, arr: IndexableVal) -> ArrValue {60 let arr = arr.to_array();61 arr.map(func)62}6364#[builtin]65pub fn builtin_map_with_index(func: FuncVal, arr: IndexableVal) -> ArrValue {66 let arr = arr.to_array();67 arr.map_with_index(func)68}6970#[builtin]71pub fn builtin_flatmap(72 func: NativeFn<((Either![String, Val],), Val)>,73 arr: IndexableVal,74) -> Result<IndexableVal> {75 use std::fmt::Write;76 match arr {77 IndexableVal::Str(str) => {78 let mut out = String::new();79 for c in str.chars() {80 match func(Either2::A(c.to_string()))? {81 Val::Str(o) => write!(out, "{o}").unwrap(),82 Val::Null => continue,83 _ => bail!("in std.join all items should be strings"),84 };85 }86 Ok(IndexableVal::Str(out.into()))87 }88 IndexableVal::Arr(a) => {89 let mut out = Vec::new();90 for el in a.iter() {91 let el = el?;92 match func(Either2::B(el))? {93 Val::Arr(o) => {94 for oe in o.iter() {95 out.push(oe?);96 }97 }98 Val::Null => continue,99 _ => bail!("in std.join all items should be arrays"),100 };101 }102 Ok(IndexableVal::Arr(out.into()))103 }104 }105}106107#[builtin]108pub fn builtin_filter(func: FuncVal, arr: ArrValue) -> Result<ArrValue> {109 arr.filter(|val| bool::from_untyped(func.evaluate_simple(&(val.clone(),), false)?))110}111112#[builtin]113pub fn builtin_filter_map(114 filter_func: FuncVal,115 map_func: FuncVal,116 arr: ArrValue,117) -> Result<ArrValue> {118 Ok(builtin_filter(filter_func, arr)?.map(map_func))119}120121#[builtin]122pub fn builtin_foldl(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {123 let mut acc = init;124 for i in arr.iter() {125 acc = func.evaluate_simple(&(acc, i?), false)?;126 }127 Ok(acc)128}129130#[builtin]131pub fn builtin_foldr(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {132 let mut acc = init;133 for i in arr.iter().rev() {134 acc = func.evaluate_simple(&(i?, acc), false)?;135 }136 Ok(acc)137}138139#[builtin]140pub fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {141 if to < from {142 return Ok(ArrValue::empty());143 }144 Ok(ArrValue::range_inclusive(from, to))145}146147#[builtin]148pub fn builtin_join(sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {149 use std::fmt::Write;150 Ok(match sep {151 IndexableVal::Arr(joiner_items) => {152 let mut out = Vec::new();153154 let mut first = true;155 for item in arr.iter() {156 let item = item?.clone();157 if let Val::Arr(items) = item {158 if !first {159 out.reserve(joiner_items.len());160 // TODO: extend161 for item in joiner_items.iter() {162 out.push(item?);163 }164 }165 first = false;166 out.reserve(items.len());167 for item in items.iter() {168 out.push(item?);169 }170 } else if matches!(item, Val::Null) {171 continue;172 } else {173 bail!("in std.join all items should be arrays");174 }175 }176177 IndexableVal::Arr(out.into())178 }179 IndexableVal::Str(sep) => {180 let mut out = String::new();181182 let mut first = true;183 for item in arr.iter() {184 let item = item?.clone();185 if let Val::Str(item) = item {186 if !first {187 out += &sep;188 }189 first = false;190 write!(out, "{item}").unwrap();191 } else if matches!(item, Val::Null) {192 continue;193 } else {194 bail!("in std.join all items should be strings");195 }196 }197198 IndexableVal::Str(out.into())199 }200 })201}202203#[builtin]204pub fn builtin_lines(arr: ArrValue) -> Result<IndexableVal> {205 builtin_join(206 IndexableVal::Str("\n".into()),207 ArrValue::extended(arr, ArrValue::eager(vec![Val::string("")])),208 )209}210211pub fn deep_join_inner(out: &mut String, arr: IndexableVal) -> Result<()> {212 use std::fmt::Write;213 match arr {214 IndexableVal::Str(s) => write!(out, "{s}").expect("no error"),215 IndexableVal::Arr(arr) => {216 for ele in arr.iter() {217 let indexable = IndexableVal::from_untyped(ele?)?;218 deep_join_inner(out, indexable)?;219 }220 }221 }222 Ok(())223}224225#[builtin]226pub fn builtin_deep_join(arr: IndexableVal) -> Result<String> {227 let mut out = String::new();228 deep_join_inner(&mut out, arr)?;229 Ok(out)230}231232#[builtin]233pub fn builtin_reverse(arr: ArrValue) -> ArrValue {234 arr.reversed()235}236237#[builtin]238pub fn builtin_any(arr: ArrValue) -> Result<bool> {239 for v in arr.iter() {240 let v = bool::from_untyped(v?)?;241 if v {242 return Ok(true);243 }244 }245 Ok(false)246}247248#[builtin]249pub fn builtin_all(arr: ArrValue) -> Result<bool> {250 for v in arr.iter() {251 let v = bool::from_untyped(v?)?;252 if !v {253 return Ok(false);254 }255 }256 Ok(true)257}258259#[builtin]260pub fn builtin_member(arr: IndexableVal, x: Val) -> Result<bool> {261 match arr {262 IndexableVal::Str(str) => {263 let x: IStr = IStr::from_untyped(x)?;264 Ok(!x.is_empty() && str.contains(&*x))265 }266 IndexableVal::Arr(a) => {267 for item in a.iter() {268 let item = item?;269 if equals(&item, &x)? {270 return Ok(true);271 }272 }273 Ok(false)274 }275 }276}277278#[builtin]279pub fn builtin_contains(arr: IndexableVal, elem: Val) -> Result<bool> {280 builtin_member(arr, elem)281}282283#[builtin]284pub fn builtin_count(arr: ArrValue, x: Val) -> Result<usize> {285 let mut count = 0;286 for item in arr.iter() {287 if equals(&item?, &x)? {288 count += 1;289 }290 }291 Ok(count)292}293294#[builtin]295pub fn builtin_avg(arr: Vec<f64>, onEmpty: Option<Thunk<Val>>) -> Result<Val> {296 if arr.is_empty() {297 return eval_on_empty(onEmpty);298 }299 Ok(Val::try_num(arr.iter().sum::<f64>() / (arr.len() as f64))?)300}301302#[builtin]303pub fn builtin_remove_at(arr: ArrValue, at: i32) -> Result<ArrValue> {304 let newArrLeft = arr.clone().slice(None, Some(at), None);305 let newArrRight = arr.slice(Some(at + 1), None, None);306307 Ok(ArrValue::extended(newArrLeft, newArrRight))308}309310#[builtin]311pub fn builtin_remove(arr: ArrValue, elem: Val) -> Result<ArrValue> {312 for (index, item) in arr.iter().enumerate() {313 if equals(&item?, &elem)? {314 return builtin_remove_at(arr.clone(), index as i32);315 }316 }317 Ok(arr)318}319320#[builtin]321pub fn builtin_flatten_arrays(arrs: Vec<ArrValue>) -> ArrValue {322 pub fn flatten_inner(values: &[ArrValue]) -> ArrValue {323 if values.len() == 1 {324 return values[0].clone();325 } else if values.len() == 2 {326 return ArrValue::extended(values[0].clone(), values[1].clone());327 }328 let (a, b) = values.split_at(values.len() / 2);329 ArrValue::extended(flatten_inner(a), flatten_inner(b))330 }331 if arrs.is_empty() {332 return ArrValue::empty();333 } else if arrs.len() == 1 {334 return arrs.into_iter().next().expect("single");335 }336 flatten_inner(&arrs)337}338339#[builtin]340pub fn builtin_flatten_deep_array(value: Val) -> Result<Vec<Val>> {341 fn process(value: Val, out: &mut Vec<Val>) -> Result<()> {342 match value {343 Val::Arr(arr) => {344 for ele in arr.iter() {345 process(ele?, out)?;346 }347 }348 _ => out.push(value),349 }350 Ok(())351 }352 let mut out = Vec::new();353 process(value, &mut out)?;354 Ok(out)355}356357#[builtin]358pub fn builtin_prune(359 a: Val,360361 #[default(false)]362 #[cfg(feature = "exp-preserve-order")]363 preserve_order: bool,364) -> Result<Val> {365 fn is_content(val: &Val) -> bool {366 match val {367 Val::Null => false,368 Val::Arr(a) => !a.is_empty(),369 Val::Obj(o) => !o.is_empty(),370 _ => true,371 }372 }373 Ok(match a {374 Val::Arr(a) => {375 let mut out = Vec::new();376 for (i, ele) in a.iter().enumerate() {377 let ele = ele378 .and_then(|v| {379 builtin_prune(380 v,381 #[cfg(feature = "exp-preserve-order")]382 preserve_order,383 )384 })385 .with_description(|| format!("elem <{i}> pruning"))?;386 if is_content(&ele) {387 out.push(ele);388 }389 }390 Val::Arr(ArrValue::eager(out))391 }392 Val::Obj(o) => {393 let mut out = ObjValueBuilder::new();394 for (name, value) in o.iter(395 #[cfg(feature = "exp-preserve-order")]396 preserve_order,397 ) {398 let value = value399 .and_then(|v| {400 builtin_prune(401 v,402 #[cfg(feature = "exp-preserve-order")]403 preserve_order,404 )405 })406 .with_description(|| format!("field <{name}> pruning"))?;407 if !is_content(&value) {408 continue;409 }410 out.field(name).value(value);411 }412 Val::Obj(out.build())413 }414 _ => a,415 })416}1#![allow(non_snake_case)]23use jrsonnet_evaluator::{4 bail,5 function::{builtin, FuncVal},6 runtime_error,7 typed::{BoundedI32, BoundedUsize, Either2, NativeFn, Typed},8 val::{equals, ArrValue, IndexableVal},9 Either, IStr, ObjValueBuilder, Result, ResultExt, Thunk, Val,10};1112pub fn eval_on_empty(on_empty: Option<Thunk<Val>>) -> Result<Val> {13 if let Some(on_empty) = on_empty {14 on_empty.evaluate()15 } else {16 bail!("expected non-empty array")17 }18}1920#[builtin]21pub fn builtin_make_array(sz: BoundedI32<0, { i32::MAX }>, func: FuncVal) -> Result<ArrValue> {22 if *sz == 0 {23 return Ok(ArrValue::empty());24 }25 func.evaluate_trivial().map_or_else(26 || Ok(ArrValue::range_exclusive(0, *sz).map(func)),27 |trivial| {28 let mut out = Vec::with_capacity(*sz as usize);29 for _ in 0..*sz {30 out.push(trivial.clone());31 }32 Ok(ArrValue::eager(out))33 },34 )35}3637#[builtin]38pub fn builtin_repeat(what: Either![IStr, ArrValue], count: usize) -> Result<Val> {39 Ok(match what {40 Either2::A(s) => Val::string(s.repeat(count)),41 Either2::B(arr) => Val::Arr(42 ArrValue::repeated(arr, count)43 .ok_or_else(|| runtime_error!("repeated length overflow"))?,44 ),45 })46}4748#[builtin]49pub fn builtin_slice(50 indexable: IndexableVal,51 index: Option<i32>,52 end: Option<i32>,53 step: Option<BoundedUsize<1, { i32::MAX as usize }>>,54) -> Result<Val> {55 indexable.slice(index, end, step).map(Val::from)56}5758#[builtin]59pub fn builtin_map(func: FuncVal, arr: IndexableVal) -> ArrValue {60 let arr = arr.to_array();61 arr.map(func)62}6364#[builtin]65pub fn builtin_map_with_index(func: FuncVal, arr: IndexableVal) -> ArrValue {66 let arr = arr.to_array();67 arr.map_with_index(func)68}6970#[builtin]71pub fn builtin_flatmap(72 func: NativeFn<((Either![String, Val],), Val)>,73 arr: IndexableVal,74) -> Result<IndexableVal> {75 use std::fmt::Write;76 match arr {77 IndexableVal::Str(str) => {78 let mut out = String::new();79 for c in str.chars() {80 match func(Either2::A(c.to_string()))? {81 Val::Str(o) => write!(out, "{o}").unwrap(),82 Val::Null => continue,83 _ => bail!("in std.join all items should be strings"),84 };85 }86 Ok(IndexableVal::Str(out.into()))87 }88 IndexableVal::Arr(a) => {89 let mut out = Vec::new();90 for el in a.iter() {91 let el = el?;92 match func(Either2::B(el))? {93 Val::Arr(o) => {94 for oe in o.iter() {95 out.push(oe?);96 }97 }98 Val::Null => continue,99 _ => bail!("in std.join all items should be arrays"),100 };101 }102 Ok(IndexableVal::Arr(out.into()))103 }104 }105}106107#[builtin]108pub fn builtin_filter(func: FuncVal, arr: ArrValue) -> Result<ArrValue> {109 arr.filter(|val| bool::from_untyped(func.evaluate_simple(&(val.clone(),), false)?))110}111112#[builtin]113pub fn builtin_filter_map(114 filter_func: FuncVal,115 map_func: FuncVal,116 arr: ArrValue,117) -> Result<ArrValue> {118 Ok(builtin_filter(filter_func, arr)?.map(map_func))119}120121#[builtin]122pub fn builtin_foldl(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {123 let mut acc = init;124 for i in arr.iter() {125 acc = func.evaluate_simple(&(acc, i?), false)?;126 }127 Ok(acc)128}129130#[builtin]131pub fn builtin_foldr(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {132 let mut acc = init;133 for i in arr.iter().rev() {134 acc = func.evaluate_simple(&(i?, acc), false)?;135 }136 Ok(acc)137}138139#[builtin]140pub fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {141 if to < from {142 return Ok(ArrValue::empty());143 }144 Ok(ArrValue::range_inclusive(from, to))145}146147#[builtin]148pub fn builtin_join(sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {149 use std::fmt::Write;150 Ok(match sep {151 IndexableVal::Arr(joiner_items) => {152 let mut out = Vec::new();153154 let mut first = true;155 for item in arr.iter() {156 let item = item?.clone();157 if let Val::Arr(items) = item {158 if !first {159 out.reserve(joiner_items.len());160 // TODO: extend161 for item in joiner_items.iter() {162 out.push(item?);163 }164 }165 first = false;166 out.reserve(items.len());167 for item in items.iter() {168 out.push(item?);169 }170 } else if matches!(item, Val::Null) {171 continue;172 } else {173 bail!("in std.join all items should be arrays");174 }175 }176177 IndexableVal::Arr(out.into())178 }179 IndexableVal::Str(sep) => {180 let mut out = String::new();181182 let mut first = true;183 for item in arr.iter() {184 let item = item?.clone();185 if let Val::Str(item) = item {186 if !first {187 out += &sep;188 }189 first = false;190 write!(out, "{item}").unwrap();191 } else if matches!(item, Val::Null) {192 continue;193 } else {194 bail!("in std.join all items should be strings");195 }196 }197198 IndexableVal::Str(out.into())199 }200 })201}202203#[builtin]204pub fn builtin_lines(arr: ArrValue) -> Result<IndexableVal> {205 builtin_join(206 IndexableVal::Str("\n".into()),207 ArrValue::extended(arr, ArrValue::eager(vec![Val::string("")])),208 )209}210211pub fn deep_join_inner(out: &mut String, arr: IndexableVal) -> Result<()> {212 use std::fmt::Write;213 match arr {214 IndexableVal::Str(s) => write!(out, "{s}").expect("no error"),215 IndexableVal::Arr(arr) => {216 for ele in arr.iter() {217 let indexable = IndexableVal::from_untyped(ele?)?;218 deep_join_inner(out, indexable)?;219 }220 }221 }222 Ok(())223}224225#[builtin]226pub fn builtin_deep_join(arr: IndexableVal) -> Result<String> {227 let mut out = String::new();228 deep_join_inner(&mut out, arr)?;229 Ok(out)230}231232#[builtin]233pub fn builtin_reverse(arr: ArrValue) -> ArrValue {234 arr.reversed()235}236237#[builtin]238pub fn builtin_any(arr: ArrValue) -> Result<bool> {239 for v in arr.iter() {240 let v = bool::from_untyped(v?)?;241 if v {242 return Ok(true);243 }244 }245 Ok(false)246}247248#[builtin]249pub fn builtin_all(arr: ArrValue) -> Result<bool> {250 for v in arr.iter() {251 let v = bool::from_untyped(v?)?;252 if !v {253 return Ok(false);254 }255 }256 Ok(true)257}258259#[builtin]260pub fn builtin_member(arr: IndexableVal, x: Val) -> Result<bool> {261 match arr {262 IndexableVal::Str(str) => {263 let x: IStr = IStr::from_untyped(x)?;264 Ok(!x.is_empty() && str.contains(&*x))265 }266 IndexableVal::Arr(a) => {267 for item in a.iter() {268 let item = item?;269 if equals(&item, &x)? {270 return Ok(true);271 }272 }273 Ok(false)274 }275 }276}277278#[builtin]279pub fn builtin_find(value: Val, arr: ArrValue) -> Result<Vec<usize>> {280 let mut out = Vec::new();281 for (i, ele) in arr.iter().enumerate() {282 let ele = ele?;283 if equals(&ele, &value)? {284 out.push(i);285 }286 }287 Ok(out)288}289290#[builtin]291pub fn builtin_contains(arr: IndexableVal, elem: Val) -> Result<bool> {292 builtin_member(arr, elem)293}294295#[builtin]296pub fn builtin_count(arr: ArrValue, x: Val) -> Result<usize> {297 let mut count = 0;298 for item in arr.iter() {299 if equals(&item?, &x)? {300 count += 1;301 }302 }303 Ok(count)304}305306#[builtin]307pub fn builtin_avg(arr: Vec<f64>, onEmpty: Option<Thunk<Val>>) -> Result<Val> {308 if arr.is_empty() {309 return eval_on_empty(onEmpty);310 }311 Ok(Val::try_num(arr.iter().sum::<f64>() / (arr.len() as f64))?)312}313314#[builtin]315pub fn builtin_remove_at(arr: ArrValue, at: i32) -> Result<ArrValue> {316 let newArrLeft = arr.clone().slice(None, Some(at), None);317 let newArrRight = arr.slice(Some(at + 1), None, None);318319 Ok(ArrValue::extended(newArrLeft, newArrRight))320}321322#[builtin]323pub fn builtin_remove(arr: ArrValue, elem: Val) -> Result<ArrValue> {324 for (index, item) in arr.iter().enumerate() {325 if equals(&item?, &elem)? {326 return builtin_remove_at(arr.clone(), index as i32);327 }328 }329 Ok(arr)330}331332#[builtin]333pub fn builtin_flatten_arrays(arrs: Vec<ArrValue>) -> ArrValue {334 pub fn flatten_inner(values: &[ArrValue]) -> ArrValue {335 if values.len() == 1 {336 return values[0].clone();337 } else if values.len() == 2 {338 return ArrValue::extended(values[0].clone(), values[1].clone());339 }340 let (a, b) = values.split_at(values.len() / 2);341 ArrValue::extended(flatten_inner(a), flatten_inner(b))342 }343 if arrs.is_empty() {344 return ArrValue::empty();345 } else if arrs.len() == 1 {346 return arrs.into_iter().next().expect("single");347 }348 flatten_inner(&arrs)349}350351#[builtin]352pub fn builtin_flatten_deep_array(value: Val) -> Result<Vec<Val>> {353 fn process(value: Val, out: &mut Vec<Val>) -> Result<()> {354 match value {355 Val::Arr(arr) => {356 for ele in arr.iter() {357 process(ele?, out)?;358 }359 }360 _ => out.push(value),361 }362 Ok(())363 }364 let mut out = Vec::new();365 process(value, &mut out)?;366 Ok(out)367}368369#[builtin]370pub fn builtin_prune(371 a: Val,372373 #[default(false)]374 #[cfg(feature = "exp-preserve-order")]375 preserve_order: bool,376) -> Result<Val> {377 fn is_content(val: &Val) -> bool {378 match val {379 Val::Null => false,380 Val::Arr(a) => !a.is_empty(),381 Val::Obj(o) => !o.is_empty(),382 _ => true,383 }384 }385 Ok(match a {386 Val::Arr(a) => {387 let mut out = Vec::new();388 for (i, ele) in a.iter().enumerate() {389 let ele = ele390 .and_then(|v| {391 builtin_prune(392 v,393 #[cfg(feature = "exp-preserve-order")]394 preserve_order,395 )396 })397 .with_description(|| format!("elem <{i}> pruning"))?;398 if is_content(&ele) {399 out.push(ele);400 }401 }402 Val::Arr(ArrValue::eager(out))403 }404 Val::Obj(o) => {405 let mut out = ObjValueBuilder::new();406 for (name, value) in o.iter(407 #[cfg(feature = "exp-preserve-order")]408 preserve_order,409 ) {410 let value = value411 .and_then(|v| {412 builtin_prune(413 v,414 #[cfg(feature = "exp-preserve-order")]415 preserve_order,416 )417 })418 .with_description(|| format!("field <{name}> pruning"))?;419 if !is_content(&value) {420 continue;421 }422 out.field(name).value(value);423 }424 Val::Obj(out.build())425 }426 _ => a,427 })428}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.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/misc.rs
+++ b/crates/jrsonnet-stdlib/src/misc.rs
@@ -7,7 +7,7 @@
manifest::JsonFormat,
typed::{Either2, Either4},
val::{equals, ArrValue},
- Context, Either, IStr, ObjValue, Thunk, Val,
+ Context, Either, IStr, ObjValue, ResultExt, Thunk, Val,
};
use crate::{extvar_source, Settings};
@@ -141,3 +141,14 @@
_ => bail!("both arguments should be of the same type"),
})
}
+
+#[builtin]
+pub fn builtin_assert_equal(a: Val, b: Val) -> Result<bool> {
+ if equals(&a, &b)? {
+ return Ok(true);
+ }
+ let format = JsonFormat::std_to_json(" ".to_owned(), "\n", ": ");
+ let a = a.manifest(&format).description("<a> manifestification")?;
+ let b = b.manifest(&format).description("<b> manifestification")?;
+ bail!("assertion failed: A != B\nA: {a}\nB: {b}")
+}
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)),
}