diff --git a/ObjectMapper/Core/FromJSON.swift b/ObjectMapper/Core/FromJSON.swift index 335e2eb1..a8d82f38 100755 --- a/ObjectMapper/Core/FromJSON.swift +++ b/ObjectMapper/Core/FromJSON.swift @@ -29,48 +29,6 @@ internal final class FromJSON { } } - /// Array of Raw representable - class func rawRepresentableArray(inout field: [N], object: [N.RawValue]?) { - if let values = object { - field = values.filterMap { N(rawValue: $0) } - } - } - - /// Array of Raw representable - class func rawRepresentableArray(inout field: [N]?, object: [N.RawValue]?) { - if let values = object { - field = values.filterMap { N(rawValue: $0) } - } - } - - /// Array of Raw representable - class func rawRepresentableArray(inout field: [N]!, object: [N.RawValue]?) { - if let values = object { - field = values.filterMap { N(rawValue: $0) } - } - } - - /// Dictionary of Raw representable - class func rawRepresentableDict(inout field: [String: N], object: [String: N.RawValue]?) { - if let values = object { - field = values.filterMap { N(rawValue: $0) } - } - } - - /// Dictionary of Raw representable - class func rawRepresentableDict(inout field: [String: N]?, object: [String: N.RawValue]?) { - if let values = object { - field = values.filterMap { N(rawValue: $0) } - } - } - - /// Dictionary of Raw representable - class func rawRepresentableDict(inout field: [String: N]!, object: [String: N.RawValue]?) { - if let values = object { - field = values.filterMap { N(rawValue: $0) } - } - } - /// Mappable object class func object(inout field: N, object: AnyObject?) { if let value: N = Mapper().map(object) { diff --git a/ObjectMapper/Core/Operators.swift b/ObjectMapper/Core/Operators.swift index 6f2398f6..8f1076a3 100755 --- a/ObjectMapper/Core/Operators.swift +++ b/ObjectMapper/Core/Operators.swift @@ -76,33 +76,21 @@ public func <- (inout left: T!, right: Map) { * Array of Raw Representable object */ public func <- (inout left: [T], right: Map) { - if right.mappingType == MappingType.FromJSON { - FromJSON.rawRepresentableArray(&left, object: right.value()) - } else { - ToJSON.rawRepresentableArray(left, key: right.currentKey!, dictionary: &right.JSONDictionary) - } + left <- (right, EnumTransform()) } /** * Array of Raw Representable object */ public func <- (inout left: [T]?, right: Map) { - if right.mappingType == MappingType.FromJSON { - FromJSON.rawRepresentableArray(&left, object: right.value()) - } else { - ToJSON.rawRepresentableArray(left, key: right.currentKey!, dictionary: &right.JSONDictionary) - } + left <- (right, EnumTransform()) } /** * Array of Raw Representable object */ public func <- (inout left: [T]!, right: Map) { - if right.mappingType == MappingType.FromJSON { - FromJSON.rawRepresentableArray(&left, object: right.value()) - } else { - ToJSON.rawRepresentableArray(left, key: right.currentKey!, dictionary: &right.JSONDictionary) - } + left <- (right, EnumTransform()) } // MARK:- Dictionaries of Raw Representable type @@ -110,36 +98,25 @@ public func <- (inout left: [T]!, right: Map) { * Dictionary of Raw Representable object */ public func <- (inout left: [String: T], right: Map) { - if right.mappingType == MappingType.FromJSON { - FromJSON.rawRepresentableDict(&left, object: right.value()) - } else { - ToJSON.rawRepresentableDict(left, key: right.currentKey!, dictionary: &right.JSONDictionary) - } + left <- (right, EnumTransform()) } /** * Dictionary of Raw Representable object */ public func <- (inout left: [String: T]?, right: Map) { - if right.mappingType == MappingType.FromJSON { - FromJSON.rawRepresentableDict(&left, object: right.value()) - } else { - ToJSON.rawRepresentableDict(left, key: right.currentKey!, dictionary: &right.JSONDictionary) - } + left <- (right, EnumTransform()) } /** * Dictionary of Raw Representable object */ public func <- (inout left: [String: T]!, right: Map) { - if right.mappingType == MappingType.FromJSON { - FromJSON.rawRepresentableDict(&left, object: right.value()) - } else { - ToJSON.rawRepresentableDict(left, key: right.currentKey!, dictionary: &right.JSONDictionary) - } + left <- (right, EnumTransform()) } // MARK:- Transforms + /** * Object of Basic type with Transform */ @@ -179,6 +156,110 @@ public func <- (inout l } } +/// Array of Basic type with Transform +public func <- (inout left: [T.Object], right: (Map, T)) { + let (map, transform) = right + if map.mappingType == MappingType.FromJSON { + let values = fromJSONArrayWithTransform(map.currentValue, transform) + FromJSON.basicType(&left, object: values) + } else { + let values = toJSONArrayWithTransform(left, transform) + ToJSON.optionalBasicType(values, key: map.currentKey!, dictionary: &map.JSONDictionary) + } +} + +/// Optional array of Basic type with Transform +public func <- (inout left: [T.Object]?, right: (Map, T)) { + let (map, transform) = right + if map.mappingType == MappingType.FromJSON { + let values = fromJSONArrayWithTransform(map.currentValue, transform) + FromJSON.optionalBasicType(&left, object: values) + } else { + let values = toJSONArrayWithTransform(left, transform) + ToJSON.optionalBasicType(values, key: map.currentKey!, dictionary: &map.JSONDictionary) + } +} + +/// Implicitly unwrapped optional array of Basic type with Transform +public func <- (inout left: [T.Object]!, right: (Map, T)) { + let (map, transform) = right + if map.mappingType == MappingType.FromJSON { + let values = fromJSONArrayWithTransform(map.currentValue, transform) + FromJSON.optionalBasicType(&left, object: values) + } else { + let values = toJSONArrayWithTransform(left, transform) + ToJSON.optionalBasicType(values, key: map.currentKey!, dictionary: &map.JSONDictionary) + } +} + +/// Dictionary of Basic type with Transform +public func <- (inout left: [String: T.Object], right: (Map, T)) { + let (map, transform) = right + if map.mappingType == MappingType.FromJSON { + let values = fromJSONDictionaryWithTransform(map.currentValue, transform) + FromJSON.basicType(&left, object: values) + } else { + let values = toJSONDictionaryWithTransform(left, transform) + ToJSON.optionalBasicType(values, key: map.currentKey!, dictionary: &map.JSONDictionary) + } +} + +/// Optional dictionary of Basic type with Transform +public func <- (inout left: [String: T.Object]?, right: (Map, T)) { + let (map, transform) = right + if map.mappingType == MappingType.FromJSON { + let values = fromJSONDictionaryWithTransform(map.currentValue, transform) + FromJSON.optionalBasicType(&left, object: values) + } else { + let values = toJSONDictionaryWithTransform(left, transform) + ToJSON.optionalBasicType(values, key: map.currentKey!, dictionary: &map.JSONDictionary) + } +} + +/// Implicitly unwrapped optional dictionary of Basic type with Transform +public func <- (inout left: [String: T.Object]!, right: (Map, T)) { + let (map, transform) = right + if map.mappingType == MappingType.FromJSON { + let values = fromJSONDictionaryWithTransform(map.currentValue, transform) + FromJSON.optionalBasicType(&left, object: values) + } else { + let values = toJSONDictionaryWithTransform(left, transform) + ToJSON.optionalBasicType(values, key: map.currentKey!, dictionary: &map.JSONDictionary) + } +} + +private func fromJSONArrayWithTransform(input: AnyObject?, transform: T) -> [T.Object] { + if let values = input as? [AnyObject] { + return values.filterMap { value in + return transform.transformFromJSON(value) + } + } else { + return [] + } +} + +private func fromJSONDictionaryWithTransform(input: AnyObject?, transform: T) -> [String: T.Object] { + if let values = input as? [String: AnyObject] { + return values.filterMap { value in + return transform.transformFromJSON(value) + } + } else { + return [:] + } +} + +private func toJSONArrayWithTransform(input: [T.Object]?, transform: T) -> [T.JSON]? { + return input?.filterMap { value in + return transform.transformToJSON(value) + } +} + +private func toJSONDictionaryWithTransform(input: [String: T.Object]?, transform: T) -> [String: T.JSON]? { + return input?.filterMap { value in + return transform.transformToJSON(value) + } +} + // MARK:- Mappable Objects - /** * Object conforming to Mappable diff --git a/ObjectMapper/Core/ToJSON.swift b/ObjectMapper/Core/ToJSON.swift index 3e04217f..3c7e3871 100644 --- a/ObjectMapper/Core/ToJSON.swift +++ b/ObjectMapper/Core/ToJSON.swift @@ -99,29 +99,6 @@ internal final class ToJSON { } } - class func rawRepresentableArray(field: [N], key: String, inout dictionary: [String : AnyObject]) { - basicType(field.map { e in e.rawValue }, key: key, dictionary: &dictionary) - } - - class func rawRepresentableArray(field: [N]?, key: String, inout dictionary: [String : AnyObject]) { - if let field = field { - rawRepresentableArray(field, key: key, dictionary: &dictionary) - } - } - - class func rawRepresentableDict(field: [String: N], key: String, inout dictionary: [String : AnyObject]) { - let raw: [String: N.RawValue] = field.map { key, value in - return (key, value.rawValue) - } - basicType(raw, key: key, dictionary: &dictionary) - } - - class func rawRepresentableDict(field: [String: N]?, key: String, inout dictionary: [String : AnyObject]) { - if let field = field { - rawRepresentableDict(field, key: key, dictionary: &dictionary) - } - } - class func object(field: N, key: String, inout dictionary: [String : AnyObject]) { setValue(Mapper().toJSON(field), forKey: key, dictionary: &dictionary) }