Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loops doesn’t like nested arrays #23

jochenkiene opened this issue Nov 18, 2019 · 0 comments

Loops doesn’t like nested arrays #23

jochenkiene opened this issue Nov 18, 2019 · 0 comments


Copy link

@jochenkiene jochenkiene commented Nov 18, 2019

Consider you have two structs:

struct Test1 {
    let a: String
    let b: Test2
struct Test2 {
    let c: [String]

If you have Test1 as Data for a leaf template, iterating over b.c is not possible.

Consider the following workaround:

diff --git a/Sources/LeafKit/LeafSerializer.swift b/Sources/LeafKit/LeafSerializer.swift
index 9fa7a2b..7ee3a2f 100644
--- a/Sources/LeafKit/LeafSerializer.swift
+++ b/Sources/LeafKit/LeafSerializer.swift
@@ -100,7 +100,21 @@ struct LeafSerializer {
     mutating func serialize(_ loop: Syntax.Loop) throws {
-        guard let array = data[loop.array]?.array else { throw "expected array at key: \(loop.array)" }
+        let arrayPath = loop.array.split(separator: ".")
+        var data =
+        for component in arrayPath.dropLast() {
+            guard let subData = data[String(component)] else {
+                throw "did not found component \(component) in path \(loop.array)"
+            }
+            if let dictionary = subData.dictionary {
+                data = dictionary
+            }
+            else {
+                throw "expected dictionary for component \(component) in path \(loop.array)"
+            }
+        }
+        guard let array = data[String(arrayPath.last!)]?.array else { throw "expected array at key: \(loop.array)" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.