Skip to content

Commit 6734361

Browse files
Merge pull request tristanhimmelman#117 from ikesyo/transform-array-dictionary-support
Add array and dictionary support for mapping with transform
2 parents c098352 + 22bb1ab commit 6734361

File tree

3 files changed

+111
-95
lines changed

3 files changed

+111
-95
lines changed

ObjectMapper/Core/FromJSON.swift

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

32-
/// Array of Raw representable
33-
class func rawRepresentableArray<N: RawRepresentable>(inout field: [N], object: [N.RawValue]?) {
34-
if let values = object {
35-
field = values.filterMap { N(rawValue: $0) }
36-
}
37-
}
38-
39-
/// Array of Raw representable
40-
class func rawRepresentableArray<N: RawRepresentable>(inout field: [N]?, object: [N.RawValue]?) {
41-
if let values = object {
42-
field = values.filterMap { N(rawValue: $0) }
43-
}
44-
}
45-
46-
/// Array of Raw representable
47-
class func rawRepresentableArray<N: RawRepresentable>(inout field: [N]!, object: [N.RawValue]?) {
48-
if let values = object {
49-
field = values.filterMap { N(rawValue: $0) }
50-
}
51-
}
52-
53-
/// Dictionary of Raw representable
54-
class func rawRepresentableDict<N: RawRepresentable>(inout field: [String: N], object: [String: N.RawValue]?) {
55-
if let values = object {
56-
field = values.filterMap { N(rawValue: $0) }
57-
}
58-
}
59-
60-
/// Dictionary of Raw representable
61-
class func rawRepresentableDict<N: RawRepresentable>(inout field: [String: N]?, object: [String: N.RawValue]?) {
62-
if let values = object {
63-
field = values.filterMap { N(rawValue: $0) }
64-
}
65-
}
66-
67-
/// Dictionary of Raw representable
68-
class func rawRepresentableDict<N: RawRepresentable>(inout field: [String: N]!, object: [String: N.RawValue]?) {
69-
if let values = object {
70-
field = values.filterMap { N(rawValue: $0) }
71-
}
72-
}
73-
7432
/// Mappable object
7533
class func object<N: Mappable>(inout field: N, object: AnyObject?) {
7634
if let value: N = Mapper().map(object) {

ObjectMapper/Core/Operators.swift

Lines changed: 111 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -76,70 +76,47 @@ public func <- <T: RawRepresentable>(inout left: T!, right: Map) {
7676
* Array of Raw Representable object
7777
*/
7878
public func <- <T: RawRepresentable>(inout left: [T], right: Map) {
79-
if right.mappingType == MappingType.FromJSON {
80-
FromJSON.rawRepresentableArray(&left, object: right.value())
81-
} else {
82-
ToJSON.rawRepresentableArray(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
83-
}
79+
left <- (right, EnumTransform())
8480
}
8581

8682
/**
8783
* Array of Raw Representable object
8884
*/
8985
public func <- <T: RawRepresentable>(inout left: [T]?, right: Map) {
90-
if right.mappingType == MappingType.FromJSON {
91-
FromJSON.rawRepresentableArray(&left, object: right.value())
92-
} else {
93-
ToJSON.rawRepresentableArray(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
94-
}
86+
left <- (right, EnumTransform())
9587
}
9688

9789
/**
9890
* Array of Raw Representable object
9991
*/
10092
public func <- <T: RawRepresentable>(inout left: [T]!, right: Map) {
101-
if right.mappingType == MappingType.FromJSON {
102-
FromJSON.rawRepresentableArray(&left, object: right.value())
103-
} else {
104-
ToJSON.rawRepresentableArray(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
105-
}
93+
left <- (right, EnumTransform())
10694
}
10795

10896
// MARK:- Dictionaries of Raw Representable type
10997
/**
11098
* Dictionary of Raw Representable object
11199
*/
112100
public func <- <T: RawRepresentable>(inout left: [String: T], right: Map) {
113-
if right.mappingType == MappingType.FromJSON {
114-
FromJSON.rawRepresentableDict(&left, object: right.value())
115-
} else {
116-
ToJSON.rawRepresentableDict(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
117-
}
101+
left <- (right, EnumTransform())
118102
}
119103

120104
/**
121105
* Dictionary of Raw Representable object
122106
*/
123107
public func <- <T: RawRepresentable>(inout left: [String: T]?, right: Map) {
124-
if right.mappingType == MappingType.FromJSON {
125-
FromJSON.rawRepresentableDict(&left, object: right.value())
126-
} else {
127-
ToJSON.rawRepresentableDict(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
128-
}
108+
left <- (right, EnumTransform())
129109
}
130110

131111
/**
132112
* Dictionary of Raw Representable object
133113
*/
134114
public func <- <T: RawRepresentable>(inout left: [String: T]!, right: Map) {
135-
if right.mappingType == MappingType.FromJSON {
136-
FromJSON.rawRepresentableDict(&left, object: right.value())
137-
} else {
138-
ToJSON.rawRepresentableDict(left, key: right.currentKey!, dictionary: &right.JSONDictionary)
139-
}
115+
left <- (right, EnumTransform())
140116
}
141117

142118
// MARK:- Transforms
119+
143120
/**
144121
* Object of Basic type with Transform
145122
*/
@@ -179,6 +156,110 @@ public func <- <T, Transform: TransformType where Transform.Object == T>(inout l
179156
}
180157
}
181158

159+
/// Array of Basic type with Transform
160+
public func <- <T: TransformType>(inout left: [T.Object], right: (Map, T)) {
161+
let (map, transform) = right
162+
if map.mappingType == MappingType.FromJSON {
163+
let values = fromJSONArrayWithTransform(map.currentValue, transform)
164+
FromJSON.basicType(&left, object: values)
165+
} else {
166+
let values = toJSONArrayWithTransform(left, transform)
167+
ToJSON.optionalBasicType(values, key: map.currentKey!, dictionary: &map.JSONDictionary)
168+
}
169+
}
170+
171+
/// Optional array of Basic type with Transform
172+
public func <- <T: TransformType>(inout left: [T.Object]?, right: (Map, T)) {
173+
let (map, transform) = right
174+
if map.mappingType == MappingType.FromJSON {
175+
let values = fromJSONArrayWithTransform(map.currentValue, transform)
176+
FromJSON.optionalBasicType(&left, object: values)
177+
} else {
178+
let values = toJSONArrayWithTransform(left, transform)
179+
ToJSON.optionalBasicType(values, key: map.currentKey!, dictionary: &map.JSONDictionary)
180+
}
181+
}
182+
183+
/// Implicitly unwrapped optional array of Basic type with Transform
184+
public func <- <T: TransformType>(inout left: [T.Object]!, right: (Map, T)) {
185+
let (map, transform) = right
186+
if map.mappingType == MappingType.FromJSON {
187+
let values = fromJSONArrayWithTransform(map.currentValue, transform)
188+
FromJSON.optionalBasicType(&left, object: values)
189+
} else {
190+
let values = toJSONArrayWithTransform(left, transform)
191+
ToJSON.optionalBasicType(values, key: map.currentKey!, dictionary: &map.JSONDictionary)
192+
}
193+
}
194+
195+
/// Dictionary of Basic type with Transform
196+
public func <- <T: TransformType>(inout left: [String: T.Object], right: (Map, T)) {
197+
let (map, transform) = right
198+
if map.mappingType == MappingType.FromJSON {
199+
let values = fromJSONDictionaryWithTransform(map.currentValue, transform)
200+
FromJSON.basicType(&left, object: values)
201+
} else {
202+
let values = toJSONDictionaryWithTransform(left, transform)
203+
ToJSON.optionalBasicType(values, key: map.currentKey!, dictionary: &map.JSONDictionary)
204+
}
205+
}
206+
207+
/// Optional dictionary of Basic type with Transform
208+
public func <- <T: TransformType>(inout left: [String: T.Object]?, right: (Map, T)) {
209+
let (map, transform) = right
210+
if map.mappingType == MappingType.FromJSON {
211+
let values = fromJSONDictionaryWithTransform(map.currentValue, transform)
212+
FromJSON.optionalBasicType(&left, object: values)
213+
} else {
214+
let values = toJSONDictionaryWithTransform(left, transform)
215+
ToJSON.optionalBasicType(values, key: map.currentKey!, dictionary: &map.JSONDictionary)
216+
}
217+
}
218+
219+
/// Implicitly unwrapped optional dictionary of Basic type with Transform
220+
public func <- <T: TransformType>(inout left: [String: T.Object]!, right: (Map, T)) {
221+
let (map, transform) = right
222+
if map.mappingType == MappingType.FromJSON {
223+
let values = fromJSONDictionaryWithTransform(map.currentValue, transform)
224+
FromJSON.optionalBasicType(&left, object: values)
225+
} else {
226+
let values = toJSONDictionaryWithTransform(left, transform)
227+
ToJSON.optionalBasicType(values, key: map.currentKey!, dictionary: &map.JSONDictionary)
228+
}
229+
}
230+
231+
private func fromJSONArrayWithTransform<T: TransformType>(input: AnyObject?, transform: T) -> [T.Object] {
232+
if let values = input as? [AnyObject] {
233+
return values.filterMap { value in
234+
return transform.transformFromJSON(value)
235+
}
236+
} else {
237+
return []
238+
}
239+
}
240+
241+
private func fromJSONDictionaryWithTransform<T: TransformType>(input: AnyObject?, transform: T) -> [String: T.Object] {
242+
if let values = input as? [String: AnyObject] {
243+
return values.filterMap { value in
244+
return transform.transformFromJSON(value)
245+
}
246+
} else {
247+
return [:]
248+
}
249+
}
250+
251+
private func toJSONArrayWithTransform<T: TransformType>(input: [T.Object]?, transform: T) -> [T.JSON]? {
252+
return input?.filterMap { value in
253+
return transform.transformToJSON(value)
254+
}
255+
}
256+
257+
private func toJSONDictionaryWithTransform<T: TransformType>(input: [String: T.Object]?, transform: T) -> [String: T.JSON]? {
258+
return input?.filterMap { value in
259+
return transform.transformToJSON(value)
260+
}
261+
}
262+
182263
// MARK:- Mappable Objects - <T: Mappable>
183264
/**
184265
* Object conforming to Mappable

ObjectMapper/Core/ToJSON.swift

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

102-
class func rawRepresentableArray<N: RawRepresentable>(field: [N], key: String, inout dictionary: [String : AnyObject]) {
103-
basicType(field.map { e in e.rawValue }, key: key, dictionary: &dictionary)
104-
}
105-
106-
class func rawRepresentableArray<N: RawRepresentable>(field: [N]?, key: String, inout dictionary: [String : AnyObject]) {
107-
if let field = field {
108-
rawRepresentableArray(field, key: key, dictionary: &dictionary)
109-
}
110-
}
111-
112-
class func rawRepresentableDict<N: RawRepresentable>(field: [String: N], key: String, inout dictionary: [String : AnyObject]) {
113-
let raw: [String: N.RawValue] = field.map { key, value in
114-
return (key, value.rawValue)
115-
}
116-
basicType(raw, key: key, dictionary: &dictionary)
117-
}
118-
119-
class func rawRepresentableDict<N: RawRepresentable>(field: [String: N]?, key: String, inout dictionary: [String : AnyObject]) {
120-
if let field = field {
121-
rawRepresentableDict(field, key: key, dictionary: &dictionary)
122-
}
123-
}
124-
125102
class func object<N: Mappable>(field: N, key: String, inout dictionary: [String : AnyObject]) {
126103
setValue(Mapper().toJSON(field), forKey: key, dictionary: &dictionary)
127104
}

0 commit comments

Comments
 (0)