Skip to content

Commit 3021502

Browse files
committed
Update Readme
1 parent bf709a3 commit 3021502

File tree

7 files changed

+23
-18
lines changed

7 files changed

+23
-18
lines changed

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,16 @@ data class City(val name: String)
8585
Instances of these classes can now be passed [along with their serializer](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md#introduction-to-serializers) to the SDK:
8686

8787
```kotlin
88-
db.collection("cities").document("LA").set(City.serializer(), city, encodeDefaults = true)
88+
db.collection("cities").document("LA").set(City.serializer(), city) { shouldEncodeElementDefault = true }
8989
```
9090

91-
The `encodeDefaults` parameter is optional and defaults to `true`, set this to false to omit writing optional properties if they are equal to theirs default values.
91+
The `buildSettings` closure is optional and allows for configuring serialization behaviour.
92+
93+
Setting the `shouldEncodeElementDefault` parameter is optional and defaults to `true`, set this to false to omit writing optional properties if they are equal to theirs default values.
9294
Using [@EncodeDefault](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-encode-default/) on properties is a recommended way to locally override the behavior set with `encodeDefaults`.
9395

94-
You can also omit the serializer but this is discouraged due to a [current limitation on Kotlin/JS and Kotlin/Native](https://github.com/Kotlin/kotlinx.serialization/issues/1116#issuecomment-704342452)
96+
You can also omit the serializer but this is discouraged due to a [current limitation on Kotlin/JS and Kotlin/Native](https://github.com/Kotlin/kotlinx.serialization/issues/1116#issuecomment-704342452).
97+
To support [contextual serialization](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md#contextual-serialization) or [open polymorphism](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/polymorphism.md#open-polymorphism) the `serializersModule` can be overridden in any `EncodeSettings` or `DecodeSettings`
9598

9699
<h4><a href="https://firebase.google.com/docs/firestore/manage-data/add-data#server_timestamp">Server Timestamp</a></h3>
97100

firebase-common/src/commonTest/kotlin/dev/gitlive/firebase/EncodersTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class EncodersTest {
124124
@Test
125125
fun encodeDecodeSealedClass() {
126126
val sealedClass = SealedClass.Test("value")
127-
val encoded = encode(SealedClass.serializer(), sealedClass, shouldEncodeElementDefault = true)
127+
val encoded = encode(SealedClass.serializer(), sealedClass) { shouldEncodeElementDefault = true }
128128

129129
nativeAssertEquals(nativeMapOf("type" to "test", "value" to "value"), encoded)
130130

firebase-common/src/iosMain/kotlin/dev/gitlive/firebase/_encoders.kt

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package dev.gitlive.firebase
66

77
import kotlinx.serialization.descriptors.PolymorphicKind
8-
import kotlinx.serialization.encoding.CompositeEncoder
98
import kotlinx.serialization.descriptors.SerialDescriptor
109
import kotlinx.serialization.descriptors.StructureKind
1110
import kotlin.collections.set

firebase-common/src/jsMain/kotlin/dev/gitlive/firebase/_encoders.kt

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package dev.gitlive.firebase
66

77
import kotlinx.serialization.descriptors.PolymorphicKind
8-
import kotlinx.serialization.encoding.CompositeEncoder
98
import kotlinx.serialization.descriptors.SerialDescriptor
109
import kotlinx.serialization.descriptors.StructureKind
1110
import kotlin.js.json

firebase-firestore/src/commonTest/kotlin/dev/gitlive/firebase/firestore/GeoPointTests.kt

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ class GeoPointTests {
1818
fun encodeGeoPointObject() = runTest {
1919
val geoPoint = GeoPoint(12.3, 45.6)
2020
val item = TestDataWithGeoPoint("123", geoPoint)
21-
val encoded = encodedAsMap(encode(item, shouldEncodeElementDefault = false))
21+
val encoded = encodedAsMap(
22+
encode<TestDataWithGeoPoint>(item) {
23+
shouldEncodeElementDefault = false
24+
}
25+
)
2226
assertEquals("123", encoded["uid"])
2327
// check GeoPoint is encoded to a platform representation
2428
assertEquals(geoPoint.nativeValue, encoded["location"])

firebase-firestore/src/commonTest/kotlin/dev/gitlive/firebase/firestore/TimestampTests.kt

+11-9
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ import dev.gitlive.firebase.nativeAssertEquals
77
import dev.gitlive.firebase.nativeMapOf
88
import dev.gitlive.firebase.runTest
99
import kotlinx.serialization.Serializable
10-
import kotlin.test.*
10+
import kotlin.test.Test
11+
import kotlin.test.assertEquals
12+
import kotlin.test.assertNotEquals
13+
import kotlin.test.assertNotNull
14+
import kotlin.test.assertNull
1115
import kotlin.time.Duration.Companion.milliseconds
12-
import kotlin.time.DurationUnit
1316

1417
@Serializable
1518
data class TestData(
@@ -41,7 +44,7 @@ class TimestampTests {
4144
"updatedAt" to timestamp.nativeValue,
4245
"deletedAt" to null
4346
),
44-
encode(item, shouldEncodeElementDefault = false)
47+
encode<TestData>(item) { shouldEncodeElementDefault = false }
4548
)
4649
}
4750

@@ -56,7 +59,7 @@ class TimestampTests {
5659
"updatedAt" to FieldValue.serverTimestamp.nativeValue,
5760
"deletedAt" to FieldValue.serverTimestamp.nativeValue
5861
),
59-
encode(item, shouldEncodeElementDefault = false)
62+
encode<TestData>(item) { shouldEncodeElementDefault = false }
6063
)
6164
}
6265

@@ -104,11 +107,10 @@ class TimestampTests {
104107

105108
@Test
106109
fun serializers() = runTest {
107-
//todo dont work in js due to use of reified type in firebaseSerializer - uncomment once switched to IR
108-
// assertEquals(BaseTimestampSerializer, (Timestamp(0, 0) as BaseTimestamp).firebaseSerializer())
109-
// assertEquals(BaseTimestampSerializer, (Timestamp.ServerTimestamp as BaseTimestamp).firebaseSerializer())
110-
// assertEquals(TimestampSerializer, Timestamp(0, 0).firebaseSerializer())
111-
// assertEquals(ServerTimestampSerializer, Timestamp.ServerTimestamp.firebaseSerializer())
110+
assertEquals(BaseTimestampSerializer, (Timestamp(0, 0) as BaseTimestamp).firebaseSerializer())
111+
assertEquals(BaseTimestampSerializer, (Timestamp.ServerTimestamp as BaseTimestamp).firebaseSerializer())
112+
assertEquals(TimestampSerializer, Timestamp(0, 0).firebaseSerializer())
113+
assertEquals(ServerTimestampSerializer, Timestamp.ServerTimestamp.firebaseSerializer())
112114
}
113115

114116
@Test

firebase-functions/src/androidMain/kotlin/dev/gitlive/firebase/functions/functions.kt

-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ import dev.gitlive.firebase.DecodeSettings
88
import dev.gitlive.firebase.Firebase
99
import dev.gitlive.firebase.FirebaseApp
1010
import dev.gitlive.firebase.decode
11-
import dev.gitlive.firebase.encode
1211
import kotlinx.coroutines.tasks.await
1312
import kotlinx.serialization.DeserializationStrategy
14-
import kotlinx.serialization.SerializationStrategy
1513
import java.util.concurrent.TimeUnit
1614

1715
actual val Firebase.functions

0 commit comments

Comments
 (0)