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
--- 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;
modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
48#[builtin]48#[builtin]
49pub fn builtin_slice(49pub fn builtin_slice(
50 indexable: IndexableVal,50 indexable: IndexableVal,
51 index: Option<i32>,51 index: Option<Option<i32>>,
52 end: Option<i32>,52 end: Option<Option<i32>>,
53 step: Option<BoundedUsize<1, { i32::MAX as usize }>>,53 step: Option<Option<BoundedUsize<1, { i32::MAX as usize }>>>,
54) -> Result<Val> {54) -> Result<Val> {
55 indexable.slice(index, end, step).map(Val::from)55 indexable
56 .slice(index.flatten(), end.flatten(), step.flatten())
57 .map(Val::from)
56}58}
5759