-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathRow.swift
More file actions
113 lines (96 loc) · 3.71 KB
/
Row.swift
File metadata and controls
113 lines (96 loc) · 3.71 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import Foundation
import JSONCodable
/// Row
open class Row<T : Codable>: Codable {
enum CodingKeys: String, CodingKey {
case id = "$id"
case sequence = "$sequence"
case tableId = "$tableId"
case databaseId = "$databaseId"
case createdAt = "$createdAt"
case updatedAt = "$updatedAt"
case permissions = "$permissions"
case data
}
/// Row ID.
public let id: String
/// Row automatically incrementing ID.
public let sequence: Int
/// Table ID.
public let tableId: String
/// Database ID.
public let databaseId: String
/// Row creation date in ISO 8601 format.
public let createdAt: String
/// Row update date in ISO 8601 format.
public let updatedAt: String
/// Row permissions. [Learn more about permissions](https://appwrite.io/docs/permissions).
public let permissions: [String]
/// Additional properties
public let data: T
init(
id: String,
sequence: Int,
tableId: String,
databaseId: String,
createdAt: String,
updatedAt: String,
permissions: [String],
data: T
) {
self.id = id
self.sequence = sequence
self.tableId = tableId
self.databaseId = databaseId
self.createdAt = createdAt
self.updatedAt = updatedAt
self.permissions = permissions
self.data = data
}
public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.id = try container.decode(String.self, forKey: .id)
self.sequence = try container.decode(Int.self, forKey: .sequence)
self.tableId = try container.decode(String.self, forKey: .tableId)
self.databaseId = try container.decode(String.self, forKey: .databaseId)
self.createdAt = try container.decode(String.self, forKey: .createdAt)
self.updatedAt = try container.decode(String.self, forKey: .updatedAt)
self.permissions = try container.decode([String].self, forKey: .permissions)
self.data = try container.decode(T.self, forKey: .data)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)
try container.encode(sequence, forKey: .sequence)
try container.encode(tableId, forKey: .tableId)
try container.encode(databaseId, forKey: .databaseId)
try container.encode(createdAt, forKey: .createdAt)
try container.encode(updatedAt, forKey: .updatedAt)
try container.encode(permissions, forKey: .permissions)
try container.encode(data, forKey: .data)
}
public func toMap() -> [String: Any] {
return [
"$id": id as Any,
"$sequence": sequence as Any,
"$tableId": tableId as Any,
"$databaseId": databaseId as Any,
"$createdAt": createdAt as Any,
"$updatedAt": updatedAt as Any,
"$permissions": permissions as Any,
"data": try! JSONEncoder().encode(data)
]
}
public static func from(map: [String: Any] ) -> Row {
return Row(
id: map["$id"] as! String,
sequence: map["$sequence"] as! Int,
tableId: map["$tableId"] as! String,
databaseId: map["$databaseId"] as! String,
createdAt: map["$createdAt"] as! String,
updatedAt: map["$updatedAt"] as! String,
permissions: map["$permissions"] as! [String],
data: try! JSONDecoder().decode(T.self, from: JSONSerialization.data(withJSONObject: map["data"] as? [String: Any] ?? map, options: []))
)
}
}