Skip to content

Commit 088be44

Browse files
authored
Merge pull request #3 from brezinajn/master
Fix decodeRecord NPE
2 parents 7e848eb + 9338772 commit 088be44

File tree

1 file changed

+42
-19
lines changed
  • Realtime/src/commonMain/kotlin/io/github/jan/supacompose/realtime/events

1 file changed

+42
-19
lines changed

Diff for: Realtime/src/commonMain/kotlin/io/github/jan/supacompose/realtime/events/ChannelAction.kt

+42-19
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,42 @@ import kotlinx.serialization.json.decodeFromJsonElement
1515
@Serializable
1616
data class Column(val name: String, val type: String)
1717

18-
sealed interface ChannelAction {
18+
interface HasRecord {
1919

2020
/**
21-
* Contains data of the row's columns
21+
* The new record, if the action has one
2222
*/
23-
val columns: List<Column>
23+
val record: JsonObject
24+
}
25+
26+
interface HasOldRecord {
2427

2528
/**
26-
* The time when the action was committed
29+
* The old record, if the action has one
2730
*/
28-
val commitTimestamp: Instant
31+
val oldRecord: JsonObject
32+
}
33+
34+
35+
sealed interface ChannelAction {
2936

3037
/**
31-
* The old record, if the action has one
38+
* Contains data of the row's columns
3239
*/
33-
val oldRecord: JsonObject? get() = null
40+
val columns: List<Column>
3441

3542
/**
36-
* The new record, if the action has one
43+
* The time when the action was committed
3744
*/
38-
val record: JsonObject? get() = null
45+
val commitTimestamp: Instant
3946

4047
@Serializable
4148
data class Insert(
4249
override val record: JsonObject,
4350
override val columns: List<Column>,
4451
@SerialName("commit_timestamp")
45-
override val commitTimestamp: Instant
46-
): ChannelAction
52+
override val commitTimestamp: Instant,
53+
) : ChannelAction, HasRecord
4754

4855
@Serializable
4956
data class Update(
@@ -53,7 +60,7 @@ sealed interface ChannelAction {
5360
override val columns: List<Column>,
5461
@SerialName("commit_timestamp")
5562
override val commitTimestamp: Instant,
56-
): ChannelAction
63+
) : ChannelAction, HasRecord, HasOldRecord
5764

5865
@Serializable
5966
data class Delete(
@@ -62,30 +69,46 @@ sealed interface ChannelAction {
6269
override val columns: List<Column>,
6370
@SerialName("commit_timestamp")
6471
override val commitTimestamp: Instant,
65-
): ChannelAction
72+
) : ChannelAction, HasOldRecord
6673

6774
@Serializable
6875
data class Select(
6976
override val record: JsonObject,
7077
override val columns: List<Column>,
7178
@SerialName("commit_timestamp")
7279
override val commitTimestamp: Instant,
73-
): ChannelAction
80+
) : ChannelAction, HasRecord
7481

7582
}
7683

7784
/**
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]
7986
*/
80-
inline fun <reified T> ChannelAction.decodeRecordOrNull(json: Json = Json): T? {
87+
inline fun <reified T> HasRecord.decodeRecordOrNull(json: Json = Json): T? {
8188
return try {
82-
record?.let { json.decodeFromJsonElement<T>(it) }
89+
record.let { json.decodeFromJsonElement<T>(it) }
8390
} catch (e: Exception) {
8491
null
8592
}
8693
}
8794

8895
/**
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
90113
*/
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

Comments
 (0)