-
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
1285cb1
commit 96e5654
Showing
60 changed files
with
181 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile | ||
|
||
@Suppress("DSL_SCOPE_VIOLATION") | ||
plugins { | ||
alias(libs.plugins.kotlin.multiplatform) | ||
alias(libs.plugins.kotlin.serialization) | ||
alias(libs.plugins.kotlinter) | ||
id("maven-publish") | ||
} | ||
group = "com.tap.synk.extension" | ||
version = libs.versions.version.name.get() | ||
|
||
kotlin { | ||
|
||
jvmToolchain { | ||
languageVersion.set(JavaLanguageVersion.of(libs.versions.java.build.version.get().toInt())) | ||
vendor.set(JvmVendorSpec.ADOPTIUM) | ||
} | ||
|
||
targets { | ||
jvm() | ||
} | ||
|
||
sourceSets { | ||
val commonMain by getting { | ||
dependencies { | ||
implementation(projects.synk) | ||
implementation(libs.kotlinx.serialization) | ||
} | ||
} | ||
|
||
val commonTest by getting { | ||
dependencies { | ||
implementation(kotlin("test")) | ||
} | ||
} | ||
} | ||
} | ||
|
||
tasks.withType<KotlinCompile>().configureEach { | ||
kotlinOptions.jvmTarget = libs.versions.java.bytecode.version.get() | ||
} |
41 changes: 41 additions & 0 deletions
41
...on/kotlin-serialization/src/commonMain/kotlin/com/tap/synk/extension/MessageSerializer.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,41 @@ | ||
package com.tap.synk.extension | ||
|
||
import com.tap.synk.meta.Meta | ||
import com.tap.synk.relay.Message | ||
import kotlinx.serialization.KSerializer | ||
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 | ||
import kotlinx.serialization.encoding.decodeStructure | ||
import kotlinx.serialization.encoding.encodeStructure | ||
|
||
class MessageSerializer<T>( | ||
private val innerSerializer: KSerializer<T> | ||
) : KSerializer<Message<T>> { | ||
|
||
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("message") { | ||
element("crdt", innerSerializer.descriptor) | ||
element("meta", MetaSerializer.descriptor) | ||
} | ||
override fun serialize(encoder: Encoder, value: Message<T>) = encoder.encodeStructure(descriptor) { | ||
encodeSerializableElement(descriptor, 0, innerSerializer, value.crdt) | ||
encodeSerializableElement(descriptor, 1, MetaSerializer, value.meta) | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): Message<T> = decoder.decodeStructure(descriptor) { | ||
var crdt : T? = null | ||
var meta : Meta? = null | ||
while (true) { | ||
when (val index = decodeElementIndex(descriptor)) { | ||
0 -> crdt = decodeSerializableElement(descriptor, 0, innerSerializer) | ||
1 -> meta = decodeSerializableElement(descriptor, 1, MetaSerializer) | ||
CompositeDecoder.DECODE_DONE -> break | ||
else -> error("Unexpected index: $index") | ||
} | ||
} | ||
Message(crdt!!, meta!!) | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
...nsion/kotlin-serialization/src/commonMain/kotlin/com/tap/synk/extension/MetaSerializer.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,40 @@ | ||
package com.tap.synk.extension | ||
|
||
import com.tap.synk.meta.Meta | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.builtins.MapSerializer | ||
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 | ||
|
||
object MetaSerializer : KSerializer<Meta> { | ||
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("meta") { | ||
element<String>("clazz") | ||
element<Map<String, String>>("timestamp_meta") | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: Meta) = encoder.encodeStructure(descriptor) { | ||
encodeStringElement(descriptor, 0, value.clazz) | ||
encodeSerializableElement(descriptor, 1, MapSerializer(String.serializer(), String.serializer()), value.timestampMeta) | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): Meta = decoder.decodeStructure(descriptor) { | ||
var clazz = "" | ||
var timestampMeta = emptyMap<String, String>() | ||
while (true) { | ||
when (val index = decodeElementIndex(descriptor)) { | ||
0 -> clazz = decodeStringElement(descriptor, 0) | ||
1 -> timestampMeta = decodeSerializableElement(descriptor, 1, MapSerializer(String.serializer(), String.serializer())) | ||
CompositeDecoder.DECODE_DONE -> break | ||
else -> error("Unexpected index: $index") | ||
} | ||
} | ||
Meta(clazz, timestampMeta) | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
extension/kotlin-serialization/src/commonTest/kotlin/com/tap/synk/extension/Foo.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,6 @@ | ||
package com.tap.synk.extension | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class Foo(val name: String, val age: Int) |
24 changes: 24 additions & 0 deletions
24
...otlin-serialization/src/commonTest/kotlin/com/tap/synk/extension/MessageSerializerTest.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,24 @@ | ||
package com.tap.synk.extension | ||
|
||
import com.tap.synk.meta.Meta | ||
import com.tap.synk.relay.Message | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlinx.serialization.json.Json | ||
|
||
class MessageSerializerTest { | ||
|
||
@Test | ||
fun `serializer correctly serializes and deserializes message`() { | ||
val foo = Foo("bar", 30) | ||
val meta = Meta("foo", mapOf("foo" to "bar")) | ||
val message = Message(foo, meta) | ||
|
||
val serializer = MessageSerializer(Foo.serializer()) | ||
|
||
val json = Json.encodeToString(serializer, message) | ||
val result = Json.decodeFromString(serializer, json) | ||
|
||
assertEquals(message, result) | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...n/kotlin-serialization/src/commonTest/kotlin/com/tap/synk/extension/MetaSerializerTest.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,19 @@ | ||
package com.tap.synk.extension | ||
|
||
|
||
import com.tap.synk.meta.Meta | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlinx.serialization.json.Json | ||
|
||
class MetaSerializerTest { | ||
|
||
@Test | ||
fun `serializer correctly serializes and deserializes meta`() { | ||
val meta = Meta("test", mapOf("foo" to "bar")) | ||
val json = Json.encodeToString(MetaSerializer, meta) | ||
val result = Json.decodeFromString(MetaSerializer, json) | ||
|
||
assertEquals(meta, result) | ||
} | ||
} |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.