From 3b8a76ccaf0b9c7e57dceae1dc626f5648df884a Mon Sep 17 00:00:00 2001 From: Jeff Lockhart Date: Mon, 8 Jan 2024 17:32:57 -0700 Subject: [PATCH 1/6] Implement Document API for DocumentBuilder --- .../kotlin/kotbase/ktx/DocumentExtensions.kt | 287 +++++++++++++----- 1 file changed, 214 insertions(+), 73 deletions(-) diff --git a/couchbase-lite-ktx/src/commonMain/kotlin/kotbase/ktx/DocumentExtensions.kt b/couchbase-lite-ktx/src/commonMain/kotlin/kotbase/ktx/DocumentExtensions.kt index cf5ec65f9..de7cf0d85 100644 --- a/couchbase-lite-ktx/src/commonMain/kotlin/kotbase/ktx/DocumentExtensions.kt +++ b/couchbase-lite-ktx/src/commonMain/kotlin/kotbase/ktx/DocumentExtensions.kt @@ -19,7 +19,7 @@ * Modified by Jeff Lockhart * - Use kotbase package * - Resolve explicitApiWarning() requirements - * - Implement MutableDictionaryInterface explicitly (not part of KMP API) + * - Implement DictionaryInterface and MutableDictionaryInterface explicitly (not part of KMP API) */ package kotbase.ktx @@ -47,12 +47,12 @@ import kotlinx.datetime.Instant public fun MutableDocument(block: DocumentBuilder.() -> Unit): MutableDocument = DocumentBuilder().apply(block).build() -public fun MutableDocument(id: String, block: DocumentBuilder.() -> Unit): MutableDocument = +public fun MutableDocument(id: String?, block: DocumentBuilder.() -> Unit): MutableDocument = DocumentBuilder(id).apply(block).build() public class DocumentBuilder internal constructor(id: String? = null) { - private val document: MutableDocument = id?.let { MutableDocument(it) } ?: MutableDocument() + private val document: MutableDocument = MutableDocument(id) internal fun build() = document @@ -64,33 +64,203 @@ public class DocumentBuilder internal constructor(id: String? = null) { } /** - * Get a property's value as an Array, which is a mapping object of an array value. - * Returns null if the property doesn't exist, or its value is not an array. + * The number of the entries in the document. + */ + public val count: Int = + document.count + + /** + * A List containing all keys, or an empty List if the document has no properties. + */ + public val keys: List = + document.keys + + /** + * Gets a property's value as an object. The object types are Blob, Array, + * Dictionary, Number, or String based on the underlying data type; or nil if the + * property value is null or the property doesn't exist. * * @param key the key. - * @return the Array object. + * @return the object value or null. */ - public fun getArray(key: String): MutableArray? = - document.getArray(key) + public fun getValue(key: String): Any? = + document.getValue(key) /** - * Get a property's value as a Dictionary, which is a mapping object of a dictionary value. - * Returns null if the property doesn't exist, or its value is not a dictionary. + * Gets a property's value as a String. + * Returns null if the value doesn't exist, or its value is not a String. * - * @param key the key. - * @return the Dictionary object or null if the key doesn't exist. + * @param key the key + * @return the String or null. */ - public fun getDictionary(key: String): MutableDictionary? = - document.getDictionary(key) + public fun getString(key: String): String? = + document.getString(key) /** - * Removes the mapping for a key from this Dictionary + * Gets a property's value as a Number. + * Returns null if the value doesn't exist, or its value is not a Number. * - * @param key the key. + * @param key the key + * @return the Number or nil. + */ + public fun getNumber(key: String): Number? = + document.getNumber(key) + + /** + * Gets a property's value as an int. + * Floating point values will be rounded. The value `true` is returned as 1, `false` as 0. + * Returns 0 if the value doesn't exist or does not have a numeric value. + * + * @param key the key + * @return the int value. + */ + public fun getInt(key: String): Int = + document.getInt(key) + + /** + * Gets a property's value as a long. + * Floating point values will be rounded. The value `true` is returned as 1, `false` as 0. + * Returns 0 if the value doesn't exist or does not have a numeric value. + * + * @param key the key + * @return the long value. + */ + public fun getLong(key: String): Long = + document.getLong(key) + + /** + * Gets a property's value as a float. + * Integers will be converted to float. The value `true` is returned as 1.0, `false` as 0.0. + * Returns 0.0 if the value doesn't exist or does not have a numeric value. + * + * @param key the key + * @return the float value. + */ + public fun getFloat(key: String): Float = + document.getFloat(key) + + /** + * Gets a property's value as a double. + * Integers will be converted to double. The value `true` is returned as 1.0, `false` as 0.0. + * Returns 0.0 if the property doesn't exist or does not have a numeric value. + * + * @param key the key + * @return the double value. + */ + public fun getDouble(key: String): Double = + document.getDouble(key) + + /** + * Gets a property's value as a boolean. Returns true if the value exists, and is either `true` + * or a nonzero number. + * + * @param key the key + * @return the boolean value. + */ + public fun getBoolean(key: String): Boolean = + document.getBoolean(key) + + /** + * Gets a property's value as a Blob. + * Returns null if the value doesn't exist, or its value is not a Blob. + * + * @param key the key + * @return the Blob value or null. + */ + public fun getBlob(key: String): Blob? = + document.getBlob(key) + + /** + * Gets a property's value as an Instant date. + * JSON does not directly support dates, so the actual property value must be a string, which is + * then parsed according to the ISO-8601 date format (the default used in JSON.) + * Returns null if the value doesn't exist, is not a string, or is not parsable as a date. + * NOTE: This is not a generic date parser! It only recognizes the ISO-8601 format, with or + * without milliseconds. + * + * @param key the key + * @return the Instant date value or null. + */ + public fun getDate(key: String): Instant? = + document.getDate(key) + + /** + * Gets content of the current object as a Map. The values contained in the returned + * Map object are all JSON based values. + * + * @return the Map object representing the content of the current object in the JSON format. + */ + public fun toMap(): Map = + document.toMap() + + /** + * Tests whether a property exists or not. + * This can be less expensive than getValue(String), + * because it does not have to allocate an Object for the property value. + * + * @param key the key + * @return the boolean value representing whether a property exists or not. + */ + public operator fun contains(key: String): Boolean = + document.contains(key) + + /** + * Populate a document with content from a Map. + * Allowed value types are List, Instant, Map, Number, null, String, Array, Blob, and Dictionary. + * If present, Lists, Arrays, Maps and Dictionaries may contain only the above types. Setting the + * document content will replace the current data including the existing Array and Dictionary + * objects. + * + * @param data the dictionary object. + * @return this Document instance + */ + public fun setData(data: Map): MutableDocument = + document.setData(data) + + /** + * Populate a document with content from a JSON string. + * Allowed value types are List, Instant, Map, Number, null, String, Array, Blob, and Dictionary. + * If present, Lists, Arrays, Maps and Dictionaries may contain only the above types. Setting the + * document content will replace the current data including the existing Array and Dictionary + * objects. + * + * @param json the dictionary object. + * @return this Document instance + */ + public fun setJSON(json: String): MutableDocument = + document.setJSON(json) + + /** + * Set an object value by key. Allowed value types are List, Instant, Map, Number, null, String, + * Array, Blob, and Dictionary. If present, Lists, Maps and Dictionaries may contain only + * the above types. An Instant date object will be converted to an ISO-8601 format string. + * + * @param key the key. + * @param value the Object value. + * @return this Document instance + */ + public fun setValue(key: String, value: Any?): MutableDocument = + document.setValue(key, value) + + /** + * Set a String value for the given key + * + * @param key the key. + * @param value the String value. * @return this MutableDocument instance */ - public fun remove(key: String): MutableDocument = - document.remove(key) + public fun setString(key: String, value: String?): MutableDocument = + document.setString(key, value) + + /** + * Set a Number value for the given key + * + * @param key the key. + * @param value the Number value. + * @return this MutableDocument instance + */ + public fun setNumber(key: String, value: Number?): MutableDocument = + document.setNumber(key, value) /** * Set an integer value for the given key @@ -142,36 +312,6 @@ public class DocumentBuilder internal constructor(id: String? = null) { public fun setBoolean(key: String, value: Boolean): MutableDocument = document.setBoolean(key, value) - /** - * Set a Number value for the given key - * - * @param key the key. - * @param value the Number value. - * @return this MutableDocument instance - */ - public fun setNumber(key: String, value: Number): MutableDocument = - document.setNumber(key, value) - - /** - * Set a String value for the given key - * - * @param key the key. - * @param value the String value. - * @return this MutableDocument instance - */ - public fun setString(key: String, value: String): MutableDocument = - document.setString(key, value) - - /** - * Set a Date value for the given key - * - * @param key the key. - * @param value the Date value. - * @return this MutableDocument instance - */ - public fun setDate(key: String, value: Instant?): MutableDocument = - document.setDate(key, value) - /** * Set a Blob value for the given key * @@ -183,16 +323,14 @@ public class DocumentBuilder internal constructor(id: String? = null) { document.setBlob(key, value) /** - * Set an object value by key. Allowed value types are List, Date, Map, Number, null, String, - * Array, Blob, and Dictionary. If present, Lists, Arrays, Maps and Dictionaries may contain only - * the above types. A Date object will be converted to an ISO-8601 format string. + * Set an Instant date value for the given key * * @param key the key. - * @param value the Object value. - * @return this Document instance + * @param value the Date value. + * @return this MutableDocument instance */ - public fun setValue(key: String, value: Any?): MutableDocument = - document.setValue(key, value) + public fun setDate(key: String, value: Instant?): MutableDocument = + document.setDate(key, value) /** * Set an Array value for the given key @@ -215,28 +353,31 @@ public class DocumentBuilder internal constructor(id: String? = null) { document.setDictionary(key, value) /** - * Populate a document with content from a Map. - * Allowed value types are List, Date, Map, Number, null, String, Array, Blob, and Dictionary. - * If present, Lists, Arrays, Maps and Dictionaries may contain only the above types. Setting the - * document content will replace the current data including the existing Array and Dictionary - * objects. + * Removes the mapping for a key from this Dictionary * - * @param data the dictionary object. - * @return this Document instance + * @param key the key. + * @return this MutableDocument instance */ - public fun setData(data: Map): MutableDocument = - document.setData(data) + public fun remove(key: String): MutableDocument = + document.remove(key) /** - * Populate a document with content from a JSON string. - * Allowed value types are List, Date, Map, Number, null, String, Array, Blob, and Dictionary. - * If present, Lists, Arrays, Maps and Dictionaries may contain only the above types. Setting the - * document content will replace the current data including the existing Array and Dictionary - * objects. + * Get a property's value as an Array. + * Returns null if the property doesn't exist, or its value is not an array. * - * @param json the dictionary object. - * @return this Document instance + * @param key the key. + * @return the Array object. */ - public fun setJSON(json: String): MutableDocument = - document.setJSON(json) + public fun getArray(key: String): MutableArray? = + document.getArray(key) + + /** + * Get a property's value as a Dictionary. + * Returns null if the property doesn't exist, or its value is not a dictionary. + * + * @param key the key. + * @return the Dictionary object or null if the key doesn't exist. + */ + public fun getDictionary(key: String): MutableDictionary? = + document.getDictionary(key) } From 74c6c47675505ad7fdb6b57025803b0a1efdabf6 Mon Sep 17 00:00:00 2001 From: Jeff Lockhart Date: Mon, 8 Jan 2024 17:33:37 -0700 Subject: [PATCH 2/6] Add mutableArrayOf, mutableDictOf, and mutableDocOf KTX APIs --- .../commonMain/kotlin/kotbase/ktx/ArrayExt.kt | 28 ++++++++++++ .../kotlin/kotbase/ktx/DictionaryExt.kt | 28 ++++++++++++ .../kotlin/kotbase/ktx/DocumentExt.kt | 44 +++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 couchbase-lite-ktx/src/commonMain/kotlin/kotbase/ktx/ArrayExt.kt create mode 100644 couchbase-lite-ktx/src/commonMain/kotlin/kotbase/ktx/DictionaryExt.kt create mode 100644 couchbase-lite-ktx/src/commonMain/kotlin/kotbase/ktx/DocumentExt.kt diff --git a/couchbase-lite-ktx/src/commonMain/kotlin/kotbase/ktx/ArrayExt.kt b/couchbase-lite-ktx/src/commonMain/kotlin/kotbase/ktx/ArrayExt.kt new file mode 100644 index 000000000..46e2ee5a8 --- /dev/null +++ b/couchbase-lite-ktx/src/commonMain/kotlin/kotbase/ktx/ArrayExt.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2024 Jeff Lockhart + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package kotbase.ktx + +import kotbase.MutableArray + +/** + * Creates a new MutableArray with content from the passed values. + * Allowed value types are List, Instant, Map, Number, null, String, Array, Blob, and Dictionary. + * If present, Lists, Arrays, Maps and Dictionaries may contain only the above types. + * + * @param values the array content values + */ +public fun mutableArrayOf(vararg values: Any?): MutableArray = + MutableArray(values.toList()) diff --git a/couchbase-lite-ktx/src/commonMain/kotlin/kotbase/ktx/DictionaryExt.kt b/couchbase-lite-ktx/src/commonMain/kotlin/kotbase/ktx/DictionaryExt.kt new file mode 100644 index 000000000..4c53b3a52 --- /dev/null +++ b/couchbase-lite-ktx/src/commonMain/kotlin/kotbase/ktx/DictionaryExt.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2024 Jeff Lockhart + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package kotbase.ktx + +import kotbase.MutableDictionary + +/** + * Creates a new MutableDictionary with content from the passed key/value pairs. + * Allowed value types are List, Instant, Map, Number, null, String, Array, Blob, and Dictionary. + * If present, Lists, Arrays, Maps and Dictionaries may contain only the above types. + * + * @param pairs the content key/value pairs + */ +public fun mutableDictOf(vararg pairs: Pair): MutableDictionary = + MutableDictionary(pairs.toMap()) diff --git a/couchbase-lite-ktx/src/commonMain/kotlin/kotbase/ktx/DocumentExt.kt b/couchbase-lite-ktx/src/commonMain/kotlin/kotbase/ktx/DocumentExt.kt new file mode 100644 index 000000000..eaeeae311 --- /dev/null +++ b/couchbase-lite-ktx/src/commonMain/kotlin/kotbase/ktx/DocumentExt.kt @@ -0,0 +1,44 @@ +/* + * Copyright 2024 Jeff Lockhart + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package kotbase.ktx + +import kotbase.MutableDocument + +/** + * Creates a new Document with a given ID and content from the passed key/value pairs. + * If the id is null, the document will be created with a new random UUID. + * Allowed value types are List, Instant, Map, Number, null, String, Array, Blob, and Dictionary. + * The List and Map must contain only the above types. + * The created document will be saved into a database when you call + * the Database's save(Document) method with the document object given. + * + * @param id the document ID + * @param pairs the content key/value pairs + */ +public fun mutableDocOf(id: String?, vararg pairs: Pair): MutableDocument = + MutableDocument(id, pairs.toMap()) + +/** + * Creates a new Document with a new random UUID and the key/value pairs as the content. + * Allowed value types are List, Instant, Map, Number, null, String, Array, Blob, and Dictionary. + * If present, Lists, Arrays, Maps and Dictionaries may contain only the above types. + * The created document will be saved into a database when you call Database.save(Document) + * with this document object. + * + * @param pairs the content key/value pairs + */ +public fun mutableDocOf(vararg pairs: Pair): MutableDocument = + MutableDocument(pairs.toMap()) From 468212d31bbd0d13bb1e4f932aa38e7be16b0a2c Mon Sep 17 00:00:00 2001 From: Jeff Lockhart Date: Mon, 8 Jan 2024 18:58:54 -0700 Subject: [PATCH 3/6] Remove unused generic type --- .../src/commonMain/kotlin/kotbase/ktx/DocumentExtensions.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/couchbase-lite-ktx/src/commonMain/kotlin/kotbase/ktx/DocumentExtensions.kt b/couchbase-lite-ktx/src/commonMain/kotlin/kotbase/ktx/DocumentExtensions.kt index de7cf0d85..e56d52334 100644 --- a/couchbase-lite-ktx/src/commonMain/kotlin/kotbase/ktx/DocumentExtensions.kt +++ b/couchbase-lite-ktx/src/commonMain/kotlin/kotbase/ktx/DocumentExtensions.kt @@ -59,7 +59,7 @@ public class DocumentBuilder internal constructor(id: String? = null) { /** * Determines the key-to-value relation between the receiver string and the provided [value]. */ - public infix fun String.to(value: T) { + public infix fun String.to(value: Any?) { setValue(this, value) } From 317df08e09758489c407eb15a22dac43f0260a0b Mon Sep 17 00:00:00 2001 From: Jeff Lockhart Date: Mon, 8 Jan 2024 18:59:19 -0700 Subject: [PATCH 4/6] Dump new KTX API --- .../api/android/couchbase-lite-ee-ktx.api | 27 +++++++++++++++++++ .../api/jvm/couchbase-lite-ee-ktx.api | 27 +++++++++++++++++++ .../api/android/couchbase-lite-ktx.api | 27 +++++++++++++++++++ .../api/jvm/couchbase-lite-ktx.api | 27 +++++++++++++++++++ 4 files changed, 108 insertions(+) diff --git a/couchbase-lite-ee-ktx/api/android/couchbase-lite-ee-ktx.api b/couchbase-lite-ee-ktx/api/android/couchbase-lite-ee-ktx.api index 41b6f9cde..190f0b1ec 100644 --- a/couchbase-lite-ee-ktx/api/android/couchbase-lite-ee-ktx.api +++ b/couchbase-lite-ee-ktx/api/android/couchbase-lite-ee-ktx.api @@ -1,3 +1,7 @@ +public final class kotbase/ktx/ArrayExtKt { + public static final fun mutableArrayOf ([Ljava/lang/Object;)Lkotbase/MutableArray; +} + public final class kotbase/ktx/CollectionExtKt { public static final fun documentFlow (Lkotbase/Collection;Ljava/lang/String;Lkotlin/coroutines/CoroutineContext;)Lkotlinx/coroutines/flow/Flow; public static synthetic fun documentFlow$default (Lkotbase/Collection;Ljava/lang/String;Lkotlin/coroutines/CoroutineContext;ILjava/lang/Object;)Lkotlinx/coroutines/flow/Flow; @@ -8,10 +12,27 @@ public final class kotbase/ktx/DatabaseExtKt { public static final fun documentFlow (Lkotbase/Database;Ljava/lang/String;Lkotlin/coroutines/CoroutineContext;)Lkotlinx/coroutines/flow/Flow; } +public final class kotbase/ktx/DictionaryExtKt { + public static final fun mutableDictOf ([Lkotlin/Pair;)Lkotbase/MutableDictionary; +} + public final class kotbase/ktx/DocumentBuilder { public fun ()V + public final fun contains (Ljava/lang/String;)Z public final fun getArray (Ljava/lang/String;)Lkotbase/MutableArray; + public final fun getBlob (Ljava/lang/String;)Lkotbase/Blob; + public final fun getBoolean (Ljava/lang/String;)Z + public final fun getCount ()I + public final fun getDate (Ljava/lang/String;)Lkotlinx/datetime/Instant; public final fun getDictionary (Ljava/lang/String;)Lkotbase/MutableDictionary; + public final fun getDouble (Ljava/lang/String;)D + public final fun getFloat (Ljava/lang/String;)F + public final fun getInt (Ljava/lang/String;)I + public final fun getKeys ()Ljava/util/List; + public final fun getLong (Ljava/lang/String;)J + public final fun getNumber (Ljava/lang/String;)Ljava/lang/Number; + public final fun getString (Ljava/lang/String;)Ljava/lang/String; + public final fun getValue (Ljava/lang/String;)Ljava/lang/Object; public final fun remove (Ljava/lang/String;)Lkotbase/MutableDocument; public final fun setArray (Ljava/lang/String;Lkotbase/Array;)Lkotbase/MutableDocument; public final fun setBlob (Ljava/lang/String;Lkotbase/Blob;)Lkotbase/MutableDocument; @@ -28,6 +49,12 @@ public final class kotbase/ktx/DocumentBuilder { public final fun setString (Ljava/lang/String;Ljava/lang/String;)Lkotbase/MutableDocument; public final fun setValue (Ljava/lang/String;Ljava/lang/Object;)Lkotbase/MutableDocument; public final fun to (Ljava/lang/String;Ljava/lang/Object;)V + public final fun toMap ()Ljava/util/Map; +} + +public final class kotbase/ktx/DocumentExtKt { + public static final fun mutableDocOf (Ljava/lang/String;[Lkotlin/Pair;)Lkotbase/MutableDocument; + public static final fun mutableDocOf ([Lkotlin/Pair;)Lkotbase/MutableDocument; } public final class kotbase/ktx/DocumentExtensionsKt { diff --git a/couchbase-lite-ee-ktx/api/jvm/couchbase-lite-ee-ktx.api b/couchbase-lite-ee-ktx/api/jvm/couchbase-lite-ee-ktx.api index 0b3608d17..b510f30c7 100644 --- a/couchbase-lite-ee-ktx/api/jvm/couchbase-lite-ee-ktx.api +++ b/couchbase-lite-ee-ktx/api/jvm/couchbase-lite-ee-ktx.api @@ -1,3 +1,7 @@ +public final class kotbase/ktx/ArrayExtKt { + public static final fun mutableArrayOf ([Ljava/lang/Object;)Lkotbase/MutableArray; +} + public final class kotbase/ktx/CollectionExtKt { public static final fun documentFlow (Lkotbase/Collection;Ljava/lang/String;Lkotlin/coroutines/CoroutineContext;)Lkotlinx/coroutines/flow/Flow; public static synthetic fun documentFlow$default (Lkotbase/Collection;Ljava/lang/String;Lkotlin/coroutines/CoroutineContext;ILjava/lang/Object;)Lkotlinx/coroutines/flow/Flow; @@ -8,10 +12,27 @@ public final class kotbase/ktx/DatabaseExtKt { public static final fun documentFlow (Lkotbase/Database;Ljava/lang/String;Lkotlin/coroutines/CoroutineContext;)Lkotlinx/coroutines/flow/Flow; } +public final class kotbase/ktx/DictionaryExtKt { + public static final fun mutableDictOf ([Lkotlin/Pair;)Lkotbase/MutableDictionary; +} + public final class kotbase/ktx/DocumentBuilder { public fun ()V + public final fun contains (Ljava/lang/String;)Z public final fun getArray (Ljava/lang/String;)Lkotbase/MutableArray; + public final fun getBlob (Ljava/lang/String;)Lkotbase/Blob; + public final fun getBoolean (Ljava/lang/String;)Z + public final fun getCount ()I + public final fun getDate (Ljava/lang/String;)Lkotlinx/datetime/Instant; public final fun getDictionary (Ljava/lang/String;)Lkotbase/MutableDictionary; + public final fun getDouble (Ljava/lang/String;)D + public final fun getFloat (Ljava/lang/String;)F + public final fun getInt (Ljava/lang/String;)I + public final fun getKeys ()Ljava/util/List; + public final fun getLong (Ljava/lang/String;)J + public final fun getNumber (Ljava/lang/String;)Ljava/lang/Number; + public final fun getString (Ljava/lang/String;)Ljava/lang/String; + public final fun getValue (Ljava/lang/String;)Ljava/lang/Object; public final fun remove (Ljava/lang/String;)Lkotbase/MutableDocument; public final fun setArray (Ljava/lang/String;Lkotbase/Array;)Lkotbase/MutableDocument; public final fun setBlob (Ljava/lang/String;Lkotbase/Blob;)Lkotbase/MutableDocument; @@ -28,6 +49,12 @@ public final class kotbase/ktx/DocumentBuilder { public final fun setString (Ljava/lang/String;Ljava/lang/String;)Lkotbase/MutableDocument; public final fun setValue (Ljava/lang/String;Ljava/lang/Object;)Lkotbase/MutableDocument; public final fun to (Ljava/lang/String;Ljava/lang/Object;)V + public final fun toMap ()Ljava/util/Map; +} + +public final class kotbase/ktx/DocumentExtKt { + public static final fun mutableDocOf (Ljava/lang/String;[Lkotlin/Pair;)Lkotbase/MutableDocument; + public static final fun mutableDocOf ([Lkotlin/Pair;)Lkotbase/MutableDocument; } public final class kotbase/ktx/DocumentExtensionsKt { diff --git a/couchbase-lite-ktx/api/android/couchbase-lite-ktx.api b/couchbase-lite-ktx/api/android/couchbase-lite-ktx.api index 41b6f9cde..190f0b1ec 100644 --- a/couchbase-lite-ktx/api/android/couchbase-lite-ktx.api +++ b/couchbase-lite-ktx/api/android/couchbase-lite-ktx.api @@ -1,3 +1,7 @@ +public final class kotbase/ktx/ArrayExtKt { + public static final fun mutableArrayOf ([Ljava/lang/Object;)Lkotbase/MutableArray; +} + public final class kotbase/ktx/CollectionExtKt { public static final fun documentFlow (Lkotbase/Collection;Ljava/lang/String;Lkotlin/coroutines/CoroutineContext;)Lkotlinx/coroutines/flow/Flow; public static synthetic fun documentFlow$default (Lkotbase/Collection;Ljava/lang/String;Lkotlin/coroutines/CoroutineContext;ILjava/lang/Object;)Lkotlinx/coroutines/flow/Flow; @@ -8,10 +12,27 @@ public final class kotbase/ktx/DatabaseExtKt { public static final fun documentFlow (Lkotbase/Database;Ljava/lang/String;Lkotlin/coroutines/CoroutineContext;)Lkotlinx/coroutines/flow/Flow; } +public final class kotbase/ktx/DictionaryExtKt { + public static final fun mutableDictOf ([Lkotlin/Pair;)Lkotbase/MutableDictionary; +} + public final class kotbase/ktx/DocumentBuilder { public fun ()V + public final fun contains (Ljava/lang/String;)Z public final fun getArray (Ljava/lang/String;)Lkotbase/MutableArray; + public final fun getBlob (Ljava/lang/String;)Lkotbase/Blob; + public final fun getBoolean (Ljava/lang/String;)Z + public final fun getCount ()I + public final fun getDate (Ljava/lang/String;)Lkotlinx/datetime/Instant; public final fun getDictionary (Ljava/lang/String;)Lkotbase/MutableDictionary; + public final fun getDouble (Ljava/lang/String;)D + public final fun getFloat (Ljava/lang/String;)F + public final fun getInt (Ljava/lang/String;)I + public final fun getKeys ()Ljava/util/List; + public final fun getLong (Ljava/lang/String;)J + public final fun getNumber (Ljava/lang/String;)Ljava/lang/Number; + public final fun getString (Ljava/lang/String;)Ljava/lang/String; + public final fun getValue (Ljava/lang/String;)Ljava/lang/Object; public final fun remove (Ljava/lang/String;)Lkotbase/MutableDocument; public final fun setArray (Ljava/lang/String;Lkotbase/Array;)Lkotbase/MutableDocument; public final fun setBlob (Ljava/lang/String;Lkotbase/Blob;)Lkotbase/MutableDocument; @@ -28,6 +49,12 @@ public final class kotbase/ktx/DocumentBuilder { public final fun setString (Ljava/lang/String;Ljava/lang/String;)Lkotbase/MutableDocument; public final fun setValue (Ljava/lang/String;Ljava/lang/Object;)Lkotbase/MutableDocument; public final fun to (Ljava/lang/String;Ljava/lang/Object;)V + public final fun toMap ()Ljava/util/Map; +} + +public final class kotbase/ktx/DocumentExtKt { + public static final fun mutableDocOf (Ljava/lang/String;[Lkotlin/Pair;)Lkotbase/MutableDocument; + public static final fun mutableDocOf ([Lkotlin/Pair;)Lkotbase/MutableDocument; } public final class kotbase/ktx/DocumentExtensionsKt { diff --git a/couchbase-lite-ktx/api/jvm/couchbase-lite-ktx.api b/couchbase-lite-ktx/api/jvm/couchbase-lite-ktx.api index 0b3608d17..b510f30c7 100644 --- a/couchbase-lite-ktx/api/jvm/couchbase-lite-ktx.api +++ b/couchbase-lite-ktx/api/jvm/couchbase-lite-ktx.api @@ -1,3 +1,7 @@ +public final class kotbase/ktx/ArrayExtKt { + public static final fun mutableArrayOf ([Ljava/lang/Object;)Lkotbase/MutableArray; +} + public final class kotbase/ktx/CollectionExtKt { public static final fun documentFlow (Lkotbase/Collection;Ljava/lang/String;Lkotlin/coroutines/CoroutineContext;)Lkotlinx/coroutines/flow/Flow; public static synthetic fun documentFlow$default (Lkotbase/Collection;Ljava/lang/String;Lkotlin/coroutines/CoroutineContext;ILjava/lang/Object;)Lkotlinx/coroutines/flow/Flow; @@ -8,10 +12,27 @@ public final class kotbase/ktx/DatabaseExtKt { public static final fun documentFlow (Lkotbase/Database;Ljava/lang/String;Lkotlin/coroutines/CoroutineContext;)Lkotlinx/coroutines/flow/Flow; } +public final class kotbase/ktx/DictionaryExtKt { + public static final fun mutableDictOf ([Lkotlin/Pair;)Lkotbase/MutableDictionary; +} + public final class kotbase/ktx/DocumentBuilder { public fun ()V + public final fun contains (Ljava/lang/String;)Z public final fun getArray (Ljava/lang/String;)Lkotbase/MutableArray; + public final fun getBlob (Ljava/lang/String;)Lkotbase/Blob; + public final fun getBoolean (Ljava/lang/String;)Z + public final fun getCount ()I + public final fun getDate (Ljava/lang/String;)Lkotlinx/datetime/Instant; public final fun getDictionary (Ljava/lang/String;)Lkotbase/MutableDictionary; + public final fun getDouble (Ljava/lang/String;)D + public final fun getFloat (Ljava/lang/String;)F + public final fun getInt (Ljava/lang/String;)I + public final fun getKeys ()Ljava/util/List; + public final fun getLong (Ljava/lang/String;)J + public final fun getNumber (Ljava/lang/String;)Ljava/lang/Number; + public final fun getString (Ljava/lang/String;)Ljava/lang/String; + public final fun getValue (Ljava/lang/String;)Ljava/lang/Object; public final fun remove (Ljava/lang/String;)Lkotbase/MutableDocument; public final fun setArray (Ljava/lang/String;Lkotbase/Array;)Lkotbase/MutableDocument; public final fun setBlob (Ljava/lang/String;Lkotbase/Blob;)Lkotbase/MutableDocument; @@ -28,6 +49,12 @@ public final class kotbase/ktx/DocumentBuilder { public final fun setString (Ljava/lang/String;Ljava/lang/String;)Lkotbase/MutableDocument; public final fun setValue (Ljava/lang/String;Ljava/lang/Object;)Lkotbase/MutableDocument; public final fun to (Ljava/lang/String;Ljava/lang/Object;)V + public final fun toMap ()Ljava/util/Map; +} + +public final class kotbase/ktx/DocumentExtKt { + public static final fun mutableDocOf (Ljava/lang/String;[Lkotlin/Pair;)Lkotbase/MutableDocument; + public static final fun mutableDocOf ([Lkotlin/Pair;)Lkotbase/MutableDocument; } public final class kotbase/ktx/DocumentExtensionsKt { From 02e3747d55510eadb35125359da46650c051d560 Mon Sep 17 00:00:00 2001 From: Jeff Lockhart Date: Mon, 8 Jan 2024 19:10:20 -0700 Subject: [PATCH 5/6] Additional docs fixes --- .../src/commonMain/kotlin/kotbase/ktx/DocumentExtensions.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/couchbase-lite-ktx/src/commonMain/kotlin/kotbase/ktx/DocumentExtensions.kt b/couchbase-lite-ktx/src/commonMain/kotlin/kotbase/ktx/DocumentExtensions.kt index e56d52334..97715fee6 100644 --- a/couchbase-lite-ktx/src/commonMain/kotlin/kotbase/ktx/DocumentExtensions.kt +++ b/couchbase-lite-ktx/src/commonMain/kotlin/kotbase/ktx/DocumentExtensions.kt @@ -232,7 +232,7 @@ public class DocumentBuilder internal constructor(id: String? = null) { /** * Set an object value by key. Allowed value types are List, Instant, Map, Number, null, String, - * Array, Blob, and Dictionary. If present, Lists, Maps and Dictionaries may contain only + * Array, Blob, and Dictionary. If present, Lists, Arrays, Maps and Dictionaries may contain only * the above types. An Instant date object will be converted to an ISO-8601 format string. * * @param key the key. From b65fd51a01deb22f0d57b33ba905b733dd36d335 Mon Sep 17 00:00:00 2001 From: Jeff Lockhart Date: Mon, 8 Jan 2024 19:44:43 -0700 Subject: [PATCH 6/6] Add docs --- couchbase-lite-ee-ktx/README.md | 20 ++++++++++++++++++++ couchbase-lite-ktx/README.md | 20 ++++++++++++++++++++ docs/ktx.md | 20 ++++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/couchbase-lite-ee-ktx/README.md b/couchbase-lite-ee-ktx/README.md index cef59fb54..901ecef65 100644 --- a/couchbase-lite-ee-ktx/README.md +++ b/couchbase-lite-ee-ktx/README.md @@ -76,6 +76,26 @@ val document = MutableDocument { database.save(document) ``` +##### Collection creation functions + +You can create a `MutableArray` or `MutableDictionary` using idiomatic `vararg` functions: + +```kotlin +mutableArrayOf("hello", 42, true) +mutableDictOf("key1" to "value1", "key2" to 2, "key3" to null) +``` + +The similar `mutableDocOf` function allows nesting dictionary types, unlike the `MutableDocument` DSL: + +```kotlin +mutableDocOf( + "string" to "hello", + "number" to 42, + "array" to mutableArrayOf(1, 2, 3), + "dict" to mutableDictOf("key" to "value") +) +``` + #### Flow support Supplementing the `Flow` APIs from [Couchbase Lite Android KTX]( diff --git a/couchbase-lite-ktx/README.md b/couchbase-lite-ktx/README.md index 3d8debc9b..e9a2a2369 100644 --- a/couchbase-lite-ktx/README.md +++ b/couchbase-lite-ktx/README.md @@ -76,6 +76,26 @@ val document = MutableDocument { database.save(document) ``` +##### Collection creation functions + +You can create a `MutableArray` or `MutableDictionary` using idiomatic `vararg` functions: + +```kotlin +mutableArrayOf("hello", 42, true) +mutableDictOf("key1" to "value1", "key2" to 2, "key3" to null) +``` + +The similar `mutableDocOf` function allows nesting dictionary types, unlike the `MutableDocument` DSL: + +```kotlin +mutableDocOf( + "string" to "hello", + "number" to 42, + "array" to mutableArrayOf(1, 2, 3), + "dict" to mutableDictOf("key" to "value") +) +``` + #### Flow support Supplementing the `Flow` APIs from [Couchbase Lite Android KTX]( diff --git a/docs/ktx.md b/docs/ktx.md index 7863e5aa8..9cfa667e5 100644 --- a/docs/ktx.md +++ b/docs/ktx.md @@ -86,6 +86,26 @@ val document = MutableDocument { database.save(document) ``` +#### Collection creation functions + +You can create a `MutableArray` or `MutableDictionary` using idiomatic `vararg` functions: + +```kotlin +mutableArrayOf("hello", 42, true) +mutableDictOf("key1" to "value1", "key2" to 2, "key3" to null) +``` + +The similar `mutableDocOf` function allows nesting dictionary types, unlike the `MutableDocument` DSL: + +```kotlin +mutableDocOf( + "string" to "hello", + "number" to 42, + "array" to mutableArrayOf(1, 2, 3), + "dict" to mutableDictOf("key" to "value") +) +``` + ### Flow support Supplementing the `Flow` APIs from [Couchbase Lite Android KTX](