Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Artur Rymarz artrmz

🎯
Focusing
View GitHub Profile
View filter.swift
{{ property.name|swiftProperty }}
View extension3.swift
let ext = Extension()
ext.registerFilter("swiftProperty", filter: Extension.Filters.swiftProperty)
Environment(loader: FileSystemLoader(bundle: [Bundle.main]), extensions: [
`extension`
])
View extension2.swift
extension Extension {
struct Filters {
enum Error: Swift.Error {
case invalidInputType
case invalidOption(option: String)
}
static func swiftProperty(_ value: Any?) throws -> Any? {
let string = try Filters.parseString(from: value)
return string.swiftProperty
View extension.swift
extension String {
static func indent(level: Int) -> String {
guard level > 0 else {
return ""
}
let prefix = String(repeating: " ", count: level)
return prefix
}
View nested.swift
{% include "templates/xsd-type-properties.stencil" %}
View rendered.swift
import Foundation
internal struct OrdinaryCoding {
let title: String
let description: String
let address: URL
}
View rendering.swift
let item = MyStruct(name: "OrdinaryCoding", properties: [
MyProperty(name: "title", type: "String"),
MyProperty(name: "description", type: "String"),
MyProperty(name: "address", type: "URL")
])
let rendered = try? myTemplate.render(item.dictionary)
View template.swift
import Foundation
internal struct {{ name }} {
{% for property in properties %} let {{ property.name }}: {{ property.type }}
{% endfor %}}
View structs.swift
struct MyStruct {
let name: String
let properties: [MyProperty]
var dictionary: [String: Any] {
return [
"name": name,
"properties": properties.map { $0.dictionary }
]
}
}
View template.swift
let myTemplate = environment.loadTemplate(name: "<TEMPLATE_FILE>") // 1