Skip to content

Commit ffaa5d6

Browse files
John Wangfacebook-github-bot
John Wang
authored andcommitted
Add @nullable to WritableMap and WritableArray for Android Java spec generator
Summary: We're adding a native module that returns a Nullable type. This makes the Java spec and the implementation to be the same. Changelog: [Android][Added] - Generate `Nullable` for optional objects and arrays in module codegen. Reviewed By: yungsters Differential Revision: D35651333 fbshipit-source-id: d73c87340e33cf79831915ce6892e457ef369175
1 parent 6958bbb commit ffaa5d6

8 files changed

+138
-3
lines changed

packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,13 @@ function translateFunctionReturnTypeToJavaType(
212212
return nullable ? 'Boolean' : 'boolean';
213213
case 'ObjectTypeAnnotation':
214214
imports.add('com.facebook.react.bridge.WritableMap');
215-
return 'WritableMap';
215+
return wrapIntoNullableIfNeeded('WritableMap');
216216
case 'GenericObjectTypeAnnotation':
217217
imports.add('com.facebook.react.bridge.WritableMap');
218-
return 'WritableMap';
218+
return wrapIntoNullableIfNeeded('WritableMap');
219219
case 'ArrayTypeAnnotation':
220220
imports.add('com.facebook.react.bridge.WritableArray');
221-
return 'WritableArray';
221+
return wrapIntoNullableIfNeeded('WritableArray');
222222
default:
223223
(realTypeAnnotation.type: empty);
224224
throw new Error(createErrorMessage(realTypeAnnotation.type));

packages/react-native-codegen/src/generators/modules/__test_fixtures__/fixtures.js

+43
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,49 @@ const COMPLEX_OBJECTS: SchemaType = {
655655
],
656656
},
657657
},
658+
{
659+
name: 'getNullableObject',
660+
optional: false,
661+
typeAnnotation: {
662+
type: 'FunctionTypeAnnotation',
663+
returnTypeAnnotation: {
664+
type: 'NullableTypeAnnotation',
665+
typeAnnotation: {
666+
type: 'ObjectTypeAnnotation',
667+
properties: [],
668+
},
669+
},
670+
params: [],
671+
},
672+
},
673+
{
674+
name: 'getNullableGenericObject',
675+
optional: false,
676+
typeAnnotation: {
677+
type: 'FunctionTypeAnnotation',
678+
returnTypeAnnotation: {
679+
type: 'NullableTypeAnnotation',
680+
typeAnnotation: {
681+
type: 'GenericObjectTypeAnnotation',
682+
},
683+
},
684+
params: [],
685+
},
686+
},
687+
{
688+
name: 'getNullableArray',
689+
optional: false,
690+
typeAnnotation: {
691+
type: 'FunctionTypeAnnotation',
692+
returnTypeAnnotation: {
693+
type: 'NullableTypeAnnotation',
694+
typeAnnotation: {
695+
type: 'ArrayTypeAnnotation',
696+
},
697+
},
698+
params: [],
699+
},
700+
},
658701
],
659702
},
660703
moduleNames: ['SampleTurboModule'],

packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleCpp-test.js.snap

+12
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,25 @@ static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getArrays(jsi
6161
static_cast<NativeSampleTurboModuleCxxSpecJSI *>(&turboModule)->getArrays(rt, args[0].asObject(rt));
6262
return jsi::Value::undefined();
6363
}
64+
static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getNullableObject(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
65+
return static_cast<NativeSampleTurboModuleCxxSpecJSI *>(&turboModule)->getNullableObject(rt);
66+
}
67+
static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getNullableGenericObject(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
68+
return static_cast<NativeSampleTurboModuleCxxSpecJSI *>(&turboModule)->getNullableGenericObject(rt);
69+
}
70+
static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getNullableArray(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
71+
return static_cast<NativeSampleTurboModuleCxxSpecJSI *>(&turboModule)->getNullableArray(rt);
72+
}
6473
6574
NativeSampleTurboModuleCxxSpecJSI::NativeSampleTurboModuleCxxSpecJSI(std::shared_ptr<CallInvoker> jsInvoker)
6675
: TurboModule(\\"SampleTurboModule\\", jsInvoker) {
6776
methodMap_[\\"difficult\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_difficult};
6877
methodMap_[\\"optionals\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_optionals};
6978
methodMap_[\\"optionalMethod\\"] = MethodMetadata {3, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_optionalMethod};
7079
methodMap_[\\"getArrays\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getArrays};
80+
methodMap_[\\"getNullableObject\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getNullableObject};
81+
methodMap_[\\"getNullableGenericObject\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getNullableGenericObject};
82+
methodMap_[\\"getNullableArray\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getNullableArray};
7183
}
7284
7385

packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleH-test.js.snap

+27
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ public:
8989
virtual void optionals(jsi::Runtime &rt, jsi::Object A) = 0;
9090
virtual void optionalMethod(jsi::Runtime &rt, jsi::Object options, jsi::Function callback, jsi::Array extras) = 0;
9191
virtual void getArrays(jsi::Runtime &rt, jsi::Object options) = 0;
92+
virtual std::optional<jsi::Object> getNullableObject(jsi::Runtime &rt) = 0;
93+
virtual std::optional<jsi::Object> getNullableGenericObject(jsi::Runtime &rt) = 0;
94+
virtual std::optional<jsi::Array> getNullableArray(jsi::Runtime &rt) = 0;
9295
9396
};
9497
@@ -142,6 +145,30 @@ private:
142145
return bridging::callFromJs<void>(
143146
rt, &T::getArrays, jsInvoker_, instance_, std::move(options));
144147
}
148+
std::optional<jsi::Object> getNullableObject(jsi::Runtime &rt) override {
149+
static_assert(
150+
bridging::getParameterCount(&T::getNullableObject) == 1,
151+
\\"Expected getNullableObject(...) to have 1 parameters\\");
152+
153+
return bridging::callFromJs<std::optional<jsi::Object>>(
154+
rt, &T::getNullableObject, jsInvoker_, instance_);
155+
}
156+
std::optional<jsi::Object> getNullableGenericObject(jsi::Runtime &rt) override {
157+
static_assert(
158+
bridging::getParameterCount(&T::getNullableGenericObject) == 1,
159+
\\"Expected getNullableGenericObject(...) to have 1 parameters\\");
160+
161+
return bridging::callFromJs<std::optional<jsi::Object>>(
162+
rt, &T::getNullableGenericObject, jsInvoker_, instance_);
163+
}
164+
std::optional<jsi::Array> getNullableArray(jsi::Runtime &rt) override {
165+
static_assert(
166+
bridging::getParameterCount(&T::getNullableArray) == 1,
167+
\\"Expected getNullableArray(...) to have 1 parameters\\");
168+
169+
return bridging::callFromJs<std::optional<jsi::Array>>(
170+
rt, &T::getNullableArray, jsInvoker_, instance_);
171+
}
145172
146173
private:
147174
T *instance_;

packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleHObjCpp-test.js.snap

+3
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ namespace JS {
192192
callback:(RCTResponseSenderBlock)callback
193193
extras:(NSArray *)extras;
194194
- (void)getArrays:(JS::NativeSampleTurboModule::SpecGetArraysOptions &)options;
195+
- (NSDictionary * _Nullable)getNullableObject;
196+
- (NSDictionary * _Nullable)getNullableGenericObject;
197+
- (NSArray<id<NSObject>> * _Nullable)getNullableArray;
195198
196199
@end
197200
namespace facebook {

packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleJavaSpec-test.js.snap

+14
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ import com.facebook.react.bridge.ReactMethod;
5858
import com.facebook.react.bridge.ReactModuleWithSpec;
5959
import com.facebook.react.bridge.ReadableArray;
6060
import com.facebook.react.bridge.ReadableMap;
61+
import com.facebook.react.bridge.WritableArray;
6162
import com.facebook.react.bridge.WritableMap;
6263
import com.facebook.react.turbomodule.core.interfaces.TurboModule;
64+
import javax.annotation.Nullable;
6365
6466
public abstract class NativeSampleTurboModuleSpec extends ReactContextBaseJavaModule implements ReactModuleWithSpec, TurboModule {
6567
public NativeSampleTurboModuleSpec(ReactApplicationContext reactContext) {
@@ -81,6 +83,18 @@ public abstract class NativeSampleTurboModuleSpec extends ReactContextBaseJavaMo
8183
@ReactMethod
8284
@DoNotStrip
8385
public abstract void getArrays(ReadableMap options);
86+
87+
@ReactMethod(isBlockingSynchronousMethod = true)
88+
@DoNotStrip
89+
public abstract @Nullable WritableMap getNullableObject();
90+
91+
@ReactMethod(isBlockingSynchronousMethod = true)
92+
@DoNotStrip
93+
public abstract @Nullable WritableMap getNullableGenericObject();
94+
95+
@ReactMethod(isBlockingSynchronousMethod = true)
96+
@DoNotStrip
97+
public abstract @Nullable WritableArray getNullableArray();
8498
}
8599
",
86100
}

packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleJniCpp-test.js.snap

+15
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,27 @@ static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getArr
7070
return static_cast<JavaTurboModule &>(turboModule).invokeJavaMethod(rt, VoidKind, \\"getArrays\\", \\"(Lcom/facebook/react/bridge/ReadableMap;)V\\", args, count);
7171
}
7272
73+
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableObject(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
74+
return static_cast<JavaTurboModule &>(turboModule).invokeJavaMethod(rt, ObjectKind, \\"getNullableObject\\", \\"()Lcom/facebook/react/bridge/WritableMap;\\", args, count);
75+
}
76+
77+
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableGenericObject(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
78+
return static_cast<JavaTurboModule &>(turboModule).invokeJavaMethod(rt, ObjectKind, \\"getNullableGenericObject\\", \\"()Lcom/facebook/react/bridge/WritableMap;\\", args, count);
79+
}
80+
81+
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableArray(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
82+
return static_cast<JavaTurboModule &>(turboModule).invokeJavaMethod(rt, ArrayKind, \\"getNullableArray\\", \\"()Lcom/facebook/react/bridge/WritableArray;\\", args, count);
83+
}
84+
7385
NativeSampleTurboModuleSpecJSI::NativeSampleTurboModuleSpecJSI(const JavaTurboModule::InitParams &params)
7486
: JavaTurboModule(params) {
7587
methodMap_[\\"difficult\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_difficult};
7688
methodMap_[\\"optionals\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_optionals};
7789
methodMap_[\\"optionalMethod\\"] = MethodMetadata {3, __hostFunction_NativeSampleTurboModuleSpecJSI_optionalMethod};
7890
methodMap_[\\"getArrays\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_getArrays};
91+
methodMap_[\\"getNullableObject\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableObject};
92+
methodMap_[\\"getNullableGenericObject\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableGenericObject};
93+
methodMap_[\\"getNullableArray\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableArray};
7994
}
8095
8196
std::shared_ptr<TurboModule> complex_objects_ModuleProvider(const std::string moduleName, const JavaTurboModule::InitParams &params) {

packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleMm-test.js.snap

+21
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,18 @@ namespace facebook {
104104
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, VoidKind, \\"getArrays\\", @selector(getArrays:), args, count);
105105
}
106106
107+
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableObject(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
108+
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, ObjectKind, \\"getNullableObject\\", @selector(getNullableObject), args, count);
109+
}
110+
111+
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableGenericObject(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
112+
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, ObjectKind, \\"getNullableGenericObject\\", @selector(getNullableGenericObject), args, count);
113+
}
114+
115+
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableArray(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
116+
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, ArrayKind, \\"getNullableArray\\", @selector(getNullableArray), args, count);
117+
}
118+
107119
NativeSampleTurboModuleSpecJSI::NativeSampleTurboModuleSpecJSI(const ObjCTurboModule::InitParams &params)
108120
: ObjCTurboModule(params) {
109121
@@ -118,6 +130,15 @@ namespace facebook {
118130
119131
methodMap_[\\"getArrays\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_getArrays};
120132
setMethodArgConversionSelector(@\\"getArrays\\", 0, @\\"JS_NativeSampleTurboModule_SpecGetArraysOptions:\\");
133+
134+
methodMap_[\\"getNullableObject\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableObject};
135+
136+
137+
methodMap_[\\"getNullableGenericObject\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableGenericObject};
138+
139+
140+
methodMap_[\\"getNullableArray\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableArray};
141+
121142
}
122143
} // namespace react
123144
} // namespace facebook

0 commit comments

Comments
 (0)