Skip to content

Commit cb33de8

Browse files
committed
refactor: converted DynamicFromObject from java to kotlin
1 parent ee85957 commit cb33de8

File tree

2 files changed

+85
-89
lines changed

2 files changed

+85
-89
lines changed

Diff for: packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/DynamicFromObject.java

-89
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.bridge
9+
10+
import androidx.annotation.Nullable
11+
import com.facebook.common.logging.FLog
12+
import com.facebook.react.common.ReactConstants
13+
14+
/** Implementation of Dynamic wrapping a ReadableArray. */
15+
class DynamicFromObject(@Nullable private var mObject: Any?) : Dynamic {
16+
17+
override fun recycle() {
18+
// Noop - nothing to recycle since there is no pooling
19+
}
20+
21+
override fun isNull(): Boolean {
22+
return mObject == null
23+
}
24+
25+
override fun asBoolean(): Boolean {
26+
if (mObject !is Boolean) {
27+
throw ClassCastException("Dynamic value from Object is not a boolean")
28+
}
29+
return mObject as Boolean
30+
}
31+
32+
override fun asDouble(): Double {
33+
if (mObject !is Number) {
34+
throw ClassCastException("Dynamic value from Object is not a number")
35+
}
36+
return (mObject as Number).toDouble()
37+
}
38+
39+
override fun asInt(): Int {
40+
if (mObject !is Number) {
41+
throw ClassCastException("Dynamic value from Object is not a number")
42+
}
43+
// Numbers from JS are always Doubles
44+
return (mObject as Double).toInt()
45+
}
46+
47+
override fun asString(): String {
48+
if (mObject !is String) {
49+
throw ClassCastException("Dynamic value from Object is not a string")
50+
}
51+
return mObject as String
52+
}
53+
54+
override fun asArray(): ReadableArray {
55+
if (mObject !is ReadableArray) {
56+
throw ClassCastException("Dynamic value from Object is not a ReadableArray")
57+
}
58+
return mObject as ReadableArray
59+
}
60+
61+
override fun asMap(): ReadableMap {
62+
if (mObject !is ReadableMap) {
63+
throw ClassCastException("Dynamic value from Object is not a ReadableMap")
64+
}
65+
return mObject as ReadableMap
66+
}
67+
68+
override fun getType(): ReadableType {
69+
return when {
70+
isNull() -> ReadableType.Null
71+
mObject is Boolean -> ReadableType.Boolean
72+
mObject is Number -> ReadableType.Number
73+
mObject is String -> ReadableType.String
74+
mObject is ReadableMap -> ReadableType.Map
75+
mObject is ReadableArray -> ReadableType.Array
76+
else -> {
77+
FLog.e(
78+
ReactConstants.TAG,
79+
"Unmapped object type " + (mObject?.javaClass?.name ?: "<NULL object>")
80+
)
81+
ReadableType.Null
82+
}
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)