This commit is contained in:
InsanusMokrassar 2019-11-20 10:03:16 +06:00
parent 6a6f404381
commit e2d73d59e7
5 changed files with 48 additions and 62 deletions

View File

@ -43,6 +43,12 @@ kotlin {
api "com.benasher44:uuid:$uuidVersion" api "com.benasher44:uuid:$uuidVersion"
} }
} }
commonTest {
dependencies {
implementation kotlin('test-common')
implementation kotlin('test-annotations-common')
}
}
jvmMain { jvmMain {
dependencies { dependencies {
implementation kotlin('stdlib-jdk8') implementation kotlin('stdlib-jdk8')
@ -53,5 +59,10 @@ kotlin {
api "com.benasher44:uuid:$uuidVersion" api "com.benasher44:uuid:$uuidVersion"
} }
} }
jvmTest {
dependencies {
implementation kotlin('test-junit')
}
}
} }
} }

View File

@ -8,69 +8,9 @@ typealias ContentId = String
/** /**
* Content which is planned to be registered in database * Content which is planned to be registered in database
*/ */
@Serializable(ContentSerializer::class) @Serializable
sealed class Content sealed class Content
@Serializer(Content::class)
object ContentSerializer : KSerializer<Content> {
override val descriptor: SerialDescriptor = object : SerialClassDescImpl("com.insanusmokrassar.postssystem.core.content.Content") {
init {
addElement("type") // req will have index 0
addElement("data") // res will have index 1
}
}
override fun serialize(encoder: Encoder, obj: Content) {
encoder.beginCollection(
descriptor,
2
).also {
when (obj) {
is SimpleSpecialContent -> {
it.encodeStringElement(descriptor, 0, SimpleSpecialContent.serializer().descriptor.name)
it.encodeSerializableElement(descriptor, 1, SimpleSpecialContent.serializer(), obj)
}
is SimpleTextContent -> {
it.encodeStringElement(descriptor, 0, SimpleTextContent.serializer().descriptor.name)
it.encodeSerializableElement(descriptor, 1, SimpleTextContent.serializer(), obj)
}
}
}
}
private fun <T> CompositeDecoder.deserialize(i: Int, deserializationStrategy: DeserializationStrategy<T>): T {
return decodeSerializableElement(
descriptor,
i,
deserializationStrategy
)
}
override fun deserialize(decoder: Decoder): Content {
lateinit var result: Content
decoder.beginStructure(descriptor).let {
var type: String? = null
deserializeLoop@while (true) {
when (val i = it.decodeElementIndex(descriptor)) {
CompositeDecoder.READ_DONE -> break@deserializeLoop
0 -> type = it.decodeStringElement(descriptor, i)
1 -> result = when (type) {
SimpleSpecialContent.serializer().descriptor.name -> it.deserialize(
i, SimpleSpecialContent.serializer()
)
SimpleTextContent.serializer().descriptor.name -> it.deserialize(
i, SimpleTextContent.serializer()
)
else -> throw UnsupportedOperationException("Can't decode object with type $type")
}
else -> throw SerializationException("Unknown index $i")
}
}
}
return result
}
}
@Serializable @Serializable
data class SimpleSpecialContent( data class SimpleSpecialContent(
val internalId: ContentId val internalId: ContentId

View File

@ -4,7 +4,7 @@ import com.benasher44.uuid.uuid4
import com.insanusmokrassar.postssystem.core.content.ContentId import com.insanusmokrassar.postssystem.core.content.ContentId
import com.insanusmokrassar.postssystem.core.post.PostId import com.insanusmokrassar.postssystem.core.post.PostId
private fun generateId() = uuid4().toString() fun generateId() = uuid4().toString()
fun generatePostId(): PostId = generateId() fun generatePostId(): PostId = generateId()
fun generateContentId(): ContentId = generateId() fun generateContentId(): ContentId = generateId()

View File

@ -0,0 +1,34 @@
package com.insanusmokrassar.postssystem.core.api
import com.insanusmokrassar.postssystem.core.content.*
import com.insanusmokrassar.postssystem.core.utils.generateContentId
import kotlinx.serialization.json.Json
import kotlin.test.Test
import kotlin.test.assertEquals
class ContentSerialization {
@Test
fun test_that_content_correctly_serializing_and_deserializing() {
val contents = listOf(
SimpleTextContent("Example"),
SimpleSpecialContent("Some internal id")
)
val registeredContentFakes = contents.map { content ->
RegisteredContent(
generateContentId(),
content
)
}
val stringified = registeredContentFakes.map {
Json.plain.stringify(RegisteredContent.serializer(), it)
}
val parsed = stringified.map {
Json.plain.parse(RegisteredContent.serializer(), it)
}
parsed.forEachIndexed { i, registeredContent -> assertEquals(registeredContentFakes[i], registeredContent) }
}
}

View File

@ -30,6 +30,7 @@ dependencies {
implementation project(":postssystem.core") implementation project(":postssystem.core")
api "org.jetbrains.exposed:exposed:$exposed_version" api "org.jetbrains.exposed:exposed:$exposed_version"
testImplementation "org.xerial:sqlite-jdbc:$test_sqlite_version" testImplementation "org.xerial:sqlite-jdbc:$test_sqlite_version"
testImplementation "org.junit.jupiter:junit-jupiter-api:$test_junit_version" testImplementation "org.junit.jupiter:junit-jupiter-api:$test_junit_version"
} }