@@ -15,35 +15,42 @@ import kotlinx.serialization.json.decodeFromJsonElement
15
15
@Serializable
16
16
data class Column (val name : String , val type : String )
17
17
18
- sealed interface ChannelAction {
18
+ interface HasRecord {
19
19
20
20
/* *
21
- * Contains data of the row's columns
21
+ * The new record, if the action has one
22
22
*/
23
- val columns: List <Column >
23
+ val record: JsonObject
24
+ }
25
+
26
+ interface HasOldRecord {
24
27
25
28
/* *
26
- * The time when the action was committed
29
+ * The old record, if the action has one
27
30
*/
28
- val commitTimestamp: Instant
31
+ val oldRecord: JsonObject
32
+ }
33
+
34
+
35
+ sealed interface ChannelAction {
29
36
30
37
/* *
31
- * The old record, if the action has one
38
+ * Contains data of the row's columns
32
39
*/
33
- val oldRecord : JsonObject ? get() = null
40
+ val columns : List < Column >
34
41
35
42
/* *
36
- * The new record, if the action has one
43
+ * The time when the action was committed
37
44
*/
38
- val record : JsonObject ? get() = null
45
+ val commitTimestamp : Instant
39
46
40
47
@Serializable
41
48
data class Insert (
42
49
override val record : JsonObject ,
43
50
override val columns : List <Column >,
44
51
@SerialName(" commit_timestamp" )
45
- override val commitTimestamp : Instant
46
- ): ChannelAction
52
+ override val commitTimestamp : Instant ,
53
+ ) : ChannelAction, HasRecord
47
54
48
55
@Serializable
49
56
data class Update (
@@ -53,7 +60,7 @@ sealed interface ChannelAction {
53
60
override val columns : List <Column >,
54
61
@SerialName(" commit_timestamp" )
55
62
override val commitTimestamp : Instant ,
56
- ): ChannelAction
63
+ ) : ChannelAction, HasRecord, HasOldRecord
57
64
58
65
@Serializable
59
66
data class Delete (
@@ -62,30 +69,46 @@ sealed interface ChannelAction {
62
69
override val columns : List <Column >,
63
70
@SerialName(" commit_timestamp" )
64
71
override val commitTimestamp : Instant ,
65
- ): ChannelAction
72
+ ) : ChannelAction, HasOldRecord
66
73
67
74
@Serializable
68
75
data class Select (
69
76
override val record : JsonObject ,
70
77
override val columns : List <Column >,
71
78
@SerialName(" commit_timestamp" )
72
79
override val commitTimestamp : Instant ,
73
- ): ChannelAction
80
+ ) : ChannelAction, HasRecord
74
81
75
82
}
76
83
77
84
/* *
78
- * Decodes [ChannelAction .record] as [T] and returns it or null when either [ChannelAction.record] is null or it cannot be decoded as [T]
85
+ * Decodes [HasRecord .record] as [T] and returns it or returns null when it cannot be decoded as [T]
79
86
*/
80
- inline fun <reified T > ChannelAction .decodeRecordOrNull (json : Json = Json ): T ? {
87
+ inline fun <reified T > HasRecord .decodeRecordOrNull (json : Json = Json ): T ? {
81
88
return try {
82
- record? .let { json.decodeFromJsonElement<T >(it) }
89
+ record.let { json.decodeFromJsonElement<T >(it) }
83
90
} catch (e: Exception ) {
84
91
null
85
92
}
86
93
}
87
94
88
95
/* *
89
- * Decodes [ChannelAction.oldRecord] as [T] and returns it
96
+ * Decodes [HasOldRecord.oldRecord] as [T] and returns it or returns null when it cannot be decoded as [T]
97
+ */
98
+ inline fun <reified T > HasOldRecord.decodeOldRecordOrNull (json : Json = Json ): T ? {
99
+ return try {
100
+ oldRecord.let { json.decodeFromJsonElement<T >(it) }
101
+ } catch (e: Exception ) {
102
+ null
103
+ }
104
+ }
105
+
106
+ /* *
107
+ * Decodes [HasRecord.record] as [T] and returns it
108
+ */
109
+ inline fun <reified T > HasRecord.decodeRecord (json : Json = Json ) = json.decodeFromJsonElement<T >(record)
110
+
111
+ /* *
112
+ * Decodes [HasOldRecord.oldRecord] as [T] and returns it
90
113
*/
91
- inline fun <reified T > ChannelAction. decodeRecord (json : Json = Json ) = json.decodeFromJsonElement<T >(record !! )
114
+ inline fun <reified T > HasOldRecord. decodeOldRecord (json : Json = Json ) = json.decodeFromJsonElement<T >(oldRecord )
0 commit comments