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

Allow accessing the globals with index without a preceding identifier #560

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions crates/core/src/model/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ use super::ValueView;
pub struct Path<'s>(Vec<ScalarCow<'s>>);

impl<'s> Path<'s> {
pub fn empty() -> Self {
Path(vec![])
}

/// Create a `Value` reference.
pub fn with_index<I: Into<ScalarCow<'s>>>(value: I) -> Self {
let indexes = vec![value.into()];
Expand Down
3 changes: 2 additions & 1 deletion crates/core/src/parser/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ Raw = @{ (!(TagStart | ExpressionStart) ~ ANY)+ }
// Inner parsing
Identifier = @{ (ASCII_ALPHA | "_" | NON_WHITESPACE_CONTROL_HYPHEN) ~ (ASCII_ALPHANUMERIC | "_" | NON_WHITESPACE_CONTROL_HYPHEN)* }

Variable = ${ Identifier
// For root level hash access we'd need to accept that there might not be an Identifier preceding a hash access (the brackets with a value inside)
Variable = ${ ( Identifier | ("[" ~ WHITESPACE* ~ Value ~ WHITESPACE* ~ "]") )
~ ( ("." ~ Identifier)
| ("[" ~ WHITESPACE* ~ Value ~ WHITESPACE* ~ "]")
)*
Expand Down
48 changes: 43 additions & 5 deletions crates/core/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,17 @@ fn parse_variable_pair(variable: Pair) -> Variable {
let mut indexes = variable.into_inner();

let first_identifier = indexes
.next()
.expect("A variable starts with an identifier.")
.as_str()
.to_owned();
let mut variable = Variable::with_literal(first_identifier);
.peek()
.expect("A variable starts with an identifier or an index");

let mut variable = match first_identifier.as_rule() {
Rule::Identifier => {
indexes.next();
Variable::with_literal(first_identifier.as_str().to_owned())
}
Rule::Value => Variable::empty(),
_ => unreachable!(),
};

let indexes = indexes.map(|index| match index.as_rule() {
Rule::Identifier => Expression::with_literal(index.as_str().to_owned()),
Expand Down Expand Up @@ -1153,6 +1159,38 @@ mod test {
assert_eq!(parse_variable_pair(variable), expected);
}

#[test]
fn test_parse_variable_pair_with_root_index_literals() {
let variable = LiquidParser::parse(Rule::Variable, "['bar']")
.unwrap()
.next()
.unwrap();

let indexes: Vec<Expression> = vec![Expression::Literal(Value::scalar("bar"))];

let mut expected = Variable::empty();
expected.extend(indexes);

assert_eq!(parse_variable_pair(variable), expected);
}

#[test]
fn test_parse_variable_pair_with_root_index_variable() {
let variable = LiquidParser::parse(Rule::Variable, "[foo.bar]")
.unwrap()
.next()
.unwrap();

let indexes = vec![Expression::Variable(
Variable::with_literal("foo").push_literal("bar"),
)];

let mut expected = Variable::empty();
expected.extend(indexes);

assert_eq!(parse_variable_pair(variable), expected);
}

#[test]
fn test_whitespace_control() {
let options = Language::default();
Expand Down
21 changes: 17 additions & 4 deletions crates/core/src/runtime/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,22 @@ use super::Runtime;
/// A `Value` reference.
#[derive(Clone, Debug, PartialEq)]
pub struct Variable {
variable: Scalar,
variable: Option<Scalar>,
indexes: Vec<Expression>,
}

impl Variable {
pub fn empty() -> Self {
Self {
variable: None,
indexes: Default::default(),
}
}

/// Create a `Value` reference.
pub fn with_literal<S: Into<Scalar>>(value: S) -> Self {
Self {
variable: value.into(),
variable: Some(value.into()),
indexes: Default::default(),
}
}
Expand All @@ -32,7 +39,10 @@ impl Variable {

/// Convert to a `Path`.
pub fn try_evaluate<'c>(&'c self, runtime: &'c dyn Runtime) -> Option<Path<'c>> {
let mut path = Path::with_index(self.variable.as_ref());
let mut path = match self.variable.as_ref() {
Some(v) => Path::with_index(v.clone()),
None => Path::empty(),
};
path.reserve(self.indexes.len());
for expr in &self.indexes {
let v = expr.try_evaluate(runtime)?;
Expand All @@ -47,7 +57,10 @@ impl Variable {

/// Convert to a `Path`.
pub fn evaluate<'c>(&'c self, runtime: &'c dyn Runtime) -> Result<Path<'c>> {
let mut path = Path::with_index(self.variable.as_ref());
let mut path = match self.variable.as_ref() {
Some(v) => Path::with_index(v.clone()),
None => Path::empty(),
};
path.reserve(self.indexes.len());
for expr in &self.indexes {
let v = expr.evaluate(runtime)?;
Expand Down
41 changes: 41 additions & 0 deletions tests/conformance_ruby/variable_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,47 @@ fn test_simple_variable() {
);
}

#[test]
fn test_simple_root_index_with_literal() {
assert_template_result!(r#"worked"#, r#"{{['test']}}"#, o!({"test": "worked"}));
}

#[test]
fn test_variable_index_access_with_literal() {
assert_template_result!(
r#"worked"#,
r#"{{nested['test']}}"#,
o!({"nested": {"test": "worked"}})
);
}

#[test]
fn test_nested_hash_access() {
assert_template_result!(
r#"worked"#,
r#"{{['nested']['test']}}"#,
o!({"nested": {"test": "worked"}})
);
}

#[test]
fn test_nested_literal_and_variable_access() {
assert_template_result!(
r#"worked"#,
r#"{{['nested'].test}}"#,
o!({"nested": {"test": "worked"}})
);
}

#[test]
fn test_root_index_with_variable() {
assert_template_result!(
r#"it did"#,
r#"{{[nested.test]}}"#,
o!({"nested": {"test": "worked"}, "worked": "it did" })
);
}

#[test]
#[should_panic]
fn test_variable_render_calls_to_liquid() {
Expand Down