forked from GitLiveApp/firebase-kotlin-sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEncodedObject.kt
29 lines (23 loc) · 937 Bytes
/
EncodedObject.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package dev.gitlive.firebase.internal
import kotlin.jvm.JvmInline
/**
* Platform specific object for storing encoded data that can be used for methods that explicitly require an object.
* 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.
*/
sealed interface EncodedObject
internal fun EncodedObject.getRaw(): Map<String, Any?> = when (this) {
is EncodedObjectImpl -> raw
}
@JvmInline
@PublishedApi
internal value class EncodedObjectImpl(val raw: Map<String, Any?>) : EncodedObject
@PublishedApi
internal expect fun Any.asNativeMap(): Map<*, *>?
@PublishedApi
internal fun Map<*, *>.asEncodedObject(): EncodedObject = map { (key, value) ->
if (key is String) {
key to value
} else {
throw IllegalArgumentException("Expected a String key but received $key")
}
}.toMap().let(::EncodedObjectImpl)