Skip to content

Commit

Permalink
Refactor RawRepresentable mapping using EnumTransform
Browse files Browse the repository at this point in the history
  • Loading branch information
ikesyo committed Apr 30, 2015
1 parent 465ddab commit 22bb1ab
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 95 deletions.
42 changes: 0 additions & 42 deletions ObjectMapper/Core/FromJSON.swift
Original file line number Diff line number Diff line change
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
36 changes: 6 additions & 30 deletions ObjectMapper/Core/Operators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,67 +76,43 @@ 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
Expand Down
23 changes: 0 additions & 23 deletions ObjectMapper/Core/ToJSON.swift
Original file line number Diff line number Diff line change
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 22bb1ab

Please sign in to comment.