Skip to content

Commit cf4c9ee

Browse files
committed
Document ImmutableMappable
1 parent 21690f8 commit cf4c9ee

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

README.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ ObjectMapper is a framework written in Swift that makes it easy for you to conve
2525
- Nested Objects (stand alone, in arrays or in dictionaries)
2626
- Custom transformations during mapping
2727
- Struct support
28+
- [Immutable support](#immutablemappable-protocol-beta) (currently in beta)
2829

2930
# The Basics
3031
To support mapping, a class or struct just needs to implement the ```Mappable``` protocol which includes the following functions:
@@ -144,6 +145,127 @@ ObjectMapper uses this function to get objects to use for mapping. Developers sh
144145

145146
If you need to implemented ObjectMapper in an extension, you will need to select this protocol instead of `Mappable`.
146147

148+
## `ImmutableMappable` Protocol (Beta)
149+
150+
> ⚠️ This feature is currently in Beta. There might be breaking API changes in the future.
151+
152+
`ImmutableMappable` provides an availability to map immutable properties. This is how `ImmutableMappable` differs from `Mappable`:
153+
154+
<table>
155+
<tr>
156+
<th>ImmutableMappable</th>
157+
<th>Mappable</th>
158+
</tr>
159+
<tr>
160+
<th colspan="2">Properties</th>
161+
</tr>
162+
<tr>
163+
<td>
164+
<pre>
165+
<strong>let</strong> id: Int
166+
<strong>let</strong> name: String?
167+
</pre>
168+
</td>
169+
<td>
170+
<pre>
171+
var id: Int!
172+
var name: String?
173+
</pre>
174+
</td>
175+
</tr>
176+
<tr>
177+
<th colspan="2">JSON -> Model</th>
178+
</tr>
179+
<tr>
180+
<td>
181+
<pre>
182+
init(map: Map) <strong>throws</strong> {
183+
id = <strong>try</strong> map.value("id")
184+
name = <strong>try?</strong> map.value("name")
185+
}
186+
</pre>
187+
</td>
188+
<td>
189+
<pre>
190+
mutating func mapping(map: Map) {
191+
id <- map["id"]
192+
name <- map["name"]
193+
}
194+
</pre>
195+
</td>
196+
</tr>
197+
<tr>
198+
<th colspan="2">Model -> JSON</th>
199+
</tr>
200+
<tr>
201+
<td>
202+
<pre>
203+
mutating func mapping(map: Map) {
204+
id <strong>>>></strong> map["id"]
205+
name <strong>>>></strong> map["name"]
206+
}
207+
</pre>
208+
</td>
209+
<td>
210+
<pre>
211+
mutating func mapping(map: Map) {
212+
id <- map["id"]
213+
name <- map["name"]
214+
}
215+
</pre>
216+
</td>
217+
</tr>
218+
<tr>
219+
<th colspan="2">Initializing</th>
220+
</tr>
221+
<tr>
222+
<td>
223+
<pre>
224+
<strong>try</strong> User(JSONString: JSONString)
225+
</pre>
226+
</td>
227+
<td>
228+
<pre>
229+
User(JSONString: JSONString)
230+
</pre>
231+
</td>
232+
</tr>
233+
</table>
234+
235+
#### `init(map: Map) throws`
236+
237+
This throwable initializer is used to map immutable properties from the given `Map`. Every immutable properties should be initialized in this initializer.
238+
239+
This initializer throws an error when ...
240+
241+
- ... failed to pop a value from the `Map`
242+
- ... failed to transform a value using `Transform`
243+
244+
`ImmutableMappable` uses `Map.value(_:using:)` method to get values from the `Map`. This method should be used with `try` keyword because it is throwable. `Optional` properties could be easily handled using `try?`.
245+
246+
```swift
247+
init(map: Map) throws {
248+
name = try map.value("name") // throws an error when fails
249+
createdAt = try map.value("createdAt", using: DateTransform()) // throws an error when fails
250+
updatedAt = try? map.value("updatedAt", using: DateTransform()) // optional
251+
posts = (try? map.value("posts")) ?? [] // optional + default value
252+
}
253+
```
254+
255+
#### `mutating func mapping(map: Map)`
256+
257+
This method is where the reverse transform is performed. Since immutable properties are not mapped with `<-` operator, developers have to map reverse transform manually using `>>>` operator.
258+
259+
```swift
260+
mutating func mapping(map: Map) {
261+
name >>> map["name"]
262+
createdAt >>> (map["createdAt"], DateTransform())
263+
updatedAt >>> (map["updatedAt"], DateTransform())
264+
posts >>> map["posts"]
265+
}
266+
```
267+
268+
147269
# Easy Mapping of Nested Objects
148270
ObjectMapper supports dot notation within keys for easy mapping of nested objects. Given the following JSON String:
149271
```json

0 commit comments

Comments
 (0)