git.delta.rocks / jrsonnet / refs/commits / e46de0a3e23e

difftreelog

fix accept null as std.slice argument/in slicing syntax

Yaroslav Bolyukin2024-11-03parent: #7160d47.patch.diff
in: master

3 files changed

modifiedcrates/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)
 				}
modifiedcrates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth
655 }655 }
656}656}
657
658impl<T> Typed for Option<T>
659where
660 T: Typed,
661{
662 const TYPE: &'static ComplexValType =
663 &ComplexValType::UnionRef(&[&ComplexValType::Simple(ValType::Null), T::TYPE]);
664
665 fn into_untyped(typed: Self) -> Result<Val> {
666 typed.map_or_else(|| Ok(Val::Null), |v| T::into_untyped(v))
667 }
668
669 fn from_untyped(untyped: Val) -> Result<Self> {
670 if matches!(untyped, Val::Null) {
671 Ok(None)
672 } else {
673 T::from_untyped(untyped).map(Some)
674 }
675 }
676}
657677
658pub struct NativeFn<D: NativeDesc>(D::Value);678pub struct NativeFn<D: NativeDesc>(D::Value);
659impl<D: NativeDesc> Deref for NativeFn<D> {679impl<D: NativeDesc> Deref for NativeFn<D> {
modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -48,11 +48,13 @@
 #[builtin]
 pub fn builtin_slice(
 	indexable: IndexableVal,
-	index: Option<i32>,
-	end: Option<i32>,
-	step: Option<BoundedUsize<1, { i32::MAX as usize }>>,
+	index: Option<Option<i32>>,
+	end: Option<Option<i32>>,
+	step: Option<Option<BoundedUsize<1, { i32::MAX as usize }>>>,
 ) -> Result<Val> {
-	indexable.slice(index, end, step).map(Val::from)
+	indexable
+		.slice(index.flatten(), end.flatten(), step.flatten())
+		.map(Val::from)
 }
 
 #[builtin]