mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-14 19:57:04 +05:30
refactor(database): move SavedPostSerializer
to core
This commit is contained in:
parent
dfe95a646e
commit
82ce45b8e4
5 changed files with 7 additions and 3 deletions
|
@ -8,6 +8,7 @@ import dev.msfjarvis.claw.gradle.addTestDependencies
|
|||
|
||||
plugins {
|
||||
id("dev.msfjarvis.claw.kotlin-jvm")
|
||||
alias(libs.plugins.kotlin.serialization)
|
||||
alias(libs.plugins.sqldelight)
|
||||
}
|
||||
|
||||
|
@ -20,3 +21,9 @@ sqldelight {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(libs.kotlinx.serialization.core)
|
||||
testImplementation(libs.kotlinx.serialization.json)
|
||||
addTestDependencies(project)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,97 @@
|
|||
/*
|
||||
* Copyright © 2023 Harsh Shandilya.
|
||||
* Use of this source code is governed by an MIT-style
|
||||
* license that can be found in the LICENSE file or at
|
||||
* https://opensource.org/licenses/MIT.
|
||||
*/
|
||||
package dev.msfjarvis.claw.database
|
||||
|
||||
import dev.msfjarvis.claw.database.local.SavedPost
|
||||
import kotlinx.serialization.ExperimentalSerializationApi
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.builtins.ListSerializer
|
||||
import kotlinx.serialization.builtins.serializer
|
||||
import kotlinx.serialization.descriptors.SerialDescriptor
|
||||
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
|
||||
import kotlinx.serialization.descriptors.element
|
||||
import kotlinx.serialization.encoding.CompositeDecoder
|
||||
import kotlinx.serialization.encoding.Decoder
|
||||
import kotlinx.serialization.encoding.Encoder
|
||||
import kotlinx.serialization.encoding.decodeStructure
|
||||
import kotlinx.serialization.encoding.encodeStructure
|
||||
|
||||
@OptIn(ExperimentalSerializationApi::class)
|
||||
object SavedPostSerializer : KSerializer<SavedPost> {
|
||||
private val delegateSerializer = ListSerializer(String.serializer())
|
||||
override val descriptor: SerialDescriptor =
|
||||
buildClassSerialDescriptor("SavedPost") {
|
||||
element<String>("shortId")
|
||||
element<String>("title")
|
||||
element<String>("url")
|
||||
element<String>("createdAt")
|
||||
element<Int?>("commentCount", isOptional = true)
|
||||
element<String>("commentsUrl")
|
||||
element<String>("submitterName")
|
||||
element<String>("submitterAvatarUrl")
|
||||
element<List<String>>("tags")
|
||||
element<String>("description")
|
||||
}
|
||||
|
||||
override fun deserialize(decoder: Decoder): SavedPost {
|
||||
return decoder.decodeStructure(descriptor) {
|
||||
var shortId = ""
|
||||
var title = ""
|
||||
var url = ""
|
||||
var createdAt = ""
|
||||
var commentCount: Int? = null
|
||||
var commentsUrl = ""
|
||||
var submitterName = ""
|
||||
var submitterAvatarUrl = ""
|
||||
var tags = emptyList<String>()
|
||||
var description = ""
|
||||
while (true) {
|
||||
when (val index = decodeElementIndex(descriptor)) {
|
||||
0 -> shortId = decodeStringElement(descriptor, 0)
|
||||
1 -> title = decodeStringElement(descriptor, 1)
|
||||
2 -> url = decodeStringElement(descriptor, 2)
|
||||
3 -> createdAt = decodeStringElement(descriptor, 3)
|
||||
4 -> commentCount = decodeNullableSerializableElement(descriptor, 4, Int.serializer())
|
||||
5 -> commentsUrl = decodeStringElement(descriptor, 5)
|
||||
6 -> submitterName = decodeStringElement(descriptor, 6)
|
||||
7 -> submitterAvatarUrl = decodeStringElement(descriptor, 7)
|
||||
8 -> tags = decodeSerializableElement(descriptor, 8, delegateSerializer)
|
||||
9 -> description = decodeStringElement(descriptor, 9)
|
||||
CompositeDecoder.DECODE_DONE -> break
|
||||
else -> error("Unexpected index: $index")
|
||||
}
|
||||
}
|
||||
SavedPost(
|
||||
shortId = shortId,
|
||||
title = title,
|
||||
url = url,
|
||||
createdAt = createdAt,
|
||||
commentCount = commentCount,
|
||||
commentsUrl = commentsUrl,
|
||||
submitterName = submitterName,
|
||||
submitterAvatarUrl = submitterAvatarUrl,
|
||||
tags = tags,
|
||||
description = description,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override fun serialize(encoder: Encoder, value: SavedPost) {
|
||||
encoder.encodeStructure(descriptor) {
|
||||
encodeStringElement(descriptor, 0, value.shortId)
|
||||
encodeStringElement(descriptor, 1, value.title)
|
||||
encodeStringElement(descriptor, 2, value.url)
|
||||
encodeStringElement(descriptor, 3, value.createdAt)
|
||||
encodeNullableSerializableElement(descriptor, 4, Int.serializer(), value.commentCount)
|
||||
encodeStringElement(descriptor, 5, value.commentsUrl)
|
||||
encodeStringElement(descriptor, 6, value.submitterName)
|
||||
encodeStringElement(descriptor, 7, value.submitterAvatarUrl)
|
||||
encodeSerializableElement(descriptor, 8, delegateSerializer, value.tags)
|
||||
encodeStringElement(descriptor, 9, value.description)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* Copyright © 2023 Harsh Shandilya.
|
||||
* Use of this source code is governed by an MIT-style
|
||||
* license that can be found in the LICENSE file or at
|
||||
* https://opensource.org/licenses/MIT.
|
||||
*/
|
||||
package dev.msfjarvis.claw.database
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import dev.msfjarvis.claw.database.local.SavedPost
|
||||
import java.io.File
|
||||
import kotlinx.serialization.ExperimentalSerializationApi
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonNamingStrategy
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
@OptIn(ExperimentalSerializationApi::class)
|
||||
class SavedPostSerializerTest {
|
||||
private val json = Json {
|
||||
ignoreUnknownKeys = true
|
||||
namingStrategy = JsonNamingStrategy.SnakeCase
|
||||
}
|
||||
private val text = getJson()
|
||||
|
||||
@Test
|
||||
fun serialize() {
|
||||
val encoded = json.encodeToString(SavedPostSerializer, SAVED_POST)
|
||||
assertThat(encoded).isNotEmpty()
|
||||
assertThat(encoded).isEqualTo(text)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun deserialize() {
|
||||
val decoded = json.decodeFromString(SavedPostSerializer, text)
|
||||
assertThat(decoded).isEqualTo(SAVED_POST)
|
||||
}
|
||||
|
||||
private fun getJson(): String {
|
||||
// Load the JSON response
|
||||
val uri = javaClass.classLoader!!.getResource("saved_post.json")
|
||||
val file = File(uri.path)
|
||||
return String(file.readBytes())
|
||||
}
|
||||
|
||||
private companion object {
|
||||
private val SAVED_POST =
|
||||
SavedPost(
|
||||
title = "Fun Format Friday: You now have a super computer. What next?",
|
||||
shortId = "nbigsf",
|
||||
url = "",
|
||||
createdAt = "2023-05-04T23:43:50.000-05:00",
|
||||
commentCount = 13,
|
||||
commentsUrl = "https://lobste.rs/s/nbigsf/fun_format_friday_you_now_have_super",
|
||||
submitterName = "LenFalken",
|
||||
submitterAvatarUrl = "/avatars/LenFalken-100.png",
|
||||
tags = listOf("ask", "programming"),
|
||||
description =
|
||||
"<p>You suddenly have in your possession a super computer. What comes next? What projects are suddenly possible for you? What performance tests can you now explore?</p>\n",
|
||||
)
|
||||
}
|
||||
}
|
1
database/core/src/test/resources/saved_post.json
Normal file
1
database/core/src/test/resources/saved_post.json
Normal file
|
@ -0,0 +1 @@
|
|||
{"short_id":"nbigsf","title":"Fun Format Friday: You now have a super computer. What next?","url":"","created_at":"2023-05-04T23:43:50.000-05:00","comment_count":13,"comments_url":"https://lobste.rs/s/nbigsf/fun_format_friday_you_now_have_super","submitter_name":"LenFalken","submitter_avatar_url":"/avatars/LenFalken-100.png","tags":["ask","programming"],"description":"<p>You suddenly have in your possession a super computer. What comes next? What projects are suddenly possible for you? What performance tests can you now explore?</p>\n"}
|
Loading…
Add table
Add a link
Reference in a new issue