Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: field in super should not be interpreted with standalone-super concept. #154

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
}
Loading