-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathRow.kt
More file actions
105 lines (94 loc) · 2.52 KB
/
Row.kt
File metadata and controls
105 lines (94 loc) · 2.52 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
package io.appwrite.models
import com.google.gson.annotations.SerializedName
import io.appwrite.extensions.jsonCast
/**
* Row
*/
data class Row<T>(
/**
* Row ID.
*/
@SerializedName("\$id")
val id: String,
/**
* Row sequence ID.
*/
@SerializedName("\$sequence")
val sequence: String,
/**
* Table ID.
*/
@SerializedName("\$tableId")
val tableId: String,
/**
* Database ID.
*/
@SerializedName("\$databaseId")
val databaseId: String,
/**
* Row creation date in ISO 8601 format.
*/
@SerializedName("\$createdAt")
val createdAt: String,
/**
* Row update date in ISO 8601 format.
*/
@SerializedName("\$updatedAt")
val updatedAt: String,
/**
* Row permissions. [Learn more about permissions](https://appwrite.io/docs/permissions).
*/
@SerializedName("\$permissions")
val permissions: List<String>,
/**
* Additional properties
*/
@SerializedName("data")
val data: T
) {
fun toMap(): Map<String, Any> = mapOf(
"\$id" to id as Any,
"\$sequence" to sequence as Any,
"\$tableId" to tableId as Any,
"\$databaseId" to databaseId as Any,
"\$createdAt" to createdAt as Any,
"\$updatedAt" to updatedAt as Any,
"\$permissions" to permissions as Any,
"data" to data!!.jsonCast(to = Map::class.java)
)
companion object {
operator fun invoke(
id: String,
sequence: String,
tableId: String,
databaseId: String,
createdAt: String,
updatedAt: String,
permissions: List<String>,
data: Map<String, Any>
) = Row<Map<String, Any>>(
id,
sequence,
tableId,
databaseId,
createdAt,
updatedAt,
permissions,
data
)
@Suppress("UNCHECKED_CAST")
fun <T> from(
map: Map<String, Any>,
nestedType: Class<T>
) = Row<T>(
id = map["\$id"] as String,
sequence = map["\$sequence"] as String,
tableId = map["\$tableId"] as String,
databaseId = map["\$databaseId"] as String,
createdAt = map["\$createdAt"] as String,
updatedAt = map["\$updatedAt"] as String,
permissions = map["\$permissions"] as List<String>,
data = map["data"]?.jsonCast(to = nestedType) ?: map.jsonCast(to = nestedType)
)
}
}