Skip to content

Commit

Permalink
Merge pull request #154 from CertainLach/fix/if-field-in-super-with-n…
Browse files Browse the repository at this point in the history
…o-super

Fix: field in super should not be interpreted with standalone-super concept.
  • Loading branch information
CertainLach authored Mar 21, 2024
2 parents a7b20f9 + d1e2f8e commit c2313a2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
19 changes: 17 additions & 2 deletions crates/jrsonnet-evaluator/src/evaluate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::rc::Rc;
use jrsonnet_gcmodule::{Cc, Trace};
use jrsonnet_interner::IStr;
use jrsonnet_parser::{
ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, FieldMember, FieldName, ForSpecData,
IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc,
ArgsDesc, AssertStmt, BinaryOpType, BindSpec, CompSpec, Expr, FieldMember, FieldName,
ForSpecData, IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc,
};
use jrsonnet_types::ValType;

Expand Down Expand Up @@ -439,6 +439,21 @@ pub fn evaluate(ctx: Context, expr: &LocExpr) -> Result<Val> {
Parened(e) => evaluate(ctx, e)?,
Str(v) => Val::string(v.clone()),
Num(v) => Val::new_checked_num(*v)?,
// I have tried to remove special behavior from super by implementing standalone-super
// expresion, but looks like this case still needs special treatment.
//
// Note that other jsonnet implementations will fail on `if value in (super)` expression,
// because the standalone super literal is not supported, that is because in other
// implementations `in super` treated differently from in `smth_else`.
BinaryOp(field, BinaryOpType::In, e)
if matches!(&*e.0, Expr::Literal(LiteralType::Super)) =>
{
let Some(super_obj) = ctx.super_obj() else {
return Ok(Val::Bool(false));
};
let field = evaluate(ctx.clone(), field)?;
Val::Bool(super_obj.has_field_ex(field.to_string()?, true))
}
BinaryOp(v1, o, v2) => evaluate_binary_op_special(ctx, v1, *o, v2)?,
UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,
Var(name) => State::push(
Expand Down
9 changes: 9 additions & 0 deletions tests/golden/issue153.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
local upper = {
'bar': if 'baz' in super then super.baz else 'nope',
};

local obj = {
foo+: upper,
};

obj
5 changes: 5 additions & 0 deletions tests/golden/issue153.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"foo": {
"bar": "nope"
}
}

0 comments on commit c2313a2

Please sign in to comment.