Skip to content

Commit

Permalink
Merge pull request #117 from ikesyo/transform-array-dictionary-support
Browse files Browse the repository at this point in the history
Add array and dictionary support for mapping with transform
  • Loading branch information
tristanhimmelman committed Apr 30, 2015
2 parents c098352 + 22bb1ab commit 6734361
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 95 deletions.
42 changes: 0 additions & 42 deletions ObjectMapper/Core/FromJSON.swift
Expand Up @@ -29,48 +29,6 @@ internal final class FromJSON {
}
}

/// Array of Raw representable
class func rawRepresentableArray<N: RawRepresentable>(inout field: [N], object: [N.RawValue]?) {
if let values = object {
field = values.filterMap { N(rawValue: $0) }
}
}

/// Array of Raw representable
class func rawRepresentableArray<N: RawRepresentable>(inout field: [N]?, object: [N.RawValue]?) {
if let values = object {
field = values.filterMap { N(rawValue: $0) }
}
}

/// Array of Raw representable
class func rawRepresentableArray<N: RawRepresentable>(inout field: [N]!, object: [N.RawValue]?) {
if let values = object {
field = values.filterMap { N(rawValue: $0) }
}
}

/// Dictionary of Raw representable
class func rawRepresentableDict<N: RawRepresentable>(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<N: RawRepresentable>(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<N: RawRepresentable>(inout field: [String: N]!, object: [String: N.RawValue]?) {
if let values = object {
field = values.filterMap { N(rawValue: $0) }
}
}

/// Mappable object
class func object<N: Mappable>(inout field: N, object: AnyObject?) {
if let value: N = Mapper().map(object) {
Expand Down
141 changes: 111 additions & 30 deletions ObjectMapper/Core/Operators.swift
Expand Up @@ -76,70 +76,47 @@ public func <- <T: RawRepresentable>(inout left: T!, right: Map) {
* Array of Raw Representable object
*/
public func <- <T: RawRepresentable>(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 <- <T: RawRepresentable>(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 <- <T: RawRepresentable>(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
/**
* Dictionary of Raw Representable object
*/
public func <- <T: RawRepresentable>(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 <- <T: RawRepresentable>(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 <- <T: RawRepresentable>(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
*/
Expand Down Expand Up @@ -179,6 +156,110 @@ public func <- <T, Transform: TransformType where Transform.Object == T>(inout l
}
}

/// Array of Basic type with Transform
public func <- <T: TransformType>(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 <- <T: TransformType>(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 <- <T: TransformType>(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 <- <T: TransformType>(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 <- <T: TransformType>(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 <- <T: TransformType>(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<T: TransformType>(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<T: TransformType>(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<T: TransformType>(input: [T.Object]?, transform: T) -> [T.JSON]? {
return input?.filterMap { value in
return transform.transformToJSON(value)
}
}

private func toJSONDictionaryWithTransform<T: TransformType>(input: [String: T.Object]?, transform: T) -> [String: T.JSON]? {
return input?.filterMap { value in
return transform.transformToJSON(value)
}
}

// MARK:- Mappable Objects - <T: Mappable>
/**
* Object conforming to Mappable
Expand Down
23 changes: 0 additions & 23 deletions ObjectMapper/Core/ToJSON.swift
Expand Up @@ -99,29 +99,6 @@ internal final class ToJSON {
}
}

class func rawRepresentableArray<N: RawRepresentable>(field: [N], key: String, inout dictionary: [String : AnyObject]) {
basicType(field.map { e in e.rawValue }, key: key, dictionary: &dictionary)
}

class func rawRepresentableArray<N: RawRepresentable>(field: [N]?, key: String, inout dictionary: [String : AnyObject]) {
if let field = field {
rawRepresentableArray(field, key: key, dictionary: &dictionary)
}
}

class func rawRepresentableDict<N: RawRepresentable>(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<N: RawRepresentable>(field: [String: N]?, key: String, inout dictionary: [String : AnyObject]) {
if let field = field {
rawRepresentableDict(field, key: key, dictionary: &dictionary)
}
}

class func object<N: Mappable>(field: N, key: String, inout dictionary: [String : AnyObject]) {
setValue(Mapper().toJSON(field), forKey: key, dictionary: &dictionary)
}
Expand Down

0 comments on commit 6734361

Please sign in to comment.