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<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_map_with_key(func: FuncVal, obj: ObjValue) -> Result<ObjValue> {72 let mut out = ObjValueBuilder::new();73 for (k, v) in obj.iter(74 // Makes sense mapped object should be ordered the same way, should not break anything when the output is not ordered (the default).75 // The thrown error might be different, but jsonnet76 // does not specify the evaluation order.77 #[cfg(feature = "exp-preserve-order")]78 true,79 ) {80 let v = v?;81 out.field(k.clone())82 .value(func.evaluate_simple(&(k, v), false)?);83 }84 Ok(out.build())85}8687#[builtin]88pub fn builtin_flatmap(89 func: NativeFn<((Either![String, Val],), Val)>,90 arr: IndexableVal,91) -> Result<IndexableVal> {92 use std::fmt::Write;93 match arr {94 IndexableVal::Str(str) => {95 let mut out = String::new();96 for c in str.chars() {97 match func(Either2::A(c.to_string()))? {98 Val::Str(o) => write!(out, "{o}").unwrap(),99 Val::Null => continue,100 _ => bail!("in std.join all items should be strings"),101 };102 }103 Ok(IndexableVal::Str(out.into()))104 }105 IndexableVal::Arr(a) => {106 let mut out = Vec::new();107 for el in a.iter() {108 let el = el?;109 match func(Either2::B(el))? {110 Val::Arr(o) => {111 for oe in o.iter() {112 out.push(oe?);113 }114 }115 Val::Null => continue,116 _ => bail!("in std.join all items should be arrays"),117 };118 }119 Ok(IndexableVal::Arr(out.into()))120 }121 }122}123124#[builtin]125pub fn builtin_filter(func: FuncVal, arr: ArrValue) -> Result<ArrValue> {126 arr.filter(|val| bool::from_untyped(func.evaluate_simple(&(val.clone(),), false)?))127}128129#[builtin]130pub fn builtin_filter_map(131 filter_func: FuncVal,132 map_func: FuncVal,133 arr: ArrValue,134) -> Result<ArrValue> {135 Ok(builtin_filter(filter_func, arr)?.map(map_func))136}137138#[builtin]139pub fn builtin_foldl(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {140 let mut acc = init;141 for i in arr.iter() {142 acc = func.evaluate_simple(&(acc, i?), false)?;143 }144 Ok(acc)145}146147#[builtin]148pub fn builtin_foldr(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {149 let mut acc = init;150 for i in arr.iter().rev() {151 acc = func.evaluate_simple(&(i?, acc), false)?;152 }153 Ok(acc)154}155156#[builtin]157pub fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {158 if to < from {159 return Ok(ArrValue::empty());160 }161 Ok(ArrValue::range_inclusive(from, to))162}163164#[builtin]165pub fn builtin_join(sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {166 use std::fmt::Write;167 Ok(match sep {168 IndexableVal::Arr(joiner_items) => {169 let mut out = Vec::new();170171 let mut first = true;172 for item in arr.iter() {173 let item = item?.clone();174 if let Val::Arr(items) = item {175 if !first {176 out.reserve(joiner_items.len());177 // TODO: extend178 for item in joiner_items.iter() {179 out.push(item?);180 }181 }182 first = false;183 out.reserve(items.len());184 for item in items.iter() {185 out.push(item?);186 }187 } else if matches!(item, Val::Null) {188 continue;189 } else {190 bail!("in std.join all items should be arrays");191 }192 }193194 IndexableVal::Arr(out.into())195 }196 IndexableVal::Str(sep) => {197 let mut out = String::new();198199 let mut first = true;200 for item in arr.iter() {201 let item = item?.clone();202 if let Val::Str(item) = item {203 if !first {204 out += &sep;205 }206 first = false;207 write!(out, "{item}").unwrap();208 } else if matches!(item, Val::Null) {209 continue;210 } else {211 bail!("in std.join all items should be strings");212 }213 }214215 IndexableVal::Str(out.into())216 }217 })218}219220#[builtin]221pub fn builtin_lines(arr: ArrValue) -> Result<IndexableVal> {222 builtin_join(223 IndexableVal::Str("\n".into()),224 ArrValue::extended(arr, ArrValue::eager(vec![Val::string("")])),225 )226}227228#[builtin]229pub fn builtin_resolve_path(f: String, r: String) -> String {230 let Some(pos) = f.rfind('/') else {231 return r;232 };233 format!("{}{}", &f[..=pos], r)234}235236pub fn deep_join_inner(out: &mut String, arr: IndexableVal) -> Result<()> {237 use std::fmt::Write;238 match arr {239 IndexableVal::Str(s) => write!(out, "{s}").expect("no error"),240 IndexableVal::Arr(arr) => {241 for ele in arr.iter() {242 let indexable = IndexableVal::from_untyped(ele?)?;243 deep_join_inner(out, indexable)?;244 }245 }246 }247 Ok(())248}249250#[builtin]251pub fn builtin_deep_join(arr: IndexableVal) -> Result<String> {252 let mut out = String::new();253 deep_join_inner(&mut out, arr)?;254 Ok(out)255}256257#[builtin]258pub fn builtin_reverse(arr: ArrValue) -> ArrValue {259 arr.reversed()260}261262#[builtin]263pub fn builtin_any(arr: ArrValue) -> Result<bool> {264 for v in arr.iter() {265 let v = bool::from_untyped(v?)?;266 if v {267 return Ok(true);268 }269 }270 Ok(false)271}272273#[builtin]274pub fn builtin_all(arr: ArrValue) -> Result<bool> {275 for v in arr.iter() {276 let v = bool::from_untyped(v?)?;277 if !v {278 return Ok(false);279 }280 }281 Ok(true)282}283284#[builtin]285pub fn builtin_member(arr: IndexableVal, x: Val) -> Result<bool> {286 match arr {287 IndexableVal::Str(str) => {288 let x: IStr = IStr::from_untyped(x)?;289 Ok(!x.is_empty() && str.contains(&*x))290 }291 IndexableVal::Arr(a) => {292 for item in a.iter() {293 let item = item?;294 if equals(&item, &x)? {295 return Ok(true);296 }297 }298 Ok(false)299 }300 }301}302303#[builtin]304pub fn builtin_find(value: Val, arr: ArrValue) -> Result<Vec<usize>> {305 let mut out = Vec::new();306 for (i, ele) in arr.iter().enumerate() {307 let ele = ele?;308 if equals(&ele, &value)? {309 out.push(i);310 }311 }312 Ok(out)313}314315#[builtin]316pub fn builtin_contains(arr: IndexableVal, elem: Val) -> Result<bool> {317 builtin_member(arr, elem)318}319320#[builtin]321pub fn builtin_count(arr: ArrValue, x: Val) -> Result<usize> {322 let mut count = 0;323 for item in arr.iter() {324 if equals(&item?, &x)? {325 count += 1;326 }327 }328 Ok(count)329}330331#[builtin]332pub fn builtin_avg(arr: Vec<f64>, onEmpty: Option<Thunk<Val>>) -> Result<Val> {333 if arr.is_empty() {334 return eval_on_empty(onEmpty);335 }336 Ok(Val::try_num(arr.iter().sum::<f64>() / (arr.len() as f64))?)337}338339#[builtin]340pub fn builtin_remove_at(arr: ArrValue, at: i32) -> Result<ArrValue> {341 let newArrLeft = arr.clone().slice(None, Some(at), None);342 let newArrRight = arr.slice(Some(at + 1), None, None);343344 Ok(ArrValue::extended(newArrLeft, newArrRight))345}346347#[builtin]348pub fn builtin_remove(arr: ArrValue, elem: Val) -> Result<ArrValue> {349 for (index, item) in arr.iter().enumerate() {350 if equals(&item?, &elem)? {351 return builtin_remove_at(arr.clone(), index as i32);352 }353 }354 Ok(arr)355}356357#[builtin]358pub fn builtin_flatten_arrays(arrs: Vec<ArrValue>) -> ArrValue {359 pub fn flatten_inner(values: &[ArrValue]) -> ArrValue {360 if values.len() == 1 {361 return values[0].clone();362 } else if values.len() == 2 {363 return ArrValue::extended(values[0].clone(), values[1].clone());364 }365 let (a, b) = values.split_at(values.len() / 2);366 ArrValue::extended(flatten_inner(a), flatten_inner(b))367 }368 if arrs.is_empty() {369 return ArrValue::empty();370 } else if arrs.len() == 1 {371 return arrs.into_iter().next().expect("single");372 }373 flatten_inner(&arrs)374}375376#[builtin]377pub fn builtin_flatten_deep_array(value: Val) -> Result<Vec<Val>> {378 fn process(value: Val, out: &mut Vec<Val>) -> Result<()> {379 match value {380 Val::Arr(arr) => {381 for ele in arr.iter() {382 process(ele?, out)?;383 }384 }385 _ => out.push(value),386 }387 Ok(())388 }389 let mut out = Vec::new();390 process(value, &mut out)?;391 Ok(out)392}393394#[builtin]395pub fn builtin_prune(396 a: Val,397398 #[default(false)]399 #[cfg(feature = "exp-preserve-order")]400 preserve_order: bool,401) -> Result<Val> {402 fn is_content(val: &Val) -> bool {403 match val {404 Val::Null => false,405 Val::Arr(a) => !a.is_empty(),406 Val::Obj(o) => !o.is_empty(),407 _ => true,408 }409 }410 Ok(match a {411 Val::Arr(a) => {412 let mut out = Vec::new();413 for (i, ele) in a.iter().enumerate() {414 let ele = ele415 .and_then(|v| {416 builtin_prune(417 v,418 #[cfg(feature = "exp-preserve-order")]419 preserve_order,420 )421 })422 .with_description(|| format!("elem <{i}> pruning"))?;423 if is_content(&ele) {424 out.push(ele);425 }426 }427 Val::Arr(ArrValue::eager(out))428 }429 Val::Obj(o) => {430 let mut out = ObjValueBuilder::new();431 for (name, value) in o.iter(432 #[cfg(feature = "exp-preserve-order")]433 preserve_order,434 ) {435 let value = value436 .and_then(|v| {437 builtin_prune(438 v,439 #[cfg(feature = "exp-preserve-order")]440 preserve_order,441 )442 })443 .with_description(|| format!("field <{name}> pruning"))?;444 if !is_content(&value) {445 continue;446 }447 out.field(name).value(value);448 }449 Val::Obj(out.build())450 }451 _ => a,452 })453}