Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing ZBytes params for IntoZBytes. #192

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/src/main/kotlin/io.zenoh/ZBytes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down
25 changes: 13 additions & 12 deletions zenoh-kotlin/src/commonMain/kotlin/io/zenoh/Session.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -481,9 +482,9 @@ class Session private constructor(private val config: Config) : AutoCloseable {
fun get(
selector: Selector,
callback: Callback<Reply>,
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,
Expand Down Expand Up @@ -589,9 +590,9 @@ class Session private constructor(private val config: Config) : AutoCloseable {
fun <R> get(
selector: Selector,
handler: Handler<Reply, R>,
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,
Expand Down Expand Up @@ -682,9 +683,9 @@ class Session private constructor(private val config: Config) : AutoCloseable {
fun get(
selector: Selector,
channel: Channel<Reply>,
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(),
Expand Down Expand Up @@ -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<Unit> {
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<Unit> {
val put = Put(keyExpr, payload.into(), encoding ?: Encoding.default(), qos, attachment?.into())
return resolvePut(keyExpr, put)
}

Expand All @@ -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<Unit> {
val delete = Delete(keyExpr, qos, attachment)
fun delete(keyExpr: KeyExpr, qos: QoS = QoS.default(), attachment: IntoZBytes? = null): Result<Unit> {
val delete = Delete(keyExpr, qos, attachment?.into())
return resolveDelete(keyExpr, delete)
}

Expand Down Expand Up @@ -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<R> {
return jniSession?.run {
performGet(
Expand Down
10 changes: 5 additions & 5 deletions zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIPublisher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand All @@ -31,18 +31,18 @@ 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<Unit> = runCatching {
fun put(payload: IntoZBytes, encoding: Encoding?, attachment: IntoZBytes?): Result<Unit> = 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)
}

/**
* Delete operation.
*
* @param attachment Optional attachment.
*/
fun delete(attachment: ZBytes?): Result<Unit> = runCatching {
deleteViaJNI(attachment?.bytes, ptr)
fun delete(attachment: IntoZBytes?): Result<Unit> = runCatching {
deleteViaJNI(attachment?.into()?.bytes, ptr)
}

/**
Expand Down
10 changes: 5 additions & 5 deletions zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIQuery.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -48,11 +48,11 @@ internal class JNIQuery(private val ptr: Long) {
)
}

fun replyError(error: ZBytes, encoding: Encoding): Result<Unit> = runCatching {
replyErrorViaJNI(ptr, error.bytes, encoding.id, encoding.schema)
fun replyError(error: IntoZBytes, encoding: Encoding): Result<Unit> = runCatching {
replyErrorViaJNI(ptr, error.into().bytes, encoding.id, encoding.schema)
}

fun replyDelete(keyExpr: KeyExpr, timestamp: TimeStamp?, attachment: ZBytes?, qos: QoS): Result<Unit> =
fun replyDelete(keyExpr: KeyExpr, timestamp: TimeStamp?, attachment: IntoZBytes?, qos: QoS): Result<Unit> =
runCatching {
val timestampEnabled = timestamp != null
replyDeleteViaJNI(
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNISession.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -137,9 +137,9 @@ internal class JNISession {
timeout: Duration,
target: QueryTarget,
consolidation: ConsolidationMode,
payload: ZBytes?,
payload: IntoZBytes?,
encoding: Encoding?,
attachment: ZBytes?
attachment: IntoZBytes?
): Result<R> = runCatching {
val getCallback = JNIGetCallback {
replierId: String?,
Expand Down Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ package io.zenoh.protocol
*/
interface Deserializable {
interface From {
fun from(zbytes: ZBytes): Serializable
fun from(zbytes: ZBytes): IntoZBytes
}
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
30 changes: 15 additions & 15 deletions zenoh-kotlin/src/commonMain/kotlin/io/zenoh/protocol/ZBytes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -80,7 +80,7 @@ import kotlin.reflect.typeOf
* - [Number]
* - [String]
* - [ByteArray]
* - [Serializable]
* - [IntoZBytes]
*
* ```kotlin
* val myMap: Map<String, Int> = mapOf("foo" to 1, "bar" to 2)
Expand Down Expand Up @@ -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()
* }
* ```
Expand All @@ -171,7 +171,7 @@ import kotlin.reflect.typeOf
* val serialization = ZBytes.serialize<Foo>(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"))
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
* }
* ```
Expand All @@ -243,10 +243,10 @@ import kotlin.reflect.typeOf
* val deserialization = zbytes.deserialize<Foo>(mapOf(typeOf<Foo>() 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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading