Repository navigation
[Android] Add Kotlin collection factory functions for Maps and Arrays #54937
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
l2hyunwoo
wants to merge
1
commit into
react:main
Choose a base branch
from
l2hyunwoo:feat/mutable-collections-factory-function
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
...es/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/CollectionFactory.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| package com.facebook.react.bridge | ||
|
|
||
| /** | ||
| * Creates a [WritableMap] with the given key-value pairs. | ||
| * | ||
| * Example: | ||
| * ``` | ||
| * val map = writableMapOf( | ||
| * "name" to "Alice", | ||
| * "age" to 30, | ||
| * "active" to true | ||
| * ) | ||
| * ``` | ||
| * | ||
| * Supported value types: null, Boolean, Int, Long, Float, Double, String, ReadableMap, | ||
| * ReadableArray, WritableMap, WritableArray. | ||
| * | ||
| * @throws IllegalArgumentException if a value is of unsupported type | ||
| */ | ||
| public fun writableMapOf(vararg pairs: Pair<String, Any?>): WritableMap { | ||
| val map = Arguments.createMap() | ||
| for ((key, value) in pairs) { | ||
| putValue(map, key, value) | ||
| } | ||
| return map | ||
| } | ||
|
|
||
| private fun putValue(map: WritableMap, key: String, value: Any?) { | ||
| when (value) { | ||
| null -> map.putNull(key) | ||
| is Boolean -> map.putBoolean(key, value) | ||
| is Int -> map.putInt(key, value) | ||
| is Long -> map.putDouble(key, value.toDouble()) | ||
| is Float -> map.putDouble(key, value.toDouble()) | ||
| is Double -> map.putDouble(key, value) | ||
| is String -> map.putString(key, value) | ||
| is ReadableMap -> map.putMap(key, value) | ||
| is ReadableArray -> map.putArray(key, value) | ||
| else -> { | ||
| throw IllegalArgumentException( | ||
| "Unsupported value type: ${value.javaClass.name} for key '$key'" | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Creates a [WritableArray] with the given elements. | ||
| * | ||
| * Example: | ||
| * ``` | ||
| * val array = writableArrayOf("apple", "banana", "cherry") | ||
| * val mixed = writableArrayOf(1, "two", true, 4.0) | ||
| * ``` | ||
| * | ||
| * Supported element types: null, Boolean, Int, Long, Float, Double, String, ReadableMap, | ||
| * ReadableArray, WritableMap, WritableArray. | ||
| * | ||
| * @throws IllegalArgumentException if an element is of unsupported type | ||
| */ | ||
| public fun writableArrayOf(vararg elements: Any?): WritableArray { | ||
| val array = Arguments.createArray() | ||
| for (element in elements) { | ||
| pushValue(array, element) | ||
| } | ||
| return array | ||
| } | ||
|
|
||
| private fun pushValue(array: WritableArray, value: Any?) { | ||
| when (value) { | ||
| null -> array.pushNull() | ||
| is Boolean -> array.pushBoolean(value) | ||
| is Int -> array.pushInt(value) | ||
| is Long -> array.pushDouble(value.toDouble()) | ||
| is Float -> array.pushDouble(value.toDouble()) | ||
| is Double -> array.pushDouble(value) | ||
| is String -> array.pushString(value) | ||
| is ReadableMap -> array.pushMap(value) | ||
| is ReadableArray -> array.pushArray(value) | ||
| else -> { | ||
| throw IllegalArgumentException("Unsupported element type: ${value.javaClass.name}") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Creates a [ReadableMap] with the given key-value pairs. Returns an immutable view - mutations are | ||
| * not possible through the returned interface. | ||
| * | ||
| * Example: | ||
| * ``` | ||
| * val map = readableMapOf("key" to "value", "count" to 42) | ||
| * ``` | ||
| */ | ||
| public fun readableMapOf(vararg pairs: Pair<String, Any?>): ReadableMap = writableMapOf(*pairs) | ||
|
|
||
| /** | ||
| * Creates a [ReadableArray] with the given elements. Returns an immutable view - mutations are not | ||
| * possible through the returned interface. | ||
| * | ||
| * Example: | ||
| * ``` | ||
| * val array = readableArrayOf(1, 2, 3) | ||
| * ``` | ||
| */ | ||
| public fun readableArrayOf(vararg elements: Any?): ReadableArray = writableArrayOf(*elements) | ||
168 changes: 168 additions & 0 deletions
168
...eact-native/ReactAndroid/src/test/java/com/facebook/react/bridge/CollectionFactoryTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| package com.facebook.react.bridge | ||
|
|
||
| import com.facebook.testutils.shadows.ShadowArguments | ||
| import com.facebook.testutils.shadows.ShadowNativeArray | ||
| import com.facebook.testutils.shadows.ShadowNativeLoader | ||
| import com.facebook.testutils.shadows.ShadowNativeMap | ||
| import com.facebook.testutils.shadows.ShadowReadableNativeArray | ||
| import com.facebook.testutils.shadows.ShadowReadableNativeMap | ||
| import com.facebook.testutils.shadows.ShadowSoLoader | ||
| import com.facebook.testutils.shadows.ShadowWritableNativeArray | ||
| import com.facebook.testutils.shadows.ShadowWritableNativeMap | ||
| import org.assertj.core.api.Assertions.assertThat | ||
| import org.assertj.core.api.Assertions.assertThatThrownBy | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.RobolectricTestRunner | ||
| import org.robolectric.annotation.Config | ||
|
|
||
| @RunWith(RobolectricTestRunner::class) | ||
| @Config( | ||
| shadows = | ||
| [ | ||
| ShadowArguments::class, | ||
| ShadowSoLoader::class, | ||
| ShadowNativeLoader::class, | ||
| ShadowNativeArray::class, | ||
| ShadowNativeMap::class, | ||
| ShadowWritableNativeMap::class, | ||
| ShadowWritableNativeArray::class, | ||
| ShadowReadableNativeMap::class, | ||
| ShadowReadableNativeArray::class, | ||
| ] | ||
| ) | ||
| class CollectionFactoryTest { | ||
| @Test | ||
| fun `writableMapOf creates empty map`() { | ||
| val map = writableMapOf() | ||
| assertThat(map.toHashMap()).isEmpty() | ||
| } | ||
|
|
||
| @Test | ||
| fun `writableArrayOf creates empty array`() { | ||
| val array = writableArrayOf() | ||
| assertThat(array.toArrayList()).isEmpty() | ||
| } | ||
|
|
||
| @Test | ||
| fun `writableMapOf creates map with primitive values`() { | ||
| val map = writableMapOf( | ||
| "stringKey" to "stringValue", | ||
| "intKey" to 42, | ||
| "boolKey" to true, | ||
| "doubleKey" to 3.14 | ||
| ) | ||
|
|
||
| assertThat(map.toHashMap()) | ||
| .hasSize(4) | ||
| .containsEntry("stringKey", "stringValue") | ||
| .containsEntry("intKey", 42.0) | ||
| .containsEntry("boolKey", true) | ||
| .containsEntry("doubleKey", 3.14) | ||
| } | ||
|
|
||
| @Test | ||
| fun `writableArrayOf creates array with mixed types`() { | ||
| val array = writableArrayOf("one", 2, true, 4.0) | ||
|
|
||
| assertThat(array.toArrayList()).hasSize(4).containsExactly("one", 2.0, true, 4.0) | ||
| } | ||
|
|
||
| @Test | ||
| fun `writableMapOf handles null values correctly`() { | ||
| val map = writableMapOf("nullKey" to null) | ||
| assertThat(map.toHashMap()).containsEntry("nullKey", null) | ||
| } | ||
|
|
||
| @Test | ||
| fun `writableArrayOf handles null elements correctly`() { | ||
| val array = writableArrayOf("first", null, "third") | ||
| assertThat(array.toArrayList()).hasSize(3).containsExactly("first", null, "third") | ||
| } | ||
|
|
||
| @Test | ||
| fun `writableMapOf converts Long and Float to Double`() { | ||
| val map = writableMapOf("longKey" to 123L, "floatKey" to 4.5f) | ||
|
|
||
| assertThat(map.toHashMap()).containsEntry("longKey", 123.0).containsEntry("floatKey", 4.5) | ||
| } | ||
|
|
||
| @Test | ||
| fun `writableArrayOf converts Long and Float to Double`() { | ||
| val array = writableArrayOf(123L, 4.5f) | ||
|
|
||
| assertThat(array.toArrayList()).containsExactly(123.0, 4.5) | ||
| } | ||
|
|
||
| @Test | ||
| fun `writableMapOf supports nested maps`() { | ||
| val map = writableMapOf("nested" to writableMapOf("inner" to "value")) | ||
|
|
||
| val nested = map.getMap("nested") | ||
| checkNotNull(nested) | ||
| assertThat(nested.toHashMap()).containsEntry("inner", "value") | ||
| } | ||
|
|
||
| @Test | ||
| fun `writableMapOf supports nested arrays`() { | ||
| val map = writableMapOf("items" to writableArrayOf(1, 2, 3)) | ||
|
|
||
| val items = map.getArray("items") | ||
| checkNotNull(items) | ||
| assertThat(items.toArrayList()).containsExactly(1.0, 2.0, 3.0) | ||
| } | ||
|
|
||
| @Test | ||
| fun `writableArrayOf supports nested maps`() { | ||
| val array = writableArrayOf(writableMapOf("key" to "value")) | ||
|
|
||
| val nested = array.getMap(0) | ||
| checkNotNull(nested) | ||
| assertThat(nested.toHashMap()).containsEntry("key", "value") | ||
| } | ||
|
|
||
| @Test | ||
| fun `writableArrayOf supports nested arrays`() { | ||
| val array = writableArrayOf(writableArrayOf(1, 2)) | ||
|
|
||
| val nested = array.getArray(0) | ||
| assertThat(nested?.toArrayList()).containsExactly(1.0, 2.0) | ||
| } | ||
|
|
||
| @Test | ||
| fun `readableMapOf returns ReadableMap with correct content`() { | ||
| val map: ReadableMap = readableMapOf("key" to "value", "count" to 42) | ||
|
|
||
| assertThat(map.getString("key")).isEqualTo("value") | ||
| assertThat(map.getInt("count")).isEqualTo(42) | ||
| } | ||
|
|
||
| @Test | ||
| fun `readableArrayOf returns ReadableArray with correct content`() { | ||
| val array: ReadableArray = readableArrayOf(1, 2, 3) | ||
|
|
||
| assertThat(array.toArrayList()).containsExactly(1.0, 2.0, 3.0) | ||
| } | ||
|
|
||
| @Test | ||
| fun `writableMapOf throws on unsupported type`() { | ||
| assertThatThrownBy { writableMapOf("date" to java.util.Date()) } | ||
| .isInstanceOf(IllegalArgumentException::class.java) | ||
| .hasMessageContaining("Unsupported value type") | ||
| .hasMessageContaining("date") | ||
| } | ||
|
|
||
| @Test | ||
| fun `writableArrayOf throws on unsupported type`() { | ||
| assertThatThrownBy { writableArrayOf(java.util.Date()) } | ||
| .isInstanceOf(IllegalArgumentException::class.java) | ||
| .hasMessageContaining("Unsupported element type") | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to 78dbbaa you should put those inside
WritableArrayBuilderand so on