stencilproject / Stencil

Stencil is a simple and powerful template language for Swift.
https://stencil.fuller.li
BSD 2-Clause "Simplified" License
2.34k stars 223 forks source link

defect on getters / setters - iterating over nodes in context #119

Closed johndpope closed 7 years ago

johndpope commented 7 years ago

https://github.com/SwiftGen/StencilSwiftKit/issues/50

I have confirmed this sample working fine.

stencil string 
{% for article in operations %}
    <li>{{ article.title }} by {{ article.author }}</li>
{% endfor %} 

struct Article {
        let name: String
        let author: String
    }

let articleContext = [
        "articles": [
            Article(name: "Migrating from OCUnit to XCTest", author: "Kyle Fuller"),
            Article(name: "Memory Management with ARC", author: "Kyle Fuller"),
        ]
    ]

 let environment = stencilSwiftEnvironment()
 let template = StencilSwiftTemplate(templateString:stencilString,environment:environment)
 let generated = try template.render(["articles": articleContext["articles"]])

For my case, I have some swift protobuffer classes that are auto generated by Apple.

here's the actual class https://github.com/johndpope/swift-grpc-tensorflow/blob/master/Sources/op_def.pb.swift

Basically it implements SwiftProtobuf.Message public struct Tensorflow_OpDef: SwiftProtobuf.Message (protocol only)

from what I can see - the only difference between the struct Article and struct Tensorflow_OpDef

is the getters / setters

public var name: String {
    get {return _storage._name}
    set {_uniqueStorage()._name = newValue}
  }

when I reference the internal code / then it works.

{% for op in operations %}
name:{{ op._storage._name }}
{% endfor %}

Expected

{% for op in operations %}
name:{{ op.name }}
{% endfor %}

doesn't render any results.

(I get that this may not be fixable. but maybe some warnings could be added to help debug.)

screen shot 2017-05-31 at 3 21 26 pm
johndpope commented 7 years ago

i ended up resolving this by flattening out the protobuffer. perhaps some day - could use a protocol extension to simplify this. https://github.com/johndpope/tensorflow/blob/256ee4fb4b95c050275acc433005bc75b7e023a7/tensorflow/swift/Sources/GenerateOps.swift