difftreelog
feat support standalone super
in: master
3 files changed
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -24,8 +24,8 @@
NoTopLevelObjectFound,
#[error("self is only usable inside objects")]
CantUseSelfOutsideOfObject,
- #[error("super is only usable inside objects")]
- CantUseSuperOutsideOfObject,
+ #[error("no super found")]
+ NoSuperFound,
#[error("for loop can only iterate over arrays")]
InComprehensionCanOnlyIterateOverArray,
crates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth418 Literal(LiteralType::This) => {418 Literal(LiteralType::This) => {419 Val::Obj(context.this().clone().ok_or(CantUseSelfOutsideOfObject)?)419 Val::Obj(context.this().clone().ok_or(CantUseSelfOutsideOfObject)?)420 }420 }421 Literal(LiteralType::Super) => Val::Obj(422 context423 .super_obj()424 .clone()425 .ok_or(NoSuperFound)?426 .with_this(context.this().clone().unwrap()),427 ),421 Literal(LiteralType::Dollar) => {428 Literal(LiteralType::Dollar) => {422 Val::Obj(context.dollar().clone().ok_or(NoTopLevelObjectFound)?)429 Val::Obj(context.dollar().clone().ok_or(NoTopLevelObjectFound)?)423 }430 }434 || format!("variable <{}>", name),441 || format!("variable <{}>", name),435 || Ok(context.binding(name.clone())?.evaluate()?),442 || Ok(context.binding(name.clone())?.evaluate()?),436 )?,443 )?,437 Index(LocExpr(v, _), index) if matches!(&**v, Expr::Literal(LiteralType::Super)) => {438 let name = evaluate(context.clone(), index)?.try_cast_str("object index")?;439 context440 .super_obj()441 .clone()442 .expect("no super found")443 .get_raw(name, Some(&context.this().clone().expect("no this found")))?444 .expect("value not found")445 }446 Index(value, index) => {444 Index(value, index) => {447 match (evaluate(context.clone(), value)?, evaluate(context, index)?) {445 match (evaluate(context.clone(), value)?, evaluate(context, index)?) {448 (Val::Obj(v), Val::Str(s)) => {446 (Val::Obj(v), Val::Str(s)) => {620 import_location.pop();618 import_location.pop();621 Val::Str(with_state(|s| s.import_file_str(import_location, path))?)619 Val::Str(with_state(|s| s.import_file_str(import_location, path))?)622 }620 }623 Literal(LiteralType::Super) => throw!(StandaloneSuper),624 })621 })625}622}626623crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -931,6 +931,25 @@
Ok(())
}
+ #[test]
+ fn standalone_super() -> crate::error::Result<()> {
+ assert_eval!(
+ r#"
+ local obj = {
+ a: 1,
+ b: 2,
+ c: 3,
+ };
+ local test = obj + {
+ fields: std.objectFields(super),
+ d: 5,
+ };
+ test.fields == ['a', 'b', 'c']
+ "#
+ );
+ Ok(())
+ }
+
struct TestImportResolver(IStr);
impl crate::import::ImportResolver for TestImportResolver {
fn resolve_file(&self, _: &PathBuf, _: &PathBuf) -> crate::error::Result<Rc<PathBuf>> {