From ab1fc1bfe37e45596775ace41857f9248465b198 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Fri, 22 Apr 2022 20:34:01 +0000 Subject: [PATCH] feat: support legacy std.thisFile --- --- 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), --- 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)? --- 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 --- a/crates/jrsonnet-parser/src/lib.rs +++ b/crates/jrsonnet-parser/src/lib.rs @@ -210,6 +210,7 @@ pub rule expr_basic(s: &ParserSettings) -> Expr = literal(s) + / quiet!{"$intrinsicThisFile" {Expr::IntrinsicThisFile}} / quiet!{"$intrinsic(" name:$(id()) ")" {Expr::Intrinsic(name.into())}} / string_expr(s) / number_expr(s) --- 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), -- gitstuff