From 633f494e18ed1b7161168767dd9afa574570c4b6 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Fri, 12 Mar 2021 09:55:12 +0000 Subject: [PATCH] Moving stuff about, renaming --- Sources/HummingbirdMustache/Library.swift | 6 ++- Sources/HummingbirdMustache/Mirror.swift | 17 ++++++++ Sources/HummingbirdMustache/Renderable.swift | 35 ---------------- .../HummingbirdMustache/Template+Render.swift | 42 +++++++++++++------ .../TemplateRendererTests.swift | 14 +++---- 5 files changed, 59 insertions(+), 55 deletions(-) create mode 100644 Sources/HummingbirdMustache/Mirror.swift delete mode 100644 Sources/HummingbirdMustache/Renderable.swift diff --git a/Sources/HummingbirdMustache/Library.swift b/Sources/HummingbirdMustache/Library.swift index 7eb7fe7..f3e5d21 100644 --- a/Sources/HummingbirdMustache/Library.swift +++ b/Sources/HummingbirdMustache/Library.swift @@ -8,10 +8,14 @@ public class HBMustacheLibrary { templates[name] = template } + public func getTemplate(named name: String) -> HBMustacheTemplate? { + templates[name] + } + public func render(_ object: Any, withTemplateNamed name: String) -> String? { guard let template = templates[name] else { return nil } return template.render(object, library: self) } - var templates: [String: HBMustacheTemplate] + private var templates: [String: HBMustacheTemplate] } diff --git a/Sources/HummingbirdMustache/Mirror.swift b/Sources/HummingbirdMustache/Mirror.swift new file mode 100644 index 0000000..6ade425 --- /dev/null +++ b/Sources/HummingbirdMustache/Mirror.swift @@ -0,0 +1,17 @@ + + +func unwrapOptional(_ object: Any) -> Any? { + let mirror = Mirror(reflecting: object) + guard mirror.displayStyle == .optional else { return object } + guard let first = mirror.children.first else { return nil } + return first.value +} + +extension Mirror { + func getValue(forKey key: String) -> Any? { + guard let matched = children.filter({ $0.label == key }).first else { + return nil + } + return unwrapOptional(matched.value) + } +} diff --git a/Sources/HummingbirdMustache/Renderable.swift b/Sources/HummingbirdMustache/Renderable.swift deleted file mode 100644 index 84c2cfa..0000000 --- a/Sources/HummingbirdMustache/Renderable.swift +++ /dev/null @@ -1,35 +0,0 @@ - -protocol HBMustacheParent { - func child(named: String) -> Any? -} - -extension HBMustacheParent { - // default child to nil - func child(named: String) -> Any? { return nil } -} - -extension Dictionary: HBMustacheParent where Key == String { - func child(named: String) -> Any? { return self[named] } -} - -protocol HBSequence { - func renderSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String - func renderInvertedSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String -} - -extension Array: HBSequence { - func renderSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String { - var string = "" - for obj in self { - string += template.render(obj, library: library) - } - return string - } - - func renderInvertedSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String { - if count == 0 { - return template.render(self, library: library) - } - return "" - } -} diff --git a/Sources/HummingbirdMustache/Template+Render.swift b/Sources/HummingbirdMustache/Template+Render.swift index 16730f6..f6a3ac2 100644 --- a/Sources/HummingbirdMustache/Template+Render.swift +++ b/Sources/HummingbirdMustache/Template+Render.swift @@ -69,7 +69,7 @@ extension HBMustacheTemplate { childObject = customBox.child(named: name) } else { let mirror = Mirror(reflecting: object) - childObject = mirror.getAttribute(forKey: name) + childObject = mirror.getValue(forKey: name) } guard childObject != nil else { return nil } let names2 = names.dropFirst() @@ -100,19 +100,37 @@ extension HBMustacheTemplate { } } -func unwrap(_ any: Any) -> Any? { - let mirror = Mirror(reflecting: any) - guard mirror.displayStyle == .optional else { return any } - guard let first = mirror.children.first else { return nil } - return first.value +protocol HBMustacheParent { + func child(named: String) -> Any? } -extension Mirror { - func getAttribute(forKey key: String) -> Any? { - guard let matched = children.filter({ $0.label == key }).first else { - return nil +extension HBMustacheParent { + // default child to nil + func child(named: String) -> Any? { return nil } +} + +extension Dictionary: HBMustacheParent where Key == String { + func child(named: String) -> Any? { return self[named] } +} + +protocol HBSequence { + func renderSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String + func renderInvertedSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String +} + +extension Array: HBSequence { + func renderSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String { + var string = "" + for obj in self { + string += template.render(obj, library: library) } - return unwrap(matched.value) + return string + } + + func renderInvertedSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String { + if count == 0 { + return template.render(self, library: library) + } + return "" } } - diff --git a/Tests/HummingbirdMustacheTests/TemplateRendererTests.swift b/Tests/HummingbirdMustacheTests/TemplateRendererTests.swift index 612ea86..0205f14 100644 --- a/Tests/HummingbirdMustacheTests/TemplateRendererTests.swift +++ b/Tests/HummingbirdMustacheTests/TemplateRendererTests.swift @@ -110,17 +110,17 @@ final class TemplateRendererTests: XCTestCase { func testMustacheManualExample2() throws { let template = try HBMustacheTemplate(string: """ - * {{name}} - * {{age}} - * {{company}} - * {{{company}}} + *{{name}} + *{{age}} + *{{company}} + *{{{company}}} """) let object: [String: Any] = ["name": "Chris", "company": "GitHub"] XCTAssertEqual(template.render(object), """ - * Chris + *Chris * - * <b>GitHub</b> - * GitHub + *<b>GitHub</b> + *GitHub """) }