Skip to content

Commit 5dc2e4b

Browse files
committed
Restricted access to creating an EncodedObject
1 parent 32ac02f commit 5dc2e4b

File tree

4 files changed

+15
-20
lines changed

4 files changed

+15
-20
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ import kotlinx.serialization.descriptors.StructureKind
1010
import java.lang.IllegalArgumentException
1111
import kotlin.collections.set
1212

13-
actual data class EncodedObject(actual val raw: Map<String, Any?>) : Map<String, Any?> by raw {
14-
actual companion object {
15-
actual val emptyEncodedObject: EncodedObject = EncodedObject(emptyMap())
16-
}
17-
}
13+
actual data class EncodedObject internal constructor(actual val raw: Map<String, Any?>) : Map<String, Any?> by raw
1814

1915
@PublishedApi
2016
internal actual fun List<Pair<String, Any?>>.asEncodedObject() = EncodedObject(toMap())

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,9 @@ import kotlinx.serialization.modules.SerializersModule
1515
* Platform specific object for storing encoded data that can be used for methods that explicitly require an object.
1616
* 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.
1717
*
18-
* Created using [encodeAsObject]
18+
* Created using [encodeAsObject]. It is not recommended to encode to this manually.
1919
*/
2020
expect class EncodedObject {
21-
companion object {
22-
val emptyEncodedObject: EncodedObject
23-
}
24-
2521
val raw: Map<String, Any?>
2622
}
2723

@@ -45,13 +41,24 @@ inline fun <reified T> encode(value: T, shouldEncodeElementDefault: Boolean): An
4541
inline fun <reified T> encode(value: T, buildSettings: EncodeSettings.Builder.() -> Unit = {}) =
4642
encode(value, EncodeSettings.BuilderImpl().apply(buildSettings).buildEncodeSettings())
4743

44+
/**
45+
* Encodes data as an [EncodedObject].
46+
* This is not recommended for manual use, but may be done by the library internally.
47+
* @throws IllegalArgumentException if [value] is not valid as an [EncodedObject] (e.g. not encodable in the form Map<String:Any?>
48+
*/
4849
inline fun <T : Any> encodeAsObject(strategy: SerializationStrategy<T>, value: T, buildSettings: EncodeSettings.Builder.() -> Unit = {}): EncodedObject {
4950
if (value is Map<*, *> && value.keys.any { it !is String }) {
5051
throw IllegalArgumentException("$value is a Map containing non-String keys. Must be of the form Map<String, Any?>")
5152
}
5253
val encoded = encode(strategy, value, buildSettings) ?: throw IllegalArgumentException("$value was encoded as null. Must be of the form Map<String, Any?>")
5354
return encoded.asNativeMap()?.asEncodedObject() ?: throw IllegalArgumentException("$value was encoded as ${encoded::class}. Must be of the form Map<String, Any?>")
5455
}
56+
57+
/**
58+
* Encodes data as an [EncodedObject].
59+
* This is not recommended for manual use, but may be done by the library internally.
60+
* @throws IllegalArgumentException if [value] is not valid as an [EncodedObject] (e.g. not encodable in the form Map<String:Any?>
61+
*/
5562
inline fun <reified T : Any> encodeAsObject(value: T, buildSettings: EncodeSettings.Builder.() -> Unit = {}): EncodedObject {
5663
if (value is Map<*, *> && value.keys.any { it !is String }) {
5764
throw IllegalArgumentException("$value is a Map containing non-String keys. Must be of the form Map<String, Any?>")

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ import kotlinx.serialization.descriptors.SerialDescriptor
99
import kotlinx.serialization.descriptors.StructureKind
1010
import kotlin.collections.set
1111

12-
actual data class EncodedObject(actual val raw: Map<String, Any?>) : Map<Any?, Any?> by raw.mapKeys({ (key, _) -> key as? Any }) {
13-
actual companion object {
14-
actual val emptyEncodedObject: EncodedObject = EncodedObject(emptyMap())
15-
}
16-
}
12+
actual data class EncodedObject internal constructor(actual val raw: Map<String, Any?>) : Map<Any?, Any?> by raw.mapKeys({ (key, _) -> key as? Any })
1713

1814
@PublishedApi
1915
internal actual fun List<Pair<String, Any?>>.asEncodedObject() = EncodedObject(toMap())

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ import kotlinx.serialization.descriptors.StructureKind
1010
import kotlin.js.Json
1111
import kotlin.js.json
1212

13-
actual data class EncodedObject(private val keyValues: List<Pair<String, Any?>>) {
14-
actual companion object {
15-
actual val emptyEncodedObject: EncodedObject = EncodedObject(emptyList())
16-
}
17-
13+
actual class EncodedObject internal constructor(private val keyValues: List<Pair<String, Any?>>) {
1814
actual val raw get() = keyValues.toMap()
1915
val json get() = json(*keyValues.toTypedArray())
2016
}

0 commit comments

Comments
 (0)