-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a9a9dbc
commit 1b01490
Showing
9 changed files
with
149 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 20 additions & 4 deletions
24
src/commonMain/kotlin/com/petersamokhin/notionsdk/data/mapper/DatabaseResultMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,28 @@ | ||
package com.petersamokhin.notionsdk.data.mapper | ||
|
||
import com.petersamokhin.notionsdk.data.model.internal.obj.Block | ||
import com.petersamokhin.notionsdk.data.model.internal.response.PageObject | ||
import com.petersamokhin.notionsdk.data.model.internal.response.ResultsResponse | ||
import com.petersamokhin.notionsdk.data.model.result.NotionDatabase | ||
import com.petersamokhin.notionsdk.data.model.result.NotionBlock | ||
import com.petersamokhin.notionsdk.data.model.result.NotionDatabaseRow | ||
import com.petersamokhin.notionsdk.data.model.result.NotionResults | ||
|
||
internal fun ResultsResponse<PageObject>.toDomain(): NotionDatabase = | ||
NotionDatabase( | ||
rows = results.map(PageObject::toDomain), | ||
@Suppress("UNCHECKED_CAST") | ||
internal inline fun <reified T : Any, reified R : Any> ResultsResponse<T>.toDomain(): NotionResults<R> = | ||
NotionResults( | ||
results = ( | ||
when (T::class) { | ||
PageObject::class -> when (R::class) { | ||
NotionDatabaseRow::class -> results.filterIsInstance<PageObject>().map(PageObject::toDomain) | ||
else -> null | ||
} | ||
Block::class -> when (R::class) { | ||
NotionBlock::class -> results.filterIsInstance<Block>().map(Block::toDomain) | ||
else -> null | ||
} | ||
else -> error("${T::class} results response domain mapping is not supported") | ||
} ?: error("${T::class} -> ${R::class} results response domain mapping is not supported") | ||
) as List<R>, | ||
nextCursor = nextCursor, | ||
hasMore = hasMore, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
.../kotlin/com/petersamokhin/notionsdk/data/model/serializer/NotionResultsTypedSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.petersamokhin.notionsdk.data.model.serializer | ||
|
||
import com.petersamokhin.notionsdk.data.model.result.NotionResults | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.SerializationException | ||
import kotlinx.serialization.Serializer | ||
import kotlinx.serialization.builtins.ListSerializer | ||
import kotlinx.serialization.builtins.nullable | ||
import kotlinx.serialization.builtins.serializer | ||
import kotlinx.serialization.descriptors.PrimitiveKind | ||
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.descriptors.buildClassSerialDescriptor | ||
import kotlinx.serialization.encoding.CompositeDecoder | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
@Serializer(forClass = NotionResults::class) | ||
internal class NotionResultsTypedSerializer<T : Any>( | ||
resultsItemSerializer: KSerializer<T>, | ||
) : KSerializer<NotionResults<T>> { | ||
private val resultsSerializer: KSerializer<List<T>> = ListSerializer(resultsItemSerializer) | ||
private val nextCursorSerializer: KSerializer<String?> = String.serializer().nullable | ||
|
||
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("ResultsResponseTypedSerializer") { | ||
element("results", resultsSerializer.descriptor) | ||
element("next_cursor", PrimitiveSerialDescriptor("next_cursor", PrimitiveKind.STRING)) | ||
element("has_more", PrimitiveSerialDescriptor("has_more", PrimitiveKind.BOOLEAN)) | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): NotionResults<T> { | ||
val inp = decoder.beginStructure(descriptor) | ||
var results: List<T>? = null | ||
var nextCursor: String? = null | ||
var hasMore: Boolean? = null | ||
loop@ while (true) { | ||
when (val i = inp.decodeElementIndex(descriptor)) { | ||
CompositeDecoder.DECODE_DONE -> break@loop | ||
0 -> results = inp.decodeSerializableElement(descriptor, i, resultsSerializer) | ||
1 -> nextCursor = inp.decodeNullableSerializableElement(descriptor, i, nextCursorSerializer) | ||
2 -> hasMore = inp.decodeBooleanElement(descriptor, i) | ||
else -> throw SerializationException("Unknown index $i") | ||
} | ||
} | ||
inp.endStructure(descriptor) | ||
return NotionResults( | ||
results = results ?: throw SerializationException("required field 'results' is null"), | ||
nextCursor = nextCursor, | ||
hasMore = hasMore ?: throw SerializationException("required field 'has_more' is null"), | ||
) | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: NotionResults<T>) { | ||
encoder.beginStructure(descriptor).apply { | ||
encodeNullableSerializableElement(descriptor, 0, resultsSerializer, value.results) | ||
encodeNullableSerializableElement(descriptor, 1, nextCursorSerializer, value.nextCursor) | ||
encodeBooleanElement(descriptor, 2, value.hasMore) | ||
endStructure(descriptor) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters