From 465ddabcb0d692d1dc568c2c2baa0deeca6ddb2b Mon Sep 17 00:00:00 2001 From: Syo Ikeda Date: Thu, 30 Apr 2015 23:10:40 +0900 Subject: [PATCH 1/2] Add array and dictionary support for mapping with transform --- ObjectMapper/Core/Operators.swift | 105 ++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/ObjectMapper/Core/Operators.swift b/ObjectMapper/Core/Operators.swift index 6f2398f6..42d5f851 100755 --- a/ObjectMapper/Core/Operators.swift +++ b/ObjectMapper/Core/Operators.swift @@ -140,6 +140,7 @@ public func <- (inout left: [String: T]!, right: Map) { } // MARK:- Transforms + /** * Object of Basic type with Transform */ @@ -179,6 +180,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 From 22bb1ab05eee41997174bfd7cf6bab9b889634d3 Mon Sep 17 00:00:00 2001 From: Syo Ikeda Date: Thu, 30 Apr 2015 23:12:50 +0900 Subject: [PATCH 2/2] Refactor RawRepresentable mapping using EnumTransform --- ObjectMapper/Core/FromJSON.swift | 42 ------------------------------- ObjectMapper/Core/Operators.swift | 36 +++++--------------------- ObjectMapper/Core/ToJSON.swift | 23 ----------------- 3 files changed, 6 insertions(+), 95 deletions(-) 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 42d5f851..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,33 +98,21 @@ 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 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) }