-
Notifications
You must be signed in to change notification settings - Fork 58
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
Add map node #11
Add map node #11
Conversation
9625443
to
e9e33c6
Compare
e373eab
to
732b98d
Compare
This Node will be really useful to a lot of templates! But I wonder about the syntax, still. Should the name of the variable to store the result of the map really be part of the Map node? I mean, that node syntax reads strange to be and doesn't seem that natural to use:
I always wonder what That would be awesome if it would be possible to just use:
And that would just generate / output… an array (rendered as a joined string if rendered directly, or stored as an array if used with a
Sadly, I know that's not really possible as for now, because But I'm wondering if we couldn't find a more elegant solution that this \cc @kylef thoughts? |
That's one of the reasons why I ended up creating a modified set that accepts a 2nd argument instead of a body, to avoid losing type information. What we can do is simplify the map syntax of map to: When the loop item name isn't specified we can either:
And still allow the user to provide a name using the |
I like that |
Cool! I'll make those changes then. |
CHANGELOG.md
Outdated
@@ -8,6 +8,11 @@ | |||
|
|||
### New Features | |||
|
|||
* Aded `MapNode` to apply a `map` operator to an array. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/Aded/Added/
Tests/TestSuites/MapNodeTests.swift
Outdated
let result = try! template.render(MapNodeTests.context) | ||
|
||
let expected = Fixtures.string(for: "map-basic.out") | ||
XCTDiffStrings(result, expected) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if testing Stencil Nodes using templates is a good thing.
When comes the time when we decide to move the MapNode to Stencil itself, Stencil won't have our fixtures as templates to test anyway.
The way Stencil tests its nodes is by invoking the Node constructor, then its render method, and test the result, without needing a template fixture for node unit tests. See here. Maybe we should test at the same level too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll take a look at the Stencil tests.
I'll have to try and convert the current tests for this (and other nodes) to comparable tests using constructors and such.
43392b6
to
7c5687d
Compare
7c5687d
to
afb1eca
Compare
0665bf6
to
92175cb
Compare
92175cb
to
b7f6c93
Compare
b7f6c93
to
0499c28
Compare
@@ -19,6 +19,9 @@ _TODO: [Write more extension Documentation](https://github.com/SwiftGen/StencilS | |||
* `SetNode` | |||
* `{% set <Name> %}…{% endset %}` | |||
* Renders the nodes inside this block immediately, and stores the result in the `<Name`> variable of the current context. | |||
* `MapNode` | |||
* `{% map <Variable> into <Name> using <ItemName> %}…{% endmap %}` | |||
* Apply a `map` operator to an array, and store the result into a new array variable `<Name>` in the current context. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+ * Inside the map loop, a `maploop` special variable is available (akin to the `forloop` variable in `for` nodes). It exposes `maploop.count`, `maploop.first`, `maploop.last` and `maploop.item`.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add doc for maploop
variable, otherwise OK to merge.
0499c28
to
8171a2f
Compare
From the code gen PR: SwiftGen/SwiftGen#188