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 for variables to be passed as filter args #50

Merged
merged 5 commits into from Jul 11, 2016

Conversation

gchp
Copy link
Contributor

@gchp gchp commented Jul 8, 2016

This commit adds support for passing variables as arguments to filter.
It changes the FilterPrototype type to contain a list of VarOrVal
instances rather than just Value instances.

When filters are being applied, each VarOrVal instance is "unwrapped"
before being given to the filter invocation. For VarOrVal::Val this
means just extracting the inner Value. For VarOrVal::Var it means
looking up the variable value.

This fixes #49

gchp added 2 commits July 8, 2016 16:45
This commit adds support for passing variables as arguments to filter.
It changes the FilterPrototype type to contain a list of VarOrVal
instances rather than just Value instances.

When filters are being applied, each VarOrVal instance is "unwrapped"
before being given to the filter invocation. For VarOrVal::Val this
means just extracting the inner Value. For VarOrVal::Var it means
looking up the variable value.

This fixes cobalt-org#49
x => {
return Error::parser("a comma or a pipe", Some(x));
}
}
comma_previous = false;
first = false;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like this piece.

Essentially, a test was failing because instead of raising an error for a missing comma or pipe, the parser was eating the next identifier, producing the wrong result.

To get around this, I'm keeping track of whether or not this is the first time through the list, and whether the previous token was a comma. If a comma was not directly before the new Identifier, and its not the first time through the loop, then we raise an error. This fixes the test.

A better way to do this would probably be to look ahead after parsing the Identifier and check if the next token is either a comma or a pipe. What do you think? Any thoughts / suggestions?

Let me know if you need me to clarify. If you want to see the failing test, just revert the second commit in this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmh the original code seems generally problematic since we just swallow any value without looking at commas in between. As far as I understand the Liquid "spec" there should always be a comma or a pipe after any filter argument.

The smartest thing to do might be to add a peek after the match that checks if we have either a comma or a pipe after our argument and fail if we don't. If there is a comma we can consume it and continue and if there's a Pipe we can just break. Then you'll obviously need to remove the Comma match clause.

Do you think that'll work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so - will give it a shot and update!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@gchp
Copy link
Contributor Author

gchp commented Jul 8, 2016

Will update to fix the fmt errors. Let me know what you think re the above comment!

entry = try!(f(&entry, &filter.arguments));

let mut arguments = Vec::new();
for arg in filter.arguments.iter() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the iter can be inferred here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was getting errors when I removed the iter. Changed it to loop over &filter.arguments instead.

@johannhof
Copy link
Contributor

Hey, thanks so much for the PR! I left some comments.

Cheers

@johannhof
Copy link
Contributor

Looks great! 👍

One thing before we can merge: you accidentally committed src/parser.rs.bk (we should just add these to the .gitignore). Please remove it! :)

@gchp
Copy link
Contributor Author

gchp commented Jul 10, 2016

Whoops, sorry! Didn't realise I added that. Will update in the morning when I get back to my laptop.

@gchp
Copy link
Contributor Author

gchp commented Jul 11, 2016

Ok - updated!

@johannhof johannhof merged commit 4abd8c7 into cobalt-org:master Jul 11, 2016
@johannhof
Copy link
Contributor

Great job! Thanks! 👍

@gchp gchp deleted the filter-arguments branch July 11, 2016 09:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error using variables as filter arguments
2 participants