Skip to content

Commit a629f9a

Browse files
committed
moved EncodedData to internal
1 parent 9f7d3cb commit a629f9a

File tree

20 files changed

+126
-166
lines changed

20 files changed

+126
-166
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@file:JvmName("AndroidEncodedObject")
2+
package dev.gitlive.firebase.internal
3+
4+
val EncodedObject.android: Map<String, Any?> get() = raw
5+
6+
@PublishedApi
7+
internal actual fun Any.asNativeMap(): Map<*, *>? = this as? Map<*, *>

firebase-common-internal/src/androidMain/kotlin/dev/gitlive/firebase/internal/_encoders.kt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,11 @@
44

55
package dev.gitlive.firebase.internal
66

7-
import dev.gitlive.firebase.EncodedObject
87
import kotlinx.serialization.descriptors.PolymorphicKind
98
import kotlinx.serialization.descriptors.SerialDescriptor
109
import kotlinx.serialization.descriptors.StructureKind
1110
import kotlin.collections.set
1211

13-
@PublishedApi
14-
internal data class EncodedObjectImpl internal constructor(override val raw: Map<String, Any?>) : EncodedObject, Map<String, Any?> by raw
15-
16-
@PublishedApi
17-
internal actual fun Map<String, Any?>.asEncodedObject(): EncodedObject = EncodedObjectImpl(this)
18-
19-
@PublishedApi
20-
internal actual fun Any.asNativeMap(): Map<*, *>? = this as? Map<*, *>
21-
2212
actual fun FirebaseEncoder.structureEncoder(descriptor: SerialDescriptor): FirebaseCompositeEncoder = when(descriptor.kind) {
2313
StructureKind.LIST -> mutableListOf<Any?>()
2414
.also { value = it }
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package dev.gitlive.firebase.internal
2+
3+
import kotlin.jvm.JvmInline
4+
5+
/**
6+
* Platform specific object for storing encoded data that can be used for methods that explicitly require an object.
7+
* This is essentially a [Map] of [String] and [Any]? (as represented by [raw]) but since [encode] gives a platform specific value, this method wraps that.
8+
*/
9+
sealed interface EncodedObject {
10+
val raw: Map<String, Any?>
11+
}
12+
13+
@JvmInline
14+
@PublishedApi
15+
internal value class EncodedObjectImpl(override val raw: Map<String, Any?>) : EncodedObject
16+
17+
@PublishedApi
18+
internal expect fun Any.asNativeMap(): Map<*, *>?
19+
20+
@PublishedApi
21+
internal fun Map<*, *>.asEncodedObject(): EncodedObject = map { (key, value) ->
22+
if (key is String) {
23+
key to value
24+
} else {
25+
throw IllegalArgumentException("Expected a String key but received $key")
26+
}
27+
}.toMap().let(::EncodedObjectImpl)

firebase-common-internal/src/commonMain/kotlin/dev/gitlive/firebase/internal/encoders.kt

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package dev.gitlive.firebase.internal
66

77
import dev.gitlive.firebase.EncodeSettings
8-
import dev.gitlive.firebase.EncodedObject
98
import kotlinx.serialization.ExperimentalSerializationApi
109
import kotlinx.serialization.SerializationStrategy
1110
import kotlinx.serialization.descriptors.SerialDescriptor
@@ -74,21 +73,6 @@ internal inline fun <reified T> encode(value: T, encodeSettings: EncodeSettings)
7473
}.value
7574
}
7675

77-
@PublishedApi
78-
expect internal fun Any.asNativeMap(): Map<*, *>?
79-
80-
@PublishedApi
81-
internal fun Map<*, *>.asEncodedObject(): EncodedObject = map { (key, value) ->
82-
if (key is String) {
83-
key to value
84-
} else {
85-
throw IllegalArgumentException("Expected a String key but received $key")
86-
}
87-
}.toMap().asEncodedObject()
88-
89-
@PublishedApi
90-
internal expect fun Map<String, Any?>.asEncodedObject(): EncodedObject
91-
9276
/**
9377
* An extension which which serializer to use for value. Handy in updating fields by name or path
9478
* where using annotation is not possible
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package dev.gitlive.firebase.internal
2+
3+
val EncodedObject.ios: Map<Any?, *> get() = raw.mapKeys { (key, _) -> key }
4+
5+
@PublishedApi
6+
internal actual fun Any.asNativeMap(): Map<*, *>? = this as? Map<*, *>

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,11 @@
44

55
package dev.gitlive.firebase.internal
66

7-
import dev.gitlive.firebase.EncodedObject
87
import kotlinx.serialization.descriptors.PolymorphicKind
98
import kotlinx.serialization.descriptors.SerialDescriptor
109
import kotlinx.serialization.descriptors.StructureKind
1110
import kotlin.collections.set
1211

13-
@PublishedApi
14-
internal data class InternalEncodedObject internal constructor(
15-
override val raw: Map<String, Any?>
16-
) : EncodedObject, Map<Any?, Any?> by raw.mapKeys({ (key, _) -> key })
17-
18-
@PublishedApi
19-
internal actual fun Map<String, Any?>.asEncodedObject(): EncodedObject = InternalEncodedObject(this)
20-
21-
@PublishedApi
22-
internal actual fun Any.asNativeMap(): Map<*, *>? = this as? Map<*, *>
23-
2412
actual fun FirebaseEncoder.structureEncoder(descriptor: SerialDescriptor): FirebaseCompositeEncoder = when(descriptor.kind) {
2513
StructureKind.LIST -> encodeAsList()
2614
StructureKind.MAP -> mutableListOf<Any?>()
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package dev.gitlive.firebase.internal
2+
3+
import kotlin.js.Json
4+
import kotlin.js.json
5+
6+
val EncodedObject.js: Json get() = json(*raw.entries.map { (key, value) -> key to value }.toTypedArray())
7+
8+
@PublishedApi
9+
internal actual fun Any.asNativeMap(): Map<*, *>? {
10+
val json = when (this) {
11+
is Number -> null
12+
is Boolean -> null
13+
is String -> null
14+
is Map<*, *> -> {
15+
if (keys.all { it is String }) {
16+
this as Json
17+
} else {
18+
null
19+
}
20+
}
21+
is Collection<*> -> null
22+
is Array<*> -> null
23+
else -> {
24+
this as Json
25+
}
26+
} ?: return null
27+
val mutableMap = mutableMapOf<String, Any?>()
28+
for (key in js("Object").keys(json)) {
29+
mutableMap[key] = json[key]
30+
}
31+
return mutableMap.toMap()
32+
}

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

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,11 @@
44

55
package dev.gitlive.firebase.internal
66

7-
import dev.gitlive.firebase.EncodedObject
87
import kotlinx.serialization.descriptors.PolymorphicKind
98
import kotlinx.serialization.descriptors.SerialDescriptor
109
import kotlinx.serialization.descriptors.StructureKind
11-
import kotlin.js.Json
1210
import kotlin.js.json
1311

14-
@PublishedApi
15-
internal class InternalEncodedObject internal constructor(override val raw: Map<String, Any?>) :
16-
EncodedObject {
17-
override val json: Json get() = json(*raw.entries.map { (key, value) -> key to value }.toTypedArray())
18-
}
19-
20-
@PublishedApi
21-
internal actual fun Map<String, Any?>.asEncodedObject(): EncodedObject = InternalEncodedObject(this)
22-
23-
@PublishedApi
24-
internal actual fun Any.asNativeMap(): Map<*, *>? {
25-
val json = when (this) {
26-
is Number -> null
27-
is Boolean -> null
28-
is String -> null
29-
is Map<*, *> -> {
30-
if (keys.all { it is String }) {
31-
this as Json
32-
} else {
33-
null
34-
}
35-
}
36-
is Collection<*> -> null
37-
is Array<*> -> null
38-
else -> {
39-
this as Json
40-
}
41-
} ?: return null
42-
val mutableMap = mutableMapOf<String, Any?>()
43-
for (key in js("Object").keys(json)) {
44-
mutableMap[key] = json[key]
45-
}
46-
return mutableMap.toMap()
47-
}
48-
4912
actual fun FirebaseEncoder.structureEncoder(descriptor: SerialDescriptor): FirebaseCompositeEncoder = when(descriptor.kind) {
5013
StructureKind.LIST -> encodeAsList(descriptor)
5114
StructureKind.MAP -> {

firebase-common/src/androidMain/kotlin/dev/gitlive/firebase/EncodedObject.kt

Lines changed: 0 additions & 9 deletions
This file was deleted.

firebase-common/src/commonMain/kotlin/dev/gitlive/firebase/EncodedObject.kt

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)