difftreelog
feat support legacy std.thisFile
in: master
5 files changed
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -140,6 +140,11 @@
#[error("sort error: {0}")]
Sort(#[from] SortError),
+ /// Thrown as error, as this is legacy feature, and error here
+ /// is acceptable for defeating object field cache
+ #[error("should not reach outside: std.thisFile")]
+ MagicThisFileUsed,
+
#[cfg(feature = "anyhow-error")]
#[error(transparent)]
Other(Rc<anyhow::Error>),
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -568,12 +568,13 @@
(Val::Obj(v), Val::Str(key)) => s.push(
CallLocation::new(loc),
|| format!("field <{}> access", key),
- || {
- if let Some(v) = v.get(s.clone(), key.clone())? {
- Ok(v)
- } else {
- throw!(NoSuchField(key.clone()))
+ || match v.get(s.clone(), key.clone()) {
+ Ok(Some(v)) => Ok(v),
+ Ok(None) => throw!(NoSuchField(key.clone())),
+ Err(e) if matches!(e.error(), MagicThisFileUsed) => {
+ Ok(Val::Str(loc.0.to_string_lossy().into()))
}
+ Err(e) => Err(e),
},
)?,
(Val::Obj(_), n) => throw!(ValueIndexMustBeTypeGot(
@@ -674,6 +675,7 @@
.with(|b| b.get(name).copied())
.ok_or_else(|| IntrinsicNotFound(name.clone()))?,
)),
+ IntrinsicThisFile => return Err(MagicThisFileUsed.into()),
AssertExpr(assert, returned) => {
evaluate_assert(s.clone(), ctx.clone(), assert)?;
evaluate(s, ctx, returned)?
crates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -296,6 +296,8 @@
Index(LocExpr, LocExpr),
/// function(x) x
Function(ParamsDesc, LocExpr),
+ /// std.thisFile
+ IntrinsicThisFile,
/// std.primitiveEquals
Intrinsic(IStr),
/// if true == false then 1 else 2
crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth210 pub rule expr_basic(s: &ParserSettings) -> Expr210 pub rule expr_basic(s: &ParserSettings) -> Expr211 = literal(s)211 = literal(s)212212213 / quiet!{"$intrinsic(" name:$(id()) ")" {Expr::Intrinsic(name.into())}}213 / quiet!{"$intrinsicThisFile" {Expr::IntrinsicThisFile}}214 / quiet!{"$intrinsic(" name:$(id()) ")" {Expr::Intrinsic(name.into())}}214215215 / string_expr(s) / number_expr(s)216 / string_expr(s) / number_expr(s)216 / array_expr(s)217 / array_expr(s)crates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -2,6 +2,9 @@
local std = self,
local id = std.id,
+ # Magic legacy field
+ thisFile:: $intrinsicThisFile,
+
# Those functions aren't normally located in stdlib
length:: $intrinsic(length),
type:: $intrinsic(type),