difftreelog
fix accept null as std.slice argument/in slicing syntax
in: master
3 files changed
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -655,11 +655,11 @@
desc: &'static str,
) -> Result<Option<T>> {
if let Some(value) = expr {
- Ok(Some(in_frame(
+ Ok(in_frame(
loc,
|| format!("slice {desc}"),
- || T::from_untyped(evaluate(ctx.clone(), value)?),
- )?))
+ || <Option<T>>::from_untyped(evaluate(ctx.clone(), value)?),
+ )?)
} else {
Ok(None)
}
crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -655,6 +655,26 @@
}
}
+impl<T> Typed for Option<T>
+where
+ T: Typed,
+{
+ const TYPE: &'static ComplexValType =
+ &ComplexValType::UnionRef(&[&ComplexValType::Simple(ValType::Null), T::TYPE]);
+
+ fn into_untyped(typed: Self) -> Result<Val> {
+ typed.map_or_else(|| Ok(Val::Null), |v| T::into_untyped(v))
+ }
+
+ fn from_untyped(untyped: Val) -> Result<Self> {
+ if matches!(untyped, Val::Null) {
+ Ok(None)
+ } else {
+ T::from_untyped(untyped).map(Some)
+ }
+ }
+}
+
pub struct NativeFn<D: NativeDesc>(D::Value);
impl<D: NativeDesc> Deref for NativeFn<D> {
type Target = D::Value;
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, ObjValue, 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<Option<i32>>,52 end: Option<Option<i32>>,53 step: Option<Option<BoundedUsize<1, { i32::MAX as usize }>>>,54) -> Result<Val> {55 indexable56 .slice(index.flatten(), end.flatten(), step.flatten())57 .map(Val::from)58}5960#[builtin]61pub fn builtin_map(func: FuncVal, arr: IndexableVal) -> ArrValue {62 let arr = arr.to_array();63 arr.map(func)64}6566#[builtin]67pub fn builtin_map_with_index(func: FuncVal, arr: IndexableVal) -> ArrValue {68 let arr = arr.to_array();69 arr.map_with_index(func)70}7172#[builtin]73pub fn builtin_map_with_key(func: FuncVal, obj: ObjValue) -> Result<ObjValue> {74 let mut out = ObjValueBuilder::new();75 for (k, v) in obj.iter(76 // Makes sense mapped object should be ordered the same way, should not break anything when the output is not ordered (the default).77 // The thrown error might be different, but jsonnet78 // does not specify the evaluation order.79 #[cfg(feature = "exp-preserve-order")]80 true,81 ) {82 let v = v?;83 out.field(k.clone())84 .value(func.evaluate_simple(&(k, v), false)?);85 }86 Ok(out.build())87}8889#[builtin]90pub fn builtin_flatmap(91 func: NativeFn<((Either![String, Val],), Val)>,92 arr: IndexableVal,93) -> Result<IndexableVal> {94 use std::fmt::Write;95 match arr {96 IndexableVal::Str(str) => {97 let mut out = String::new();98 for c in str.chars() {99 match func(Either2::A(c.to_string()))? {100 Val::Str(o) => write!(out, "{o}").unwrap(),101 Val::Null => continue,102 _ => bail!("in std.join all items should be strings"),103 };104 }105 Ok(IndexableVal::Str(out.into()))106 }107 IndexableVal::Arr(a) => {108 let mut out = Vec::new();109 for el in a.iter() {110 let el = el?;111 match func(Either2::B(el))? {112 Val::Arr(o) => {113 for oe in o.iter() {114 out.push(oe?);115 }116 }117 Val::Null => continue,118 _ => bail!("in std.join all items should be arrays"),119 };120 }121 Ok(IndexableVal::Arr(out.into()))122 }123 }124}125126#[builtin]127pub fn builtin_filter(func: FuncVal, arr: ArrValue) -> Result<ArrValue> {128 arr.filter(|val| bool::from_untyped(func.evaluate_simple(&(val.clone(),), false)?))129}130131#[builtin]132pub fn builtin_filter_map(133 filter_func: FuncVal,134 map_func: FuncVal,135 arr: ArrValue,136) -> Result<ArrValue> {137 Ok(builtin_filter(filter_func, arr)?.map(map_func))138}139140#[builtin]141pub fn builtin_foldl(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {142 let mut acc = init;143 for i in arr.iter() {144 acc = func.evaluate_simple(&(acc, i?), false)?;145 }146 Ok(acc)147}148149#[builtin]150pub fn builtin_foldr(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {151 let mut acc = init;152 for i in arr.iter().rev() {153 acc = func.evaluate_simple(&(i?, acc), false)?;154 }155 Ok(acc)156}157158#[builtin]159pub fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {160 if to < from {161 return Ok(ArrValue::empty());162 }163 Ok(ArrValue::range_inclusive(from, to))164}165166#[builtin]167pub fn builtin_join(sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {168 use std::fmt::Write;169 Ok(match sep {170 IndexableVal::Arr(joiner_items) => {171 let mut out = Vec::new();172173 let mut first = true;174 for item in arr.iter() {175 let item = item?.clone();176 if let Val::Arr(items) = item {177 if !first {178 out.reserve(joiner_items.len());179 // TODO: extend180 for item in joiner_items.iter() {181 out.push(item?);182 }183 }184 first = false;185 out.reserve(items.len());186 for item in items.iter() {187 out.push(item?);188 }189 } else if matches!(item, Val::Null) {190 continue;191 } else {192 bail!("in std.join all items should be arrays");193 }194 }195196 IndexableVal::Arr(out.into())197 }198 IndexableVal::Str(sep) => {199 let mut out = String::new();200201 let mut first = true;202 for item in arr.iter() {203 let item = item?.clone();204 if let Val::Str(item) = item {205 if !first {206 out += &sep;207 }208 first = false;209 write!(out, "{item}").unwrap();210 } else if matches!(item, Val::Null) {211 continue;212 } else {213 bail!("in std.join all items should be strings");214 }215 }216217 IndexableVal::Str(out.into())218 }219 })220}221222#[builtin]223pub fn builtin_lines(arr: ArrValue) -> Result<IndexableVal> {224 builtin_join(225 IndexableVal::Str("\n".into()),226 ArrValue::extended(arr, ArrValue::eager(vec![Val::string("")])),227 )228}229230#[builtin]231pub fn builtin_resolve_path(f: String, r: String) -> String {232 let Some(pos) = f.rfind('/') else {233 return r;234 };235 format!("{}{}", &f[..=pos], r)236}237238pub fn deep_join_inner(out: &mut String, arr: IndexableVal) -> Result<()> {239 use std::fmt::Write;240 match arr {241 IndexableVal::Str(s) => write!(out, "{s}").expect("no error"),242 IndexableVal::Arr(arr) => {243 for ele in arr.iter() {244 let indexable = IndexableVal::from_untyped(ele?)?;245 deep_join_inner(out, indexable)?;246 }247 }248 }249 Ok(())250}251252#[builtin]253pub fn builtin_deep_join(arr: IndexableVal) -> Result<String> {254 let mut out = String::new();255 deep_join_inner(&mut out, arr)?;256 Ok(out)257}258259#[builtin]260pub fn builtin_reverse(arr: ArrValue) -> ArrValue {261 arr.reversed()262}263264#[builtin]265pub fn builtin_any(arr: ArrValue) -> Result<bool> {266 for v in arr.iter() {267 let v = bool::from_untyped(v?)?;268 if v {269 return Ok(true);270 }271 }272 Ok(false)273}274275#[builtin]276pub fn builtin_all(arr: ArrValue) -> Result<bool> {277 for v in arr.iter() {278 let v = bool::from_untyped(v?)?;279 if !v {280 return Ok(false);281 }282 }283 Ok(true)284}285286#[builtin]287pub fn builtin_member(arr: IndexableVal, x: Val) -> Result<bool> {288 match arr {289 IndexableVal::Str(str) => {290 let x: IStr = IStr::from_untyped(x)?;291 Ok(!x.is_empty() && str.contains(&*x))292 }293 IndexableVal::Arr(a) => {294 for item in a.iter() {295 let item = item?;296 if equals(&item, &x)? {297 return Ok(true);298 }299 }300 Ok(false)301 }302 }303}304305#[builtin]306pub fn builtin_find(value: Val, arr: ArrValue) -> Result<Vec<usize>> {307 let mut out = Vec::new();308 for (i, ele) in arr.iter().enumerate() {309 let ele = ele?;310 if equals(&ele, &value)? {311 out.push(i);312 }313 }314 Ok(out)315}316317#[builtin]318pub fn builtin_contains(arr: IndexableVal, elem: Val) -> Result<bool> {319 builtin_member(arr, elem)320}321322#[builtin]323pub fn builtin_count(arr: ArrValue, x: Val) -> Result<usize> {324 let mut count = 0;325 for item in arr.iter() {326 if equals(&item?, &x)? {327 count += 1;328 }329 }330 Ok(count)331}332333#[builtin]334pub fn builtin_avg(arr: Vec<f64>, onEmpty: Option<Thunk<Val>>) -> Result<Val> {335 if arr.is_empty() {336 return eval_on_empty(onEmpty);337 }338 Ok(Val::try_num(arr.iter().sum::<f64>() / (arr.len() as f64))?)339}340341#[builtin]342pub fn builtin_remove_at(arr: ArrValue, at: i32) -> Result<ArrValue> {343 let newArrLeft = arr.clone().slice(None, Some(at), None);344 let newArrRight = arr.slice(Some(at + 1), None, None);345346 Ok(ArrValue::extended(newArrLeft, newArrRight))347}348349#[builtin]350pub fn builtin_remove(arr: ArrValue, elem: Val) -> Result<ArrValue> {351 for (index, item) in arr.iter().enumerate() {352 if equals(&item?, &elem)? {353 return builtin_remove_at(arr.clone(), index as i32);354 }355 }356 Ok(arr)357}358359#[builtin]360pub fn builtin_flatten_arrays(arrs: Vec<ArrValue>) -> ArrValue {361 pub fn flatten_inner(values: &[ArrValue]) -> ArrValue {362 if values.len() == 1 {363 return values[0].clone();364 } else if values.len() == 2 {365 return ArrValue::extended(values[0].clone(), values[1].clone());366 }367 let (a, b) = values.split_at(values.len() / 2);368 ArrValue::extended(flatten_inner(a), flatten_inner(b))369 }370 if arrs.is_empty() {371 return ArrValue::empty();372 } else if arrs.len() == 1 {373 return arrs.into_iter().next().expect("single");374 }375 flatten_inner(&arrs)376}377378#[builtin]379pub fn builtin_flatten_deep_array(value: Val) -> Result<Vec<Val>> {380 fn process(value: Val, out: &mut Vec<Val>) -> Result<()> {381 match value {382 Val::Arr(arr) => {383 for ele in arr.iter() {384 process(ele?, out)?;385 }386 }387 _ => out.push(value),388 }389 Ok(())390 }391 let mut out = Vec::new();392 process(value, &mut out)?;393 Ok(out)394}395396#[builtin]397pub fn builtin_prune(398 a: Val,399400 #[default(false)]401 #[cfg(feature = "exp-preserve-order")]402 preserve_order: bool,403) -> Result<Val> {404 fn is_content(val: &Val) -> bool {405 match val {406 Val::Null => false,407 Val::Arr(a) => !a.is_empty(),408 Val::Obj(o) => !o.is_empty(),409 _ => true,410 }411 }412 Ok(match a {413 Val::Arr(a) => {414 let mut out = Vec::new();415 for (i, ele) in a.iter().enumerate() {416 let ele = ele417 .and_then(|v| {418 builtin_prune(419 v,420 #[cfg(feature = "exp-preserve-order")]421 preserve_order,422 )423 })424 .with_description(|| format!("elem <{i}> pruning"))?;425 if is_content(&ele) {426 out.push(ele);427 }428 }429 Val::Arr(ArrValue::eager(out))430 }431 Val::Obj(o) => {432 let mut out = ObjValueBuilder::new();433 for (name, value) in o.iter(434 #[cfg(feature = "exp-preserve-order")]435 preserve_order,436 ) {437 let value = value438 .and_then(|v| {439 builtin_prune(440 v,441 #[cfg(feature = "exp-preserve-order")]442 preserve_order,443 )444 })445 .with_description(|| format!("field <{name}> pruning"))?;446 if !is_content(&value) {447 continue;448 }449 out.field(name).value(value);450 }451 Val::Obj(out.build())452 }453 _ => a,454 })455}