From 585acf3c1b2a94b3e965f06951971de8187eb4f4 Mon Sep 17 00:00:00 2001 From: Darius Maitia Date: Thu, 31 Oct 2024 06:43:19 -0300 Subject: [PATCH] Selector: Adding missing static 'tryFrom' method (#283) --- .../kotlin/io/zenoh/query/IntoSelector.kt | 29 --------------- .../kotlin/io/zenoh/query/Selector.kt | 35 +++++++++++++++++++ 2 files changed, 35 insertions(+), 29 deletions(-) delete mode 100644 zenoh-kotlin/src/commonMain/kotlin/io/zenoh/query/IntoSelector.kt diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/query/IntoSelector.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/query/IntoSelector.kt deleted file mode 100644 index 37a34657..00000000 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/query/IntoSelector.kt +++ /dev/null @@ -1,29 +0,0 @@ -// -// Copyright (c) 2023 ZettaScale Technology -// -// This program and the accompanying materials are made available under the -// terms of the Eclipse Public License 2.0 which is available at -// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 -// which is available at https://www.apache.org/licenses/LICENSE-2.0. -// -// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 -// -// Contributors: -// ZettaScale Zenoh Team, -// - -package io.zenoh.query - -import io.zenoh.exceptions.ZError -import io.zenoh.keyexpr.KeyExpr - -fun String.intoSelector(): Result = runCatching { - if (this.isEmpty()) { - throw ZError("Attempting to create a KeyExpr from an empty string.") - } - val result = this.split('?', limit = 2) - val keyExpr = KeyExpr.autocanonize(result[0]).getOrThrow() - val params = if (result.size == 2) Parameters.from(result[1]).getOrThrow() else null - - Selector(keyExpr, params) -} diff --git a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/query/Selector.kt b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/query/Selector.kt index c8624e37..900a0a14 100644 --- a/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/query/Selector.kt +++ b/zenoh-kotlin/src/commonMain/kotlin/io/zenoh/query/Selector.kt @@ -14,6 +14,7 @@ package io.zenoh.query +import io.zenoh.exceptions.ZError import io.zenoh.keyexpr.KeyExpr /** @@ -50,6 +51,38 @@ import io.zenoh.keyexpr.KeyExpr */ data class Selector(val keyExpr: KeyExpr, val parameters: Parameters? = null) : AutoCloseable { + companion object { + + /** + * Try from. + * + * The default way to construct a Selector. + * + * When in string form, selectors look a lot like a URI, with similar semantics: + * - the `key_expr` before the first `?` must be a valid key expression. + * - the `parameters` after the first `?` should be encoded like the query section of a URL: + * - parameters are separated by `;`, + * - the parameter name and value are separated by the first `=`, + * - in the absence of `=`, the parameter value is considered to be the empty string, + * - both name and value should use percent-encoding to escape characters, + * - defining a value for the same parameter name twice is considered undefined behavior, + * with the encouraged behaviour being to reject operations when a duplicate parameter is detected. + * + * @param selector The selector expression as a String. + * @return a Result with the constructed Selector. + */ + fun tryFrom(selector: String): Result = runCatching { + if (selector.isEmpty()) { + throw ZError("Attempting to create a selector from an empty string.") + } + val result = selector.split('?', limit = 2) + val keyExpr = KeyExpr.autocanonize(result[0]).getOrThrow() + val params = if (result.size == 2) Parameters.from(result[1]).getOrThrow() else null + + Selector(keyExpr, params) + } + } + override fun toString(): String { return parameters?.let { "$keyExpr?$parameters" } ?: keyExpr.toString() } @@ -59,3 +92,5 @@ data class Selector(val keyExpr: KeyExpr, val parameters: Parameters? = null) : keyExpr.close() } } + +fun String.intoSelector(): Result = Selector.tryFrom(this)