0001    //
0002    //  ExtensionDictionary.swift
0003    //  Gloss
0004    //
0005    // Copyright (c) 2015 Harlan Kellaway
0006    //
0007    // Permission is hereby granted, free of charge, to any person obtaining a copy
0008    // of this software and associated documentation files (the "Software"), to deal
0009    // in the Software without restriction, including without limitation the rights
0010    // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0011    // copies of the Software, and to permit persons to whom the Software is
0012    // furnished to do so, subject to the following conditions:
0013    //
0014    // The above copyright notice and this permission notice shall be included in
0015    // all copies or substantial portions of the Software.
0016    //
0017    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0018    // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0019    // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0020    // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0021    // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0022    // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
0023    // THE SOFTWARE.
0024    //
0025    
0026    import Foundation
0027    
0028    public extension Dictionary {
0029        
0030        // MARK: - Public functions
0031        
0032        /**
0033         Parses the nested dictionary from the keyPath
0034         components separated with a delimiter and gets the value
0035         
0036         :parameter: keyPath   KeyPath with delimiter
0037         :parameter: delimiter Delimiter
0038         
0039         :returns: Value from the nested dictionary
0040         */
0041        public func valueForKeyPath
Decoder.swift:47
            if let value = json.valueForKeyPath(key, withDelimiter: keyPathDelimiter) as? T {
Decoder.swift:68
            if let subJSON = json.valueForKeyPath(key, withDelimiter: keyPathDelimiter) as? JSON {
Decoder.swift:90
            if let dateString = json.valueForKeyPath(key, withDelimiter: keyPathDelimiter) as? String {
Decoder.swift:127
            if let rawValue = json.valueForKeyPath(key, withDelimiter: keyPathDelimiter) as? T.RawValue {
Decoder.swift:147
            if let urlString = json.valueForKeyPath(key, withDelimiter: keyPathDelimiter) as? String,
Decoder.swift:169
            if let jsonArray = json.valueForKeyPath(key, withDelimiter: keyPathDelimiter) as? [JSON] {
Decoder.swift:198
            guard let dictionary = json.valueForKeyPath(key, withDelimiter: keyPathDelimiter) as? [String : JSON] else {
Decoder.swift:225
            if let rawValues = json.valueForKeyPath(key, withDelimiter: keyPathDelimiter) as? [T.RawValue] {
Decoder.swift:254
            if let dateStrings = json.valueForKeyPath(key, withDelimiter: keyPathDelimiter) as? [String] {
Decoder.swift:299
            if let urlStrings = json.valueForKeyPath(key, withDelimiter: keyPathDelimiter) as? [String] {
ExtensionDictionary.swift:58
            return subDict.valueForKeyPath(rejoined, withDelimiter: delimiter)
(keyPath: String, withDelimiter delimiter: String = GlossKeyPathDelimiter()) -> AnyObject? { 0042 var keys = keyPath.componentsSeparatedByString(delimiter) 0043 0044 guard let first = keys.first as? Key else { 0045 print("[Gloss] Unable to use string as key on type: \(Key.self)") 0046 return nil 0047 } 0048 0049 guard let value = self[first] as? AnyObject else { 0050 return nil 0051 } 0052 0053 keys.removeAtIndex(0) 0054 0055 if !keys.isEmpty, let subDict = value as? JSON { 0056 let rejoined = keys.joinWithSeparator(delimiter) 0057 0058 return subDict.valueForKeyPath(rejoined, withDelimiter: delimiter) 0059 } 0060 0061 return value 0062 } 0063 0064 // MARK: - Internal functions 0065 0066 /** 0067 Creates a dictionary from a list of elements, this allows us to map, flatMap 0068 and filter dictionaries. 0069 0070 :parameter: elements Elements to add to the new dictionary 0071 */ 0072 init(elements: [Element]) { 0073 self.init() 0074 0075 for (key, value) in elements { 0076 self[key] = value 0077 } 0078 } 0079 0080 /** 0081 Flat map for dictionary. 0082 0083 :parameter: transform Transform 0084 */ 0085 func flatMap
Decoder.swift:202
            return dictionary.flatMap { (key, value) in
Encoder.swift:211
            return dictionary.flatMap { (key, value) in
<KeyPrime : Hashable, ValuePrime>(transform: (Key, Value) throws -> (KeyPrime, ValuePrime)?) rethrows -> [KeyPrime : ValuePrime] { 0086 return Dictionary<KeyPrime,ValuePrime>(elements: try flatMap({ (key, value) in 0087 return try transform(key, value) 0088 })) 0089 } 0090 0091 /** 0092 Adds entries from provided dictionary 0093 0094 :parameter: other Dictionary to add entries from 0095 :parameter: delimiter Keypath delimiter 0096 */ 0097 mutating func add
Gloss.swift:110
            json.add(j!, delimiter: keyPathDelimiter)
(other: Dictionary, delimiter: String = GlossKeyPathDelimiter()) -> () { 0098 for (key, value) in other { 0099 if let key = key as? String { 0100 self.setValue(valueToSet: value, forKeyPath: key, withDelimiter: delimiter) 0101 } else { 0102 self.updateValue(value, forKey:key) 0103 } 0104 } 0105 } 0106 0107 // MARK: - Private functions 0108 0109 /** 0110 Creates a nested dictionary from the keyPath 0111 components separated with a delimiter and sets the value 0112 0113 :parameter: valueToSet Value to set 0114 :parameter: keyPath KeyPath of the value 0115 */ 0116 private mutating func setValue
ExtensionDictionary.swift:100
                self.setValue(valueToSet: value, forKeyPath: key, withDelimiter: delimiter)
ExtensionDictionary.swift:136
            subdict.setValue(valueToSet: val, forKeyPath: rejoined, withDelimiter: delimiter)
(valueToSet val: Any, forKeyPath keyPath: String, withDelimiter delimiter: String = GlossKeyPathDelimiter()) { 0117 var keys = keyPath.componentsSeparatedByString(delimiter) 0118 0119 guard let first = keys.first as? Key else { 0120 print("[Gloss] Unable to use string as key on type: \(Key.self)") 0121 return 0122 } 0123 0124 keys.removeAtIndex(0) 0125 0126 if keys.isEmpty, let settable = val as? Value { 0127 self[first] = settable 0128 } else { 0129 let rejoined = keys.joinWithSeparator(delimiter) 0130 var subdict: JSON = [:] 0131 0132 if let sub = self[first] as? JSON { 0133 subdict = sub 0134 } 0135 0136 subdict.setValue(valueToSet: val, forKeyPath: rejoined, withDelimiter: delimiter) 0137 0138 if let settable = subdict as? Value { 0139 self[first] = settable 0140 } else { 0141 print("[Gloss] Unable to set value: \(subdict) to dictionary of type: \(self.dynamicType)") 0142 } 0143 } 0144 0145 } 0146 0147 } 0148