1
1
package com.airbnb.mvrx
2
2
3
+ import android.util.SparseArray
3
4
import androidx.collection.ArrayMap
4
5
import androidx.collection.LongSparseArray
5
6
import androidx.collection.SparseArrayCompat
6
- import android.util.SparseArray
7
+ import kotlin.reflect.KCallable
7
8
import kotlin.reflect.KClass
8
9
import kotlin.reflect.KMutableProperty
9
10
import kotlin.reflect.KProperty1
@@ -27,8 +28,9 @@ private const val IMMUTABLE_MAP_MESSAGE =
27
28
internal fun KClass <* >.assertImmutability () {
28
29
require(this .isData) { " MvRx state must be a data class!" }
29
30
30
- fun KProperty1 <* , * >.isSubtype (klass : KClass <* >) =
31
- returnType.isSubtypeOf(klass.starProjectedType)
31
+ fun KProperty1 <* , * >.isSubtype (vararg classes : KClass <* >): Boolean {
32
+ return classes.any { klass -> returnType.isSubtypeOf(klass.starProjectedType) }
33
+ }
32
34
33
35
this .declaredMemberProperties.forEach { prop ->
34
36
when {
@@ -37,9 +39,13 @@ internal fun KClass<*>.assertImmutability() {
37
39
prop.isSubtype(SparseArray ::class ) -> " You cannot use SparseArray for ${prop.name} .\n $IMMUTABLE_LIST_MESSAGE "
38
40
prop.isSubtype(LongSparseArray ::class ) -> " You cannot use LongSparseArray for ${prop.name} .\n $IMMUTABLE_LIST_MESSAGE "
39
41
prop.isSubtype(SparseArrayCompat ::class ) -> " You cannot use SparseArrayCompat for ${prop.name} .\n $IMMUTABLE_LIST_MESSAGE "
40
- prop.isSubtype(ArrayMap ::class ) -> " You cannot use ArrayMap for ${prop.name} .\n $IMMUTABLE_MAP_MESSAGE "
41
- prop.isSubtype(android.util.ArrayMap ::class ) -> " You cannot use ArrayMap for ${prop.name} .\n $IMMUTABLE_MAP_MESSAGE "
42
+ prop.isSubtype(ArrayMap ::class , android.util.ArrayMap ::class ) -> {
43
+ " You cannot use ArrayMap for ${prop.name} .\n $IMMUTABLE_MAP_MESSAGE "
44
+ }
42
45
prop.isSubtype(HashMap ::class ) -> " You cannot use HashMap for ${prop.name} .\n $IMMUTABLE_MAP_MESSAGE "
46
+ prop.isSubtype(Function ::class , KCallable ::class ) -> {
47
+ " You cannot use functions inside MvRx state. Only pure data should be represented: ${prop.name} "
48
+ }
43
49
else -> null
44
50
}?.let { throw IllegalArgumentException (it) }
45
51
}
0 commit comments