forked from insidegui/WWDC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTracksJSONAdapter.swift
63 lines (48 loc) · 1.8 KB
/
TracksJSONAdapter.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// TracksJSONAdapter.swift
// WWDC
//
// Created by Guilherme Rambo on 08/02/17.
// Copyright © 2017 Guilherme Rambo. All rights reserved.
//
import Foundation
import SwiftyJSON
private enum TrackKeys: String, JSONSubscriptType {
case name, color, darkColor, titleColor, lightBGColor, ordinal
case identifier = "id"
var jsonKey: JSONKey {
return JSONKey.key(rawValue)
}
}
final class TracksJSONAdapter: Adapter {
typealias InputType = JSON
typealias OutputType = Track
func adapt(_ input: JSON) -> Result<Track, AdapterError> {
guard let identifier = input[TrackKeys.identifier].int else {
return .error(.missingKey(TrackKeys.identifier))
}
guard let name = input[TrackKeys.name].string else {
return .error(.missingKey(TrackKeys.name))
}
guard let color = input[TrackKeys.color].string else {
return .error(.missingKey(TrackKeys.color))
}
guard let darkColor = input[TrackKeys.darkColor].string else {
return .error(.missingKey(TrackKeys.darkColor))
}
guard let titleColor = input[TrackKeys.titleColor].string else {
return .error(.missingKey(TrackKeys.titleColor))
}
guard let lightBGColor = input[TrackKeys.lightBGColor].string else {
return .error(.missingKey(TrackKeys.lightBGColor))
}
let track = Track.make(identifier: "\(identifier)",
name: name,
darkColor: darkColor,
lightBackgroundColor: lightBGColor,
lightColor: color,
titleColor: titleColor)
track.order = input[TrackKeys.ordinal].intValue
return .success(track)
}
}