Skip to content

Commit

Permalink
Update Readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Daeda88 committed Jan 23, 2024
1 parent bf709a3 commit 3021502
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 18 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,16 @@ data class City(val name: String)
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:

```kotlin
db.collection("cities").document("LA").set(City.serializer(), city, encodeDefaults = true)
db.collection("cities").document("LA").set(City.serializer(), city) { shouldEncodeElementDefault = true }
```

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.
The `buildSettings` closure is optional and allows for configuring serialization behaviour.

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.
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`.

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)
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).
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`

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class EncodersTest {
@Test
fun encodeDecodeSealedClass() {
val sealedClass = SealedClass.Test("value")
val encoded = encode(SealedClass.serializer(), sealedClass, shouldEncodeElementDefault = true)
val encoded = encode(SealedClass.serializer(), sealedClass) { shouldEncodeElementDefault = true }

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package dev.gitlive.firebase

import kotlinx.serialization.descriptors.PolymorphicKind
import kotlinx.serialization.encoding.CompositeEncoder
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.StructureKind
import kotlin.collections.set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package dev.gitlive.firebase

import kotlinx.serialization.descriptors.PolymorphicKind
import kotlinx.serialization.encoding.CompositeEncoder
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.StructureKind
import kotlin.js.json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ class GeoPointTests {
fun encodeGeoPointObject() = runTest {
val geoPoint = GeoPoint(12.3, 45.6)
val item = TestDataWithGeoPoint("123", geoPoint)
val encoded = encodedAsMap(encode(item, shouldEncodeElementDefault = false))
val encoded = encodedAsMap(
encode<TestDataWithGeoPoint>(item) {
shouldEncodeElementDefault = false
}
)
assertEquals("123", encoded["uid"])
// check GeoPoint is encoded to a platform representation
assertEquals(geoPoint.nativeValue, encoded["location"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import dev.gitlive.firebase.nativeAssertEquals
import dev.gitlive.firebase.nativeMapOf
import dev.gitlive.firebase.runTest
import kotlinx.serialization.Serializable
import kotlin.test.*
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.DurationUnit

@Serializable
data class TestData(
Expand Down Expand Up @@ -41,7 +44,7 @@ class TimestampTests {
"updatedAt" to timestamp.nativeValue,
"deletedAt" to null
),
encode(item, shouldEncodeElementDefault = false)
encode<TestData>(item) { shouldEncodeElementDefault = false }
)
}

Expand All @@ -56,7 +59,7 @@ class TimestampTests {
"updatedAt" to FieldValue.serverTimestamp.nativeValue,
"deletedAt" to FieldValue.serverTimestamp.nativeValue
),
encode(item, shouldEncodeElementDefault = false)
encode<TestData>(item) { shouldEncodeElementDefault = false }
)
}

Expand Down Expand Up @@ -104,11 +107,10 @@ class TimestampTests {

@Test
fun serializers() = runTest {
//todo dont work in js due to use of reified type in firebaseSerializer - uncomment once switched to IR
// assertEquals(BaseTimestampSerializer, (Timestamp(0, 0) as BaseTimestamp).firebaseSerializer())
// assertEquals(BaseTimestampSerializer, (Timestamp.ServerTimestamp as BaseTimestamp).firebaseSerializer())
// assertEquals(TimestampSerializer, Timestamp(0, 0).firebaseSerializer())
// assertEquals(ServerTimestampSerializer, Timestamp.ServerTimestamp.firebaseSerializer())
assertEquals(BaseTimestampSerializer, (Timestamp(0, 0) as BaseTimestamp).firebaseSerializer())
assertEquals(BaseTimestampSerializer, (Timestamp.ServerTimestamp as BaseTimestamp).firebaseSerializer())
assertEquals(TimestampSerializer, Timestamp(0, 0).firebaseSerializer())
assertEquals(ServerTimestampSerializer, Timestamp.ServerTimestamp.firebaseSerializer())
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import dev.gitlive.firebase.DecodeSettings
import dev.gitlive.firebase.Firebase
import dev.gitlive.firebase.FirebaseApp
import dev.gitlive.firebase.decode
import dev.gitlive.firebase.encode
import kotlinx.coroutines.tasks.await
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.SerializationStrategy
import java.util.concurrent.TimeUnit

actual val Firebase.functions
Expand Down

0 comments on commit 3021502

Please sign in to comment.