From b781cd51470a66c9b596889cba59d787aab20905 Mon Sep 17 00:00:00 2001 From: Darius Maitia Date: Tue, 3 Sep 2024 11:37:21 -0300 Subject: [PATCH] Replacing ZBytes params for IntoZBytes. --- examples/src/main/kotlin/io.zenoh/ZBytes.kt | 4 +-- .../src/commonMain/kotlin/io/zenoh/Session.kt | 25 ++++++++-------- .../kotlin/io/zenoh/jni/JNIPublisher.kt | 10 +++---- .../kotlin/io/zenoh/jni/JNIQuery.kt | 10 +++---- .../kotlin/io/zenoh/jni/JNISession.kt | 10 +++---- .../io/zenoh/protocol/Deserializable.kt | 4 +-- .../{Serializable.kt => IntoZBytes.kt} | 8 ++--- .../kotlin/io/zenoh/protocol/ZBytes.kt | 30 +++++++++---------- .../kotlin/io/zenoh/publication/Publisher.kt | 8 ++--- .../kotlin/io/zenoh/queryable/Query.kt | 11 +++---- .../commonTest/kotlin/io/zenoh/ZBytesTest.kt | 10 +++---- 11 files changed, 66 insertions(+), 64 deletions(-) rename zenoh-kotlin/src/commonMain/kotlin/io/zenoh/protocol/{Serializable.kt => IntoZBytes.kt} (69%) diff --git a/examples/src/main/kotlin/io.zenoh/ZBytes.kt b/examples/src/main/kotlin/io.zenoh/ZBytes.kt index b5c844e17..6b1c7bd66 100644 --- a/examples/src/main/kotlin/io.zenoh/ZBytes.kt +++ b/examples/src/main/kotlin/io.zenoh/ZBytes.kt @@ -94,7 +94,7 @@ fun main() { *********************************************/ /** - * The examples below use [MyZBytes], an example class consisting that implements the [Serializable] interface. + * The examples below use [MyZBytes], an example class consisting that implements the [IntoZBytes] interface. * * In order for the serialization and deserialization to be successful on a custom class, * the class itself must override the `into(): ZBytes` function, but also the companion @@ -141,7 +141,7 @@ fun main() { check(fooMap == deserializedFooMap) } -class MyZBytes(val content: String) : Serializable, Deserializable { +class MyZBytes(val content: String) : IntoZBytes, Deserializable { override fun into(): ZBytes = content.into() diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Session.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Session.kt index 999d8a76d..b151a9941 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Session.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Session.kt @@ -22,6 +22,7 @@ import io.zenoh.jni.JNISession import io.zenoh.keyexpr.KeyExpr import io.zenoh.prelude.Encoding import io.zenoh.prelude.QoS +import io.zenoh.protocol.IntoZBytes import io.zenoh.protocol.ZBytes import io.zenoh.publication.Delete import io.zenoh.publication.Publisher @@ -481,9 +482,9 @@ class Session private constructor(private val config: Config) : AutoCloseable { fun get( selector: Selector, callback: Callback, - payload: ZBytes? = null, + payload: IntoZBytes? = null, encoding: Encoding? = null, - attachment: ZBytes? = null, + attachment: IntoZBytes? = null, timeout: Duration = Duration.ofMillis(10000), target: QueryTarget = QueryTarget.BEST_MATCHING, consolidation: ConsolidationMode = ConsolidationMode.NONE, @@ -589,9 +590,9 @@ class Session private constructor(private val config: Config) : AutoCloseable { fun get( selector: Selector, handler: Handler, - payload: ZBytes? = null, + payload: IntoZBytes? = null, encoding: Encoding? = null, - attachment: ZBytes? = null, + attachment: IntoZBytes? = null, timeout: Duration = Duration.ofMillis(10000), target: QueryTarget = QueryTarget.BEST_MATCHING, consolidation: ConsolidationMode = ConsolidationMode.NONE, @@ -682,9 +683,9 @@ class Session private constructor(private val config: Config) : AutoCloseable { fun get( selector: Selector, channel: Channel, - payload: ZBytes? = null, + payload: IntoZBytes? = null, encoding: Encoding? = null, - attachment: ZBytes? = null, + attachment: IntoZBytes? = null, timeout: Duration = Duration.ofMillis(10000), target: QueryTarget = QueryTarget.BEST_MATCHING, consolidation: ConsolidationMode = ConsolidationMode.default(), @@ -751,8 +752,8 @@ class Session private constructor(private val config: Config) : AutoCloseable { * @param attachment Optional attachment. * @return A [Result] with the status of the put operation. */ - fun put(keyExpr: KeyExpr, payload: ZBytes, encoding: Encoding? = null, qos: QoS = QoS.default(), attachment: ZBytes? = null): Result { - val put = Put(keyExpr, payload, encoding ?: Encoding.default(), qos, attachment) + fun put(keyExpr: KeyExpr, payload: IntoZBytes, encoding: Encoding? = null, qos: QoS = QoS.default(), attachment: IntoZBytes? = null): Result { + val put = Put(keyExpr, payload.into(), encoding ?: Encoding.default(), qos, attachment?.into()) return resolvePut(keyExpr, put) } @@ -776,8 +777,8 @@ class Session private constructor(private val config: Config) : AutoCloseable { * @param attachment Optional [ZBytes] attachment. * @return a [Result] with the status of the operation. */ - fun delete(keyExpr: KeyExpr, qos: QoS = QoS.default(), attachment: ZBytes? = null): Result { - val delete = Delete(keyExpr, qos, attachment) + fun delete(keyExpr: KeyExpr, qos: QoS = QoS.default(), attachment: IntoZBytes? = null): Result { + val delete = Delete(keyExpr, qos, attachment?.into()) return resolveDelete(keyExpr, delete) } @@ -824,9 +825,9 @@ class Session private constructor(private val config: Config) : AutoCloseable { timeout: Duration, target: QueryTarget, consolidation: ConsolidationMode, - payload: ZBytes?, + payload: IntoZBytes?, encoding: Encoding?, - attachment: ZBytes?, + attachment: IntoZBytes?, ): Result { return jniSession?.run { performGet( diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIPublisher.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIPublisher.kt index 0c7cd3273..d2b1909e4 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIPublisher.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIPublisher.kt @@ -15,7 +15,7 @@ package io.zenoh.jni import io.zenoh.prelude.Encoding -import io.zenoh.protocol.ZBytes +import io.zenoh.protocol.IntoZBytes /** * Adapter class to handle the interactions with Zenoh through JNI for a [io.zenoh.publication.Publisher]. @@ -31,9 +31,9 @@ internal class JNIPublisher(private val ptr: Long) { * @param encoding Encoding of the payload. * @param attachment Optional attachment. */ - fun put(payload: ZBytes, encoding: Encoding?, attachment: ZBytes?): Result = runCatching { + fun put(payload: IntoZBytes, encoding: Encoding?, attachment: IntoZBytes?): Result = runCatching { val resolvedEncoding = encoding ?: Encoding.default() - putViaJNI(payload.bytes, resolvedEncoding.id, resolvedEncoding.schema, attachment?.bytes, ptr) + putViaJNI(payload.into().bytes, resolvedEncoding.id, resolvedEncoding.schema, attachment?.into()?.bytes, ptr) } /** @@ -41,8 +41,8 @@ internal class JNIPublisher(private val ptr: Long) { * * @param attachment Optional attachment. */ - fun delete(attachment: ZBytes?): Result = runCatching { - deleteViaJNI(attachment?.bytes, ptr) + fun delete(attachment: IntoZBytes?): Result = runCatching { + deleteViaJNI(attachment?.into()?.bytes, ptr) } /** diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIQuery.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIQuery.kt index 738da7fd6..4d6f4b80b 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIQuery.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIQuery.kt @@ -17,7 +17,7 @@ package io.zenoh.jni import io.zenoh.keyexpr.KeyExpr import io.zenoh.prelude.Encoding import io.zenoh.prelude.QoS -import io.zenoh.protocol.ZBytes +import io.zenoh.protocol.IntoZBytes import io.zenoh.sample.Sample import org.apache.commons.net.ntp.TimeStamp @@ -48,11 +48,11 @@ internal class JNIQuery(private val ptr: Long) { ) } - fun replyError(error: ZBytes, encoding: Encoding): Result = runCatching { - replyErrorViaJNI(ptr, error.bytes, encoding.id, encoding.schema) + fun replyError(error: IntoZBytes, encoding: Encoding): Result = runCatching { + replyErrorViaJNI(ptr, error.into().bytes, encoding.id, encoding.schema) } - fun replyDelete(keyExpr: KeyExpr, timestamp: TimeStamp?, attachment: ZBytes?, qos: QoS): Result = + fun replyDelete(keyExpr: KeyExpr, timestamp: TimeStamp?, attachment: IntoZBytes?, qos: QoS): Result = runCatching { val timestampEnabled = timestamp != null replyDeleteViaJNI( @@ -61,7 +61,7 @@ internal class JNIQuery(private val ptr: Long) { keyExpr.keyExpr, timestampEnabled, if (timestampEnabled) timestamp!!.ntpValue() else 0, - attachment?.bytes, + attachment?.into()?.bytes, qos.express, qos.priority.value, qos.congestionControl.value diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNISession.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNISession.kt index c8ef7b3c1..df1b9f15d 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNISession.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNISession.kt @@ -24,7 +24,7 @@ import io.zenoh.jni.callbacks.JNIQueryableCallback import io.zenoh.jni.callbacks.JNISubscriberCallback import io.zenoh.keyexpr.KeyExpr import io.zenoh.prelude.* -import io.zenoh.protocol.ZBytes +import io.zenoh.protocol.IntoZBytes import io.zenoh.protocol.ZenohID import io.zenoh.protocol.into import io.zenoh.publication.Delete @@ -137,9 +137,9 @@ internal class JNISession { timeout: Duration, target: QueryTarget, consolidation: ConsolidationMode, - payload: ZBytes?, + payload: IntoZBytes?, encoding: Encoding?, - attachment: ZBytes? + attachment: IntoZBytes? ): Result = runCatching { val getCallback = JNIGetCallback { replierId: String?, @@ -192,8 +192,8 @@ internal class JNISession { timeout.toMillis(), target.ordinal, consolidation.ordinal, - attachment?.bytes, - payload?.bytes, + attachment?.into()?.bytes, + payload?.into()?.bytes, encoding?.id ?: Encoding.default().id, encoding?.schema ) diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/protocol/Deserializable.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/protocol/Deserializable.kt index 9ad142452..4f68b201f 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/protocol/Deserializable.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/protocol/Deserializable.kt @@ -21,6 +21,6 @@ package io.zenoh.protocol */ interface Deserializable { interface From { - fun from(zbytes: ZBytes): Serializable + fun from(zbytes: ZBytes): IntoZBytes } -} \ No newline at end of file +} diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/protocol/Serializable.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/protocol/IntoZBytes.kt similarity index 69% rename from zenoh-kotlin/src/commonMain/kotlin/io/zenoh/protocol/Serializable.kt rename to zenoh-kotlin/src/commonMain/kotlin/io/zenoh/protocol/IntoZBytes.kt index 0c6bd8182..4b8054eec 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/protocol/Serializable.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/protocol/IntoZBytes.kt @@ -1,18 +1,18 @@ package io.zenoh.protocol /** - * Serializable interface. + * IntoZBytes interface. * * Classes implementing this interface can be serialized into a ZBytes object. * * Example: * ```kotlin - * class Foo(val content: String) : Serializable { + * class Foo(val content: String) : IntoZBytes { * * override fun into(): ZBytes = content.into() * } * ``` */ -interface Serializable { +interface IntoZBytes { fun into(): ZBytes -} \ No newline at end of file +} diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/protocol/ZBytes.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/protocol/ZBytes.kt index 1cb2dc75e..ea46a2b4f 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/protocol/ZBytes.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/protocol/ZBytes.kt @@ -66,7 +66,7 @@ import kotlin.reflect.typeOf * - List of [Number] (Byte, Short, Int, Long, Float or Double) * - List of [String] * - List of [ByteArray] - * - List of [Serializable] + * - List of [IntoZBytes] * * The serialize syntax must be used: * ```kotlin @@ -80,7 +80,7 @@ import kotlin.reflect.typeOf * - [Number] * - [String] * - [ByteArray] - * - [Serializable] + * - [IntoZBytes] * * ```kotlin * val myMap: Map = mapOf("foo" to 1, "bar" to 2) @@ -154,13 +154,13 @@ import kotlin.reflect.typeOf * * ## Serialization * - * For custom serialization, classes to be serialized need to implement the [Serializable] interface. + * For custom serialization, classes to be serialized need to implement the [IntoZBytes] interface. * For instance: * * ```kotlin - * class Foo(val content: String) : Serializable { + * class Foo(val content: String) : IntoZBytes { * - * /*Inherits: Serializable*/ + * /*Inherits: IntoZBytes*/ * override fun into(): ZBytes = content.into() * } * ``` @@ -171,7 +171,7 @@ import kotlin.reflect.typeOf * val serialization = ZBytes.serialize(foo).getOrThrow() * ``` * - * Implementing the [Serializable] interface on a class enables the possibility of serializing lists and maps + * Implementing the [IntoZBytes] interface on a class enables the possibility of serializing lists and maps * of that type, for instance: * ```kotlin * val list = listOf(Foo("bar"), Foo("buz"), Foo("fizz")) @@ -185,9 +185,9 @@ import kotlin.reflect.typeOf * `Foo` class (defined in the previous section) implement these interfaces: * * ```kotlin - * class Foo(val content: String) : Serializable, Deserializable { + * class Foo(val content: String) : IntoZBytes, Deserializable { * - * /*Inherits: Serializable*/ + * /*Inherits: IntoZBytes*/ * override fun into(): ZBytes = content.into() * * companion object: Deserializable.From { @@ -221,11 +221,11 @@ import kotlin.reflect.typeOf * functions for deserialization for each of the types in the map. * * For instance, let's stick to the previous implementation of our example Foo class, when it - * only implemented the [Serializable] class: + * only implemented the [IntoZBytes] class: * ```kotlin - * class Foo(val content: String) : Serializable { + * class Foo(val content: String) : IntoZBytes { * - * /*Inherits: Serializable*/ + * /*Inherits: IntoZBytes*/ * override fun into(): ZBytes = content.into() * } * ``` @@ -243,10 +243,10 @@ import kotlin.reflect.typeOf * val deserialization = zbytes.deserialize(mapOf(typeOf() to ::deserializeFoo)).getOrThrow() * ``` */ -class ZBytes internal constructor(internal val bytes: ByteArray) : Serializable { +class ZBytes internal constructor(internal val bytes: ByteArray) : IntoZBytes { companion object { - fun from(serializable: Serializable) = serializable.into() + fun from(intoZBytes: IntoZBytes) = intoZBytes.into() fun from(string: String) = ZBytes(string.toByteArray()) fun from(byteArray: ByteArray) = ZBytes(byteArray) fun from(number: Number): ZBytes { @@ -289,7 +289,7 @@ class ZBytes internal constructor(internal val bytes: ByteArray) : Serializable * - [Number]: Byte, Short, Int, Long, Float, Double * - [String] * - [ByteArray] - * - [Serializable] + * - [IntoZBytes] * - Lists and Maps of the above-mentioned types. * * @see ZBytes @@ -510,7 +510,7 @@ internal fun Any?.into(): ZBytes { is String -> this.into() is Number -> this.into() is ByteArray -> this.into() - is Serializable -> this.into() + is IntoZBytes -> this.into() else -> throw IllegalArgumentException("Unsupported serializable type") } } diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/publication/Publisher.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/publication/Publisher.kt index af73963c0..5746d2fdc 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/publication/Publisher.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/publication/Publisher.kt @@ -20,7 +20,7 @@ import io.zenoh.jni.JNIPublisher import io.zenoh.keyexpr.KeyExpr import io.zenoh.prelude.Encoding import io.zenoh.prelude.QoS -import io.zenoh.protocol.ZBytes +import io.zenoh.protocol.IntoZBytes import io.zenoh.protocol.into /** @@ -77,16 +77,16 @@ class Publisher internal constructor( val express = qos.express /** Performs a PUT operation on the specified [keyExpr] with the specified [payload]. */ - fun put(payload: ZBytes, encoding: Encoding? = null, attachment: ZBytes? = null) = jniPublisher?.put(payload, encoding, attachment) ?: InvalidPublisherResult + fun put(payload: IntoZBytes, encoding: Encoding? = null, attachment: IntoZBytes? = null) = jniPublisher?.put(payload, encoding, attachment) ?: InvalidPublisherResult /** Performs a PUT operation on the specified [keyExpr] with the specified string [message]. */ - fun put(message: String, encoding: Encoding? = null, attachment: ZBytes? = null) = jniPublisher?.put(message.into(), encoding, attachment) ?: InvalidPublisherResult + fun put(message: String, encoding: Encoding? = null, attachment: IntoZBytes? = null) = jniPublisher?.put(message.into(), encoding, attachment) ?: InvalidPublisherResult /** * Performs a DELETE operation on the specified [keyExpr] */ - fun delete(attachment: ZBytes? = null) = jniPublisher?.delete(attachment) ?: InvalidPublisherResult + fun delete(attachment: IntoZBytes? = null) = jniPublisher?.delete(attachment) ?: InvalidPublisherResult fun isValid(): Boolean { return jniPublisher != null diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/queryable/Query.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/queryable/Query.kt index b658e7bc5..1b1b8e8f3 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/queryable/Query.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/queryable/Query.kt @@ -22,6 +22,7 @@ import io.zenoh.keyexpr.KeyExpr import io.zenoh.prelude.Encoding import io.zenoh.prelude.QoS import io.zenoh.prelude.SampleKind +import io.zenoh.protocol.IntoZBytes import io.zenoh.protocol.ZBytes import io.zenoh.sample.Sample import org.apache.commons.net.ntp.TimeStamp @@ -68,13 +69,13 @@ class Query internal constructor( */ fun reply( keyExpr: KeyExpr, - payload: ZBytes, + payload: IntoZBytes, encoding: Encoding = Encoding.default(), qos: QoS = QoS.default(), timestamp: TimeStamp? = null, - attachment: ZBytes? = null + attachment: IntoZBytes? = null ): Result { - val sample = Sample(keyExpr, payload, encoding, SampleKind.PUT, timestamp, qos, attachment) + val sample = Sample(keyExpr, payload.into(), encoding, SampleKind.PUT, timestamp, qos, attachment?.into()) return jniQuery?.let { val result = it.replySuccess(sample) jniQuery = null @@ -91,7 +92,7 @@ class Query internal constructor( * @param error The error information. * @param encoding The encoding of the [error]. */ - fun replyErr(error: ZBytes, encoding: Encoding = Encoding.default()): Result { + fun replyErr(error: IntoZBytes, encoding: Encoding = Encoding.default()): Result { return jniQuery?.let { val result = it.replyError(error, encoding) jniQuery = null @@ -115,7 +116,7 @@ class Query internal constructor( keyExpr: KeyExpr, qos: QoS = QoS.default(), timestamp: TimeStamp? = null, - attachment: ZBytes? = null + attachment: IntoZBytes? = null ): Result { return jniQuery?.let { val result = it.replyDelete(keyExpr, timestamp, attachment, qos) diff --git a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/ZBytesTest.kt b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/ZBytesTest.kt index 6320cd5ce..51bfa7a89 100644 --- a/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/ZBytesTest.kt +++ b/zenoh-kotlin/src/commonTest/kotlin/io/zenoh/ZBytesTest.kt @@ -15,7 +15,7 @@ package io.zenoh import io.zenoh.protocol.Deserializable -import io.zenoh.protocol.Serializable +import io.zenoh.protocol.IntoZBytes import io.zenoh.protocol.ZBytes import io.zenoh.protocol.into import org.junit.jupiter.api.Assertions.assertArrayEquals @@ -179,7 +179,7 @@ class ZBytesTests { MapTestCase(mapOf(byteArrayOf(1, 2, 3) to byteArrayOf(1, 2, 3), byteArrayOf(4, 5, 6) to byteArrayOf(4, 5, 6)), ByteArray::class, ByteArray::class), MapTestCase(mapOf(byteArrayOf(1, 2, 3) to MyZBytes("foo"), byteArrayOf(4, 5, 6) to MyZBytes("bar")), ByteArray::class, MyZBytes::class), - // MyZBytes (Serializable and Deserializable) Keys + // MyZBytes (IntoZBytes and Deserializable) Keys MapTestCase(mapOf(MyZBytes("foo") to "value1", MyZBytes("bar") to "value2"), MyZBytes::class, String::class), MapTestCase(mapOf(MyZBytes("foo") to 1.toByte(), MyZBytes("bar") to 2.toByte()), MyZBytes::class, Byte::class), MapTestCase(mapOf(MyZBytes("foo") to 1.toShort(), MyZBytes("bar") to 2.toShort()), MyZBytes::class, Short::class), @@ -410,9 +410,9 @@ class ZBytesTests { /** * Custom class for the tests. The purpose of this class is to test * the proper functioning of the serialization and deserialization for - * a class implementing the [Serializable] and the [Deserializable] interface. + * a class implementing the [IntoZBytes] and the [Deserializable] interface. */ - class MyZBytes(val content: String) : Serializable, Deserializable { + class MyZBytes(val content: String) : IntoZBytes, Deserializable { override fun into(): ZBytes = content.into() @@ -588,4 +588,4 @@ class ZBytesTests { private fun deserializeIntoListOfPairs(serializedList: ZBytes): List> { return deserializeIntoZBytesMap(serializedList).map { (k, v) -> k to v } } -} \ No newline at end of file +}