-
Notifications
You must be signed in to change notification settings - Fork 1
/
Item.swift
69 lines (57 loc) · 1.45 KB
/
Item.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
64
65
66
67
68
69
import Argo
import Curry
import Foundation
import Ogra
/// An item.
public struct Item {
public var content: [Block]
public let id: NSUUID
public var metadata: Metadata?
public var score: Int?
public var sites: [String]
public init(
content: [Block],
id: NSUUID,
metadata: Metadata? = .None,
score: Int? = .None,
sites: [String] = []
) {
self.content = content
self.id = id
self.metadata = metadata
self.score = score
self.sites = sites
}
}
// MARK: - Decodable
extension Item: Decodable {
public static func decode(json: JSON) -> Decoded<Item> {
return curry(self.init)
<^> json <|| "content"
<*> json <| "id"
<*> json <|? "metadata"
<*> json <|? "score"
<*> json <|| "sites" <|> pure([])
}
}
// MARK: - Encodable
extension Item: Encodable {
public func encode() -> JSON {
return .Object([
"content": content.encode(),
"id": id.encode(),
"metadata": metadata.encode(),
"score": score.encode(),
"sites": sites.encode()
])
}
}
// MARK: - Equatable
extension Item: Equatable {}
public func == (lhs: Item, rhs: Item) -> Bool {
return lhs.content == rhs.content
&& lhs.id == rhs.id
&& lhs.metadata == rhs.metadata
&& lhs.score == rhs.score
&& lhs.sites == rhs.sites
}