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

KTX collection and doc creation functions #13

Merged
merged 6 commits into from
Jan 23, 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
20 changes: 20 additions & 0 deletions couchbase-lite-ee-ktx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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](
Expand Down
27 changes: 27 additions & 0 deletions couchbase-lite-ee-ktx/api/android/couchbase-lite-ee-ktx.api
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 <init> ()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;
Expand All @@ -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 {
Expand Down
27 changes: 27 additions & 0 deletions couchbase-lite-ee-ktx/api/jvm/couchbase-lite-ee-ktx.api
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 <init> ()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;
Expand All @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions couchbase-lite-ktx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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](
Expand Down
27 changes: 27 additions & 0 deletions couchbase-lite-ktx/api/android/couchbase-lite-ktx.api
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 <init> ()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;
Expand All @@ -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 {
Expand Down
27 changes: 27 additions & 0 deletions couchbase-lite-ktx/api/jvm/couchbase-lite-ktx.api
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 <init> ()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;
Expand All @@ -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 {
Expand Down
28 changes: 28 additions & 0 deletions couchbase-lite-ktx/src/commonMain/kotlin/kotbase/ktx/ArrayExt.kt
Original file line number Diff line number Diff line change
@@ -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())
Original file line number Diff line number Diff line change
@@ -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<String, Any?>): MutableDictionary =
MutableDictionary(pairs.toMap())
Original file line number Diff line number Diff line change
@@ -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<String, Any?>): 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<String, Any?>): MutableDocument =
MutableDocument(pairs.toMap())
Loading