Skip to content

Commit

Permalink
feat: allow lookup of contained types via dot expression by using val…
Browse files Browse the repository at this point in the history
…ue(forUndefinedKey:)
  • Loading branch information
krzysztofzablocki committed Dec 13, 2016
1 parent c6270c2 commit 368fc4b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Sourcery.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "Sourcery"
s.version = "0.3.0"
s.version = "0.3.1"
s.summary = "A tool that brings meta-programming to Swift, allowing you to code generate Swift code."

s.description = <<-DESC
Expand Down
10 changes: 10 additions & 0 deletions Sourcery/Generating/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ private class TypesReflectionBox: NSObject {
}()
}

extension Type {
override func value(forUndefinedKey key: String) -> Any? {
if let innerType = containedTypes.lazy.filter({ $0.localName == key }).first {
return innerType
}

return super.value(forUndefinedKey: key)
}
}

enum Generator {
static func generate(_ types: [Type], template: Template) throws -> String {
var typesByName = [String: Type]()
Expand Down
4 changes: 2 additions & 2 deletions Sourcery/Models/Enum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ class Enum: Type {
return false
}

init(name: String, accessLevel: AccessLevel = .internal, isExtension: Bool = false, inheritedTypes: [String] = [], cases: [Case] = [], variables: [Variable] = []) {
init(name: String, accessLevel: AccessLevel = .internal, isExtension: Bool = false, inheritedTypes: [String] = [], cases: [Case] = [], variables: [Variable] = [], containedTypes: [Type] = []) {
self.cases = cases
self.rawType = inheritedTypes.first
super.init(name: name, accessLevel: accessLevel, isExtension: isExtension, variables: variables, inheritedTypes: inheritedTypes)
super.init(name: name, accessLevel: accessLevel, isExtension: isExtension, variables: variables, inheritedTypes: inheritedTypes, containedTypes: containedTypes)
}
}
2 changes: 1 addition & 1 deletion Sourcery/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public struct FilePath {
}

func runCLI() {
let version = "0.3.0"
let version = "0.3.1"

command(
Flag("watch",
Expand Down
10 changes: 9 additions & 1 deletion SourceryTests/GeneratorSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ class GeneratorSpec: QuickSpec {
Variable(name: "fooBar", type: "Int", accessLevel: (read: .public, write: .public), isComputed: true)
]),
Struct(name: "Bar", accessLevel: .public, inheritedTypes: ["NSObject", "KnownProtocol", "Decodable"]),
Enum(name: "Options", accessLevel: .public, cases: [Enum.Case(name: "optionA"), Enum.Case(name: "optionB")]),
Enum(name: "Options", accessLevel: .public, cases: [Enum.Case(name: "optionA"), Enum.Case(name: "optionB")], containedTypes: [
Type(name: "InnerOptions", accessLevel: .public, variables: [
Variable(name: "foo", type: "Int", accessLevel: (read: .public, write: .public), isComputed: false)
])
]),
Protocol(name: "KnownProtocol")
]

Expand Down Expand Up @@ -76,6 +80,10 @@ class GeneratorSpec: QuickSpec {
expect(generate("{{ type.Foo.name }} has {{ type.Foo.variables.first.name }} variable")).to(equal("Foo has intValue variable"))
}

it("generates contained types properly, type.ParentType.ContainedType properly") {
expect(generate("{{ type.Options.InnerOptions.variables.count }} variable")).to(equal("1 variable"))
}

it("generates enum properly") {
expect(generate("{% for case in type.Options.cases %} {{ case.name }} {% endfor %}")).to(equal(" optionA optionB "))
}
Expand Down

0 comments on commit 368fc4b

Please sign in to comment.