Fix attribute serialization tests

This commit is contained in:
Alexander Nozik 2023-02-11 19:09:27 +03:00
parent 2343f37655
commit e80c9406c9
6 changed files with 73 additions and 18 deletions

View File

@ -10,7 +10,7 @@ val kmathVersion: String by extra("0.3.1-dev-10")
allprojects {
group = "center.sciprog"
version = "0.2.2-dev-1"
version = "0.2.2-dev-3"
repositories {
mavenLocal()

View File

@ -70,7 +70,6 @@ public open class MercatorProjection(
)
)
}
}
public companion object {

View File

@ -21,4 +21,8 @@ kscience{
useSerialization{
json()
}
useSerialization(sourceSet = space.kscience.gradle.DependencySourceSet.TEST){
protobuf()
}
}

View File

@ -8,7 +8,9 @@ public interface Attribute<T>
public abstract class SerializableAttribute<T>(
public val serialId: String,
public val serializer: KSerializer<T>,
) : Attribute<T>
) : Attribute<T> {
override fun toString(): String = serialId
}
public interface AttributeWithDefault<T> : Attribute<T> {
public val default: T

View File

@ -34,8 +34,8 @@ public class AttributesSerializer(
override fun serialize(encoder: Encoder, value: Attributes) {
val json = buildJsonObject {
value.content.forEach { (key, value) ->
if (key !in serializableAttributes) error("An attribute key $key is not in the list of allowed attributes for this serializer")
value.content.forEach { (key: Attribute<*>, value: Any) ->
if (key !in serializableAttributes) error("An attribute key '$key' is not in the list of allowed attributes for this serializer")
val serializableKey = key as SerializableAttribute
val json = if (encoder is JsonEncoder) {

View File

@ -1,36 +1,86 @@
package center.sciprog.attributes
import kotlinx.serialization.*
import kotlinx.serialization.json.Json
import kotlinx.serialization.serializer
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.contextual
import kotlinx.serialization.protobuf.ProtoBuf
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals
internal class AttributesSerializationTest {
internal class TestAttributeContainer(val attributes: Attributes)
@Serializable
internal class Container(@Contextual val attributes: Attributes) {
override fun equals(other: Any?): Boolean = (other as? Container)?.attributes?.equals(attributes) ?: false
override fun hashCode(): Int = attributes.hashCode()
// internal object TestContainerAttribute: SerializableAttribute<TestAttributeContainer>("container", se)
override fun toString(): String = attributes.toString()
}
internal object TestAttribute : SerializableAttribute<Map<String, String>>("test", serializer())
internal object ContainerAttribute : SerializableAttribute<Container>("container", serializer()) {
override fun toString(): String = "container"
}
internal object TestAttribute : SerializableAttribute<Map<String, String>>("test", serializer()) {
override fun toString(): String = "test"
}
@Test
fun restore() {
//
// val serializersModule = SerializersModule {
// contextual(AttributesSerializer(setOf()))
// }
val serializer = AttributesSerializer(setOf(NameAttribute, TestAttribute))
fun restoreFromJson() {
val json = Json {
serializersModule = SerializersModule {
contextual(AttributesSerializer(setOf(NameAttribute, TestAttribute, ContainerAttribute)))
}
}
val attributes = Attributes {
NameAttribute("myTest")
TestAttribute(mapOf("a" to "aa", "b" to "bb"))
ContainerAttribute(
Container(
Attributes {
TestAttribute(mapOf("a" to "aa", "b" to "bb"))
}
)
)
}
val serialized = Json.encodeToString(serializer, attributes)
val serialized: String = json.encodeToString(attributes)
println(serialized)
val restored = Json.decodeFromString(serializer, serialized)
val restored: Attributes = json.decodeFromString(serialized)
assertEquals(attributes, restored)
}
@OptIn(ExperimentalSerializationApi::class)
@Test
@Ignore
fun restoreFromProtoBuf() {
val protoBuf = ProtoBuf {
serializersModule = SerializersModule {
contextual(AttributesSerializer(setOf(NameAttribute, TestAttribute, ContainerAttribute)))
}
}
val attributes = Attributes {
NameAttribute("myTest")
TestAttribute(mapOf("a" to "aa", "b" to "bb"))
ContainerAttribute(
Container(
Attributes {
TestAttribute(mapOf("a" to "aa", "b" to "bb"))
}
)
)
}
val serialized = protoBuf.encodeToByteArray(attributes)
val restored: Attributes = protoBuf.decodeFromByteArray(serialized)
assertEquals(attributes, restored)
}