Sourcery icon indicating copy to clipboard operation
Sourcery copied to clipboard

Attributes don't work properly

Open LuckyDmitry opened this issue 2 years ago • 4 comments

Hi, I'm trying to use attributes to generate FeatureFlags for app. I use them as description. I retrive it like that.

<%_ for variable in type.variables { -%> <%_ if let newVal = variable.attributes["FeatureFlag"] { -%> <%= "\(newVal[0])" %> <%_}%> <%_}%> It works fine, I got my struct, but to extract remoteFeatureName for example I need to use .arguments like that

Struct before

@FeatureFlag( defaultValue: false, remoteFeatureName: "provision_enabled", path: "provisioning_features.is_enabled", description: nil) }

After newVal[0]..arguments

["defaultValue": false, "remoteFeatureName": provision_enabled, "description": nil, "path": provisioning_features.is_enabled]

It removes quotes from strings and to fix it I can wrap this string, but I have more complex structres in which I can't do it. Please, help me. How can I overcome it and extract these fields without losing the quotes?

LuckyDmitry avatar Mar 24 '22 16:03 LuckyDmitry

I'm facing exactly the same problem. https://github.com/krzysztofzablocki/Sourcery/issues/1051

I have an idea 💡. Let sourcery run and create the attributes with arguments as it is. @Foo(key: "I am a string")

Then after sourcery run a text processing script to use a regex and remove the unwanted things for the above.

And you will have "I am a string"

Nikoloutsos avatar Apr 11 '22 08:04 Nikoloutsos

Wanted to use the same approach, but struggling in making |replace: ",.*","",regex to work. It seems like it doesn't use the mode modifier. It would be great if I could get arguments array, but I can't as well...

artem-alexandrov avatar Sep 29 '22 15:09 artem-alexandrov

@artem-alexandrov might be old Stencil / StencilSwiftKit version, are you on newest Sourcery?

krzysztofzablocki avatar Sep 29 '22 15:09 krzysztofzablocki

Yep, it's 1.8.2.

Maybe it's something in the syntax, I'm trying to use it like this:

{% for variable in type["FeatureFlags"].variables where variable|!annotated:"skip" %}
    {% for attribute in variable.attributes %}
        {% set attributeString %}{{ variable.attributes[attribute] }}{% endset %}
        {{ attributeString|replace:",.*","",regex }}
    {% endfor %}
{% endfor %}

just using attribute doesn't work, it only has the attribute name, also variable.attributes[attribute]|replace... doesn't work with invalidInputType error too.


Update: I ended up rewriting template to use swifttemplate... It parses params from property wrapper as dictionary, just what I needed.

artem-alexandrov avatar Sep 29 '22 16:09 artem-alexandrov