Skip to content

Commit

Permalink
fix: Deeply nested array indexes
Browse files Browse the repository at this point in the history
The parser is pretty messed up.  I have no idea why it does what it does
but it has two different code paths for parsing variables.  In one, it
somehow was treationg `a.b` as a variable `"a.b"` and not `a["b"]`.

Not sure why it broke but it now works.

Fixes cobalt-org#230
  • Loading branch information
epage committed Nov 17, 2018
1 parent e372470 commit 59ecb6e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
31 changes: 29 additions & 2 deletions liquid-compiler/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ pub fn unexpected_token_error_string(expected: &str, actual: Option<String>) ->
// creates an expression, which wraps everything that gets rendered
fn parse_expression(tokens: &[Token], options: &LiquidOptions) -> Result<Box<Renderable>> {
match tokens.get(0) {
Some(&Token::Identifier(ref x))
Some(&Token::Identifier(_))
if tokens.len() > 1 && (tokens[1] == Token::Dot || tokens[1] == Token::OpenSquare) =>
{
let mut result = tokens[0].to_arg()?.into_variable().expect("identifiers must be variables");
let indexes = parse_indexes(&tokens[1..])?;
let mut result = Variable::with_literal(x.clone());
result.extend(indexes);
Ok(Box::new(result))
}
Expand Down Expand Up @@ -346,6 +346,7 @@ mod test_parse_expression {
use liquid_interpreter::Context;
use liquid_value::Object;
use liquid_value::Value;
use liquid_value::Array;

fn null_options() -> LiquidOptions {
LiquidOptions::default()
Expand Down Expand Up @@ -401,6 +402,32 @@ mod test_parse_expression {
let result = result.render(&mut context).unwrap();
assert_eq!("42", result);
}

#[test]
fn array_index_access() {
let tokens = granularize("post[0]").unwrap();
let mut context = Context::new();
let mut post = Array::new();
post.push(Value::scalar(42i32));
context.stack_mut().set_global("post", Value::Array(post));

let result = parse_expression(&tokens, &null_options()).unwrap();
let result = result.render(&mut context).unwrap();
assert_eq!("42", result);
}

#[test]
fn mixed_access() {
let tokens = granularize("post.child[0]").unwrap();
let mut context = Context::new();
let mut post = Object::new();
post.insert("child".into(), Value::Array(vec![Value::scalar(42i32)]));
context.stack_mut().set_global("post", Value::Object(post));

let result = parse_expression(&tokens, &null_options()).unwrap();
let result = result.render(&mut context).unwrap();
assert_eq!("42", result);
}
}

#[cfg(test)]
Expand Down
16 changes: 16 additions & 0 deletions liquid-interpreter/src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ impl Expression {
Expression::Literal(Value::scalar(literal))
}

/// Convert into a literal if possible.
pub fn into_literal(self) -> Option<Value> {
match self {
Expression::Literal(x) => Some(x),
Expression::Variable(_) => None,
}
}

/// Convert into a variable, if possible.
pub fn into_variable(self) -> Option<Variable> {
match self {
Expression::Literal(_) => None,
Expression::Variable(x) => Some(x),
}
}

/// Convert to a `Value`.
pub fn evaluate(&self, context: &Context) -> Result<Value> {
let val = match *self {
Expand Down

0 comments on commit 59ecb6e

Please sign in to comment.