-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛✨ fix firestore timestamp decoding by adding a DataNormalizer
- Loading branch information
Showing
12 changed files
with
155 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
common/src/main/kotlin/de/sipgate/federmappe/common/decoder/DataNormalizer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package de.sipgate.federmappe.common.decoder | ||
|
||
interface DataNormalizer { | ||
fun normalize(data: Map<String, Any?>): Map<String, Any?> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
firestore/src/main/java/de/sipgate/federmappe/firestore/FirestoreTimestampDataNormalizer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package de.sipgate.federmappe.firestore | ||
|
||
import de.sipgate.federmappe.common.decoder.DataNormalizer | ||
|
||
class FirestoreTimestampDataNormalizer : DataNormalizer { | ||
override fun normalize(data: Map<String, Any?>): Map<String, Any?> = | ||
data.normalizeStringMapNullable() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 0 additions & 35 deletions
35
firestore/src/testDebug/kotlin/FirestoreTimestampToDecodableTimestampTest.kt
This file was deleted.
Oops, something went wrong.
79 changes: 79 additions & 0 deletions
79
...ebug/kotlin/de/sipgate/federmappe/firestore/FirestoreTimestampToDecodableTimestampTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package de.sipgate.federmappe.firestore.types.de.sipgate.federmappe.firestore | ||
|
||
import com.google.firebase.Timestamp | ||
import de.sipgate.federmappe.common.decoder.StringMapToObjectDecoder | ||
import de.sipgate.federmappe.firestore.FirestoreTimestampDataNormalizer | ||
import de.sipgate.federmappe.firestore.types.toDecodableTimestamp | ||
import kotlinx.datetime.Instant | ||
import kotlinx.datetime.serializers.InstantComponentSerializer | ||
import kotlinx.datetime.toJavaInstant | ||
import kotlinx.serialization.Contextual | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.modules.SerializersModule | ||
import kotlinx.serialization.modules.contextual | ||
import kotlinx.serialization.serializer | ||
import java.util.Date | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertIs | ||
|
||
class FirestoreTimestampToDecodableTimestampTest { | ||
|
||
@Test | ||
fun timestampWithNanosecondPrecisionIsConvertedSuccessfully() { | ||
val expectedInstant = Instant.fromEpochSeconds(1716823455, 854) | ||
|
||
val timestamp = Timestamp(expectedInstant.toJavaInstant()) | ||
|
||
val result = timestamp.toDecodableTimestamp() | ||
assertEquals(expectedInstant.epochSeconds, result["epochSeconds"]) | ||
assertEquals(expectedInstant.nanosecondsOfSecond.toLong(), result["nanosecondsOfSecond"]) | ||
} | ||
|
||
@Test | ||
fun timestampWithSecondPrecisionIsConvertedSuccessfully() { | ||
val expectedDate = Date.from(Instant.fromEpochSeconds(1716823455).toJavaInstant()) | ||
val expectedEpochSeconds = expectedDate.time / 1000 | ||
|
||
val timestamp = Timestamp(expectedDate) | ||
|
||
val result = timestamp.toDecodableTimestamp() | ||
|
||
assertEquals(expectedEpochSeconds, result["epochSeconds"]) | ||
assertEquals(0L, result["nanosecondsOfSecond"]) | ||
} | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
@Test | ||
fun firestoreTimestampIsDecodedCorrectly() { | ||
// Arrange | ||
val expectedInstant = Instant.fromEpochSeconds(1716823455) | ||
val expectedDate = Date.from(expectedInstant.toJavaInstant()) | ||
val timestamp = Timestamp(expectedDate) | ||
|
||
@Serializable | ||
data class MockLocalDataClass( | ||
@Contextual | ||
val createdAt: Instant | ||
) | ||
|
||
val serializer = serializer<MockLocalDataClass>() | ||
|
||
val data = mapOf<String, Any?>("createdAt" to timestamp) | ||
|
||
// Act | ||
val result = | ||
serializer.deserialize( | ||
StringMapToObjectDecoder( | ||
data = data, | ||
serializersModule = SerializersModule { contextual(InstantComponentSerializer) }, | ||
dataNormalizer = FirestoreTimestampDataNormalizer() | ||
), | ||
) | ||
|
||
// Assert | ||
assertEquals(expectedInstant, result.createdAt) | ||
assertIs<MockLocalDataClass>(result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
realtimedb/src/main/kotlin/de/sipgate/federmappe/realtimedb/DummyDataNormalizer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package de.sipgate.federmappe.realtimedb | ||
|
||
import de.sipgate.federmappe.common.decoder.DataNormalizer | ||
|
||
class DummyDataNormalizer : DataNormalizer { | ||
override fun normalize(data: Map<String, Any?>): Map<String, Any?> = data | ||
} |