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

Fixed using spaces in filter expressions and variables lists #178

Merged
merged 5 commits into from
May 13, 2018

Conversation

ilyapuchka
Copy link
Collaborator

@ilyapuchka ilyapuchka commented Dec 28, 2017

When parsing filter expression spaces were not trimmed in filter name and parameters. Also this fixes using spaces in any other nodes where smart split by spaces is used, i.e. in for node. It allows use of a, b or a , b, but not a ,b. The same about | and : separators. So this also fixes #169

@ilyapuchka ilyapuchka requested a review from kylef December 28, 2017 21:09
@ilyapuchka ilyapuchka changed the title Fixed using spaces in filter expression Fixed using spaces in filter expressions and variables lists Dec 29, 2017
@ilyapuchka ilyapuchka force-pushed the fix-filter-splaces branch 2 times, most recently from 140f880 to d1507d3 Compare December 29, 2017 02:20
}
}

// joins back components around characters used in variables lists and filters
private func smartJoin(_ components: [String]) -> [String] {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this have performance implications?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@yonaskolb thanks for pointing it out =) I've reimplemented it, should be faster now.

@ilyapuchka ilyapuchka force-pushed the fix-filter-splaces branch 6 times, most recently from 8ffc3a1 to 2fcb427 Compare December 29, 2017 04:03
@ilyapuchka ilyapuchka mentioned this pull request Jan 16, 2018
12 tasks
@ilyapuchka
Copy link
Collaborator Author

@kylef can we merge this? I believe this will improve templates readability

let specialCharacters = ",|:"
func appendWord(_ word: String) {
if components.count > 0 &&
(specialCharacters.characters.contains(components.last!.characters.last!) ||
Copy link
Collaborator

Choose a reason for hiding this comment

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

😱 all those force unwraps 😢

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

🤷‍♂️ it's all safe 🙄

Copy link
Collaborator

Choose a reason for hiding this comment

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

if let lastCompChar = components.last?.characters.last, (specialCharacters.contains(lastCompChar) || …)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ok, ok, pony saved 🦄 😄

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

} else {
components.append(word)
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wouldn't it be more logical to use a Set<Character> here which would seem more appropriate and already handle all that append-without-duplicates logic for you already?

Copy link
Collaborator Author

@ilyapuchka ilyapuchka May 10, 2018

Choose a reason for hiding this comment

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

That's not exactly what we are doing here. We are checking if a word is a special character (for the case when it is surrounded by spaces, i.e. in |), or if previous word ends with a special character, in which case we concatenate last and new word, so ars| default or ars | default(but not ars |default) ends up as ars|default.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah right gotcha

$0.it("can render a filter") {
let templateString = "{% for article in ars|default:articles %}" +
$0.it("can render a filter with spaces") {
let templateString = "{% for article in ars | default: a, b , articles %}" +
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure I'm familiar with that default filter accepting 3 arguments?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It supports variadic fallbacks for the default value. If a and b are not defined then articles would be there.

Copy link
Collaborator

@AliSoftware AliSoftware May 10, 2018

Choose a reason for hiding this comment

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

Cool! TIL 👍

CHANGELOG.md Outdated
@@ -24,6 +24,7 @@
- Integer literals now resolve into Int values, not Float
- Fixed accessing properties of optional properties via reflection
- No longer render optional values in arrays as `Optional(..)`
- Adds support for using spaces in filter expression
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this is an enhancement not a bug fix, it was never a bug it just wasn't designed to work like that before.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I only considered is as a bug as docs for for tag actually use spaces, but I move it to new features section.

@ilyapuchka
Copy link
Collaborator Author

@AliSoftware @yonaskolb is it good to go?

@AliSoftware
Copy link
Collaborator

I'm still not a fan of force-unwrap even especially when we have better constructs to use instead

@ilyapuchka ilyapuchka merged commit 39ed9aa into master May 13, 2018
@ilyapuchka ilyapuchka deleted the fix-filter-splaces branch May 13, 2018 00:06
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.

4 participants