Skip to content

Commit 28bdf17

Browse files
committed
Just use EnumTransform to implement direct RawRepresentable mapping
1 parent 4ad0225 commit 28bdf17

4 files changed

Lines changed: 7 additions & 50 deletions

File tree

‎ObjectMapper/Core/FromJSON.swift‎

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,6 @@ internal final class FromJSON {
2929
}
3030
}
3131

32-
/// Raw representable
33-
class func rawRepresentable<N: RawRepresentable>(inout field: N, object: N.RawValue?) {
34-
if let value = object, enumValue = N(rawValue: value) {
35-
field = enumValue
36-
}
37-
}
38-
39-
/// Optional raw representable
40-
class func rawRepresentable<N: RawRepresentable>(inout field: N?, object: N.RawValue?) {
41-
if let value = object {
42-
field = N(rawValue: value)
43-
}
44-
}
45-
46-
/// Implicitly unwrapped optional basic type
47-
class func rawRepresentable<N: RawRepresentable>(inout field: N!, object: N.RawValue?) {
48-
if let value = object {
49-
field = N(rawValue: value)
50-
}
51-
}
52-
5332
/// Array of Raw representable
5433
class func rawRepresentableArray<N: RawRepresentable>(inout field: [N], object: [N.RawValue]?) {
5534
if let values = object {

‎ObjectMapper/Core/Operators.swift‎

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,33 +54,21 @@ public func <- <T>(inout left: T!, right: Map) {
5454
* Object of Raw Representable type
5555
*/
5656
public func <- <T: RawRepresentable>(inout left: T, right: Map) {
57-
if right.mappingType == MappingType.FromJSON {
58-
FromJSON.rawRepresentable(&left, object: right.value())
59-
} else {
60-
ToJSON.rawRepresentable(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
61-
}
57+
left <- (right, EnumTransform())
6258
}
6359

6460
/**
6561
* Optional Object of Raw Representable type
6662
*/
6763
public func <- <T: RawRepresentable>(inout left: T?, right: Map) {
68-
if right.mappingType == MappingType.FromJSON {
69-
FromJSON.rawRepresentable(&left, object: right.value())
70-
} else {
71-
ToJSON.rawRepresentable(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
72-
}
64+
left <- (right, EnumTransform())
7365
}
7466

7567
/**
7668
* Implicitly Unwrapped Optional Object of Raw Representable type
7769
*/
7870
public func <- <T: RawRepresentable>(inout left: T!, right: Map) {
79-
if right.mappingType == MappingType.FromJSON {
80-
FromJSON.rawRepresentable(&left, object: right.value())
81-
} else {
82-
ToJSON.rawRepresentable(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
83-
}
71+
left <- (right, EnumTransform())
8472
}
8573

8674
// MARK:- Arrays of Raw Representable type

‎ObjectMapper/Core/ToJSON.swift‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,6 @@ internal final class ToJSON {
9999
}
100100
}
101101

102-
class func rawRepresentable<N: RawRepresentable>(field: N, key: String, inout dictionary: [String : AnyObject]) {
103-
basicType(field.rawValue, key: key, dictionary: &dictionary)
104-
}
105-
106-
class func rawRepresentable<N: RawRepresentable>(field: N?, key: String, inout dictionary: [String : AnyObject]) {
107-
if let field = field {
108-
rawRepresentable(field, key: key, dictionary: &dictionary)
109-
}
110-
}
111-
112102
class func rawRepresentableArray<N: RawRepresentable>(field: [N], key: String, inout dictionary: [String : AnyObject]) {
113103
basicType(field.map { e in e.rawValue }, key: key, dictionary: &dictionary)
114104
}

‎ObjectMapper/Transforms/EnumTransform.swift‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ public class EnumTransform<T: RawRepresentable>: TransformType {
1414

1515
public init() {}
1616

17-
public func transformFromJSON(value: AnyObject?) -> Object? {
17+
public func transformFromJSON(value: AnyObject?) -> T? {
1818
if let raw = value as? T.RawValue {
19-
return T(rawValue: raw) ?? nil
19+
return T(rawValue: raw)
2020
}
2121
return nil
2222
}
2323

24-
public func transformToJSON(value: Object?) -> JSON? {
24+
public func transformToJSON(value: T?) -> T.RawValue? {
2525
if let obj = value {
26-
return obj.rawValue as T.RawValue
26+
return obj.rawValue
2727
}
2828
return nil
2929
}

0 commit comments

Comments
 (0)