krzysztofzablocki / Sourcery

Meta-programming for Swift, stop writing boilerplate code.
http://merowing.info
MIT License
7.66k stars 616 forks source link

Support for iteration/mapping over MethodParameters array #172

Closed dgattey closed 7 years ago

dgattey commented 7 years ago

I'd like to transform a method parameters array into a list of comma separated names. Something like this, if it were pure Swift: type.methodParameters.joinWithSeparator(",").

I'm accomplishing that right now with this: {% for parameter in method.parameters %}{{ parameter.typeName }}{% if parameter.name != method.parameters.last.name %}, {% endif %}{% endfor %} to get String?, String?, String?, Bool, BasicNetworkResponse

It'd be nice to be able to do this instead {{ method.parameters.map { $0.typeName } }} or something similar. More Swifty/clean!

krzysztofzablocki commented 7 years ago

it's more of a https://github.com/SwiftGen/StencilSwiftKit or regular Stencil issue, StencilSwiftKit already has a map node you could use and it will be available in next release as I'll update our dependencies

fpg1503 commented 7 years ago

In these situations I use the local forloop variable (last doesn't work with annotated, i.e. {% if variable.name != type.variables|instance|!annotated:"AutoInject".last.name %} makes Sourcery output nothing for all the files, it may be a bug).

Instead I use {% if not forloop.last %}, {% endif %}.

Being able to map will really help me avoid having to repeat the same transformations again and again! :D

ilyapuchka commented 7 years ago

I guess type.variables|instance|!annotated:"AutoInject".last.name is not a valid Stencil expression, everything after : in filter is considered as filter argument, but "AutoInject".last.name can not be evaluated. forloop is the right approach here given what we have now available in Stencil. Also don't forget that Sourcery supports Swift and (in master) JavaScript templates where you can use all language capabilities. It will be very valuable to have a feedback on that too.