From 7517dee10e02afe7e06938c5477ee7d8dfae4d85 Mon Sep 17 00:00:00 2001 From: Maz Jaleel Date: Sun, 21 Jan 2018 22:30:47 +0400 Subject: [PATCH] fix nested key traversal --- Sources/Map.swift | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Sources/Map.swift b/Sources/Map.swift index c7e7a5e1..e3a9a239 100644 --- a/Sources/Map.swift +++ b/Sources/Map.swift @@ -133,9 +133,10 @@ private func valueFor(_ keyPathComponents: ArraySlice, dictionary: [Stri } if let keyPath = keyPathComponents.first { + let isTail = keyPathComponents.count == 1 let object = dictionary[keyPath] if object is NSNull { - return (true, nil) + return (isTail, nil) } else if keyPathComponents.count > 1, let dict = object as? [String: Any] { let tail = keyPathComponents.dropFirst() return valueFor(tail, dictionary: dict) @@ -143,7 +144,7 @@ private func valueFor(_ keyPathComponents: ArraySlice, dictionary: [Stri let tail = keyPathComponents.dropFirst() return valueFor(tail, array: array) } else { - return (object != nil, object) + return (isTail && object != nil, object) } } @@ -162,10 +163,11 @@ private func valueFor(_ keyPathComponents: ArraySlice, array: [Any]) -> if let keyPath = keyPathComponents.first, let index = Int(keyPath) , index >= 0 && index < array.count { + let isTail = keyPathComponents.count == 1 let object = array[index] if object is NSNull { - return (true, nil) + return (isTail, nil) } else if keyPathComponents.count > 1, let array = object as? [Any] { let tail = keyPathComponents.dropFirst() return valueFor(tail, array: array) @@ -173,7 +175,7 @@ private func valueFor(_ keyPathComponents: ArraySlice, array: [Any]) -> let tail = keyPathComponents.dropFirst() return valueFor(tail, dictionary: dict) } else { - return (true, object) + return (isTail, object) } }