Skip to content

Commit

Permalink
Add @nullable to WritableMap and WritableArray for Android Java spec …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
John Wang authored and facebook-github-bot committed Apr 22, 2022
1 parent 6958bbb commit ffaa5d6
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,13 @@ function translateFunctionReturnTypeToJavaType(
return nullable ? 'Boolean' : 'boolean';
case 'ObjectTypeAnnotation':
imports.add('com.facebook.react.bridge.WritableMap');
return 'WritableMap';
return wrapIntoNullableIfNeeded('WritableMap');
case 'GenericObjectTypeAnnotation':
imports.add('com.facebook.react.bridge.WritableMap');
return 'WritableMap';
return wrapIntoNullableIfNeeded('WritableMap');
case 'ArrayTypeAnnotation':
imports.add('com.facebook.react.bridge.WritableArray');
return 'WritableArray';
return wrapIntoNullableIfNeeded('WritableArray');
default:
(realTypeAnnotation.type: empty);
throw new Error(createErrorMessage(realTypeAnnotation.type));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,49 @@ const COMPLEX_OBJECTS: SchemaType = {
],
},
},
{
name: 'getNullableObject',
optional: false,
typeAnnotation: {
type: 'FunctionTypeAnnotation',
returnTypeAnnotation: {
type: 'NullableTypeAnnotation',
typeAnnotation: {
type: 'ObjectTypeAnnotation',
properties: [],
},
},
params: [],
},
},
{
name: 'getNullableGenericObject',
optional: false,
typeAnnotation: {
type: 'FunctionTypeAnnotation',
returnTypeAnnotation: {
type: 'NullableTypeAnnotation',
typeAnnotation: {
type: 'GenericObjectTypeAnnotation',
},
},
params: [],
},
},
{
name: 'getNullableArray',
optional: false,
typeAnnotation: {
type: 'FunctionTypeAnnotation',
returnTypeAnnotation: {
type: 'NullableTypeAnnotation',
typeAnnotation: {
type: 'ArrayTypeAnnotation',
},
},
params: [],
},
},
],
},
moduleNames: ['SampleTurboModule'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,25 @@ static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getArrays(jsi
static_cast<NativeSampleTurboModuleCxxSpecJSI *>(&turboModule)->getArrays(rt, args[0].asObject(rt));
return jsi::Value::undefined();
}
static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getNullableObject(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
return static_cast<NativeSampleTurboModuleCxxSpecJSI *>(&turboModule)->getNullableObject(rt);
}
static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getNullableGenericObject(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
return static_cast<NativeSampleTurboModuleCxxSpecJSI *>(&turboModule)->getNullableGenericObject(rt);
}
static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getNullableArray(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
return static_cast<NativeSampleTurboModuleCxxSpecJSI *>(&turboModule)->getNullableArray(rt);
}
NativeSampleTurboModuleCxxSpecJSI::NativeSampleTurboModuleCxxSpecJSI(std::shared_ptr<CallInvoker> jsInvoker)
: TurboModule(\\"SampleTurboModule\\", jsInvoker) {
methodMap_[\\"difficult\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_difficult};
methodMap_[\\"optionals\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_optionals};
methodMap_[\\"optionalMethod\\"] = MethodMetadata {3, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_optionalMethod};
methodMap_[\\"getArrays\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getArrays};
methodMap_[\\"getNullableObject\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getNullableObject};
methodMap_[\\"getNullableGenericObject\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getNullableGenericObject};
methodMap_[\\"getNullableArray\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getNullableArray};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ public:
virtual void optionals(jsi::Runtime &rt, jsi::Object A) = 0;
virtual void optionalMethod(jsi::Runtime &rt, jsi::Object options, jsi::Function callback, jsi::Array extras) = 0;
virtual void getArrays(jsi::Runtime &rt, jsi::Object options) = 0;
virtual std::optional<jsi::Object> getNullableObject(jsi::Runtime &rt) = 0;
virtual std::optional<jsi::Object> getNullableGenericObject(jsi::Runtime &rt) = 0;
virtual std::optional<jsi::Array> getNullableArray(jsi::Runtime &rt) = 0;
};
Expand Down Expand Up @@ -142,6 +145,30 @@ private:
return bridging::callFromJs<void>(
rt, &T::getArrays, jsInvoker_, instance_, std::move(options));
}
std::optional<jsi::Object> getNullableObject(jsi::Runtime &rt) override {
static_assert(
bridging::getParameterCount(&T::getNullableObject) == 1,
\\"Expected getNullableObject(...) to have 1 parameters\\");
return bridging::callFromJs<std::optional<jsi::Object>>(
rt, &T::getNullableObject, jsInvoker_, instance_);
}
std::optional<jsi::Object> getNullableGenericObject(jsi::Runtime &rt) override {
static_assert(
bridging::getParameterCount(&T::getNullableGenericObject) == 1,
\\"Expected getNullableGenericObject(...) to have 1 parameters\\");
return bridging::callFromJs<std::optional<jsi::Object>>(
rt, &T::getNullableGenericObject, jsInvoker_, instance_);
}
std::optional<jsi::Array> getNullableArray(jsi::Runtime &rt) override {
static_assert(
bridging::getParameterCount(&T::getNullableArray) == 1,
\\"Expected getNullableArray(...) to have 1 parameters\\");
return bridging::callFromJs<std::optional<jsi::Array>>(
rt, &T::getNullableArray, jsInvoker_, instance_);
}
private:
T *instance_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ namespace JS {
callback:(RCTResponseSenderBlock)callback
extras:(NSArray *)extras;
- (void)getArrays:(JS::NativeSampleTurboModule::SpecGetArraysOptions &)options;
- (NSDictionary * _Nullable)getNullableObject;
- (NSDictionary * _Nullable)getNullableGenericObject;
- (NSArray<id<NSObject>> * _Nullable)getNullableArray;
@end
namespace facebook {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReactModuleWithSpec;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.turbomodule.core.interfaces.TurboModule;
import javax.annotation.Nullable;
public abstract class NativeSampleTurboModuleSpec extends ReactContextBaseJavaModule implements ReactModuleWithSpec, TurboModule {
public NativeSampleTurboModuleSpec(ReactApplicationContext reactContext) {
Expand All @@ -81,6 +83,18 @@ public abstract class NativeSampleTurboModuleSpec extends ReactContextBaseJavaMo
@ReactMethod
@DoNotStrip
public abstract void getArrays(ReadableMap options);
@ReactMethod(isBlockingSynchronousMethod = true)
@DoNotStrip
public abstract @Nullable WritableMap getNullableObject();
@ReactMethod(isBlockingSynchronousMethod = true)
@DoNotStrip
public abstract @Nullable WritableMap getNullableGenericObject();
@ReactMethod(isBlockingSynchronousMethod = true)
@DoNotStrip
public abstract @Nullable WritableArray getNullableArray();
}
",
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,27 @@ static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getArr
return static_cast<JavaTurboModule &>(turboModule).invokeJavaMethod(rt, VoidKind, \\"getArrays\\", \\"(Lcom/facebook/react/bridge/ReadableMap;)V\\", args, count);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableObject(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
return static_cast<JavaTurboModule &>(turboModule).invokeJavaMethod(rt, ObjectKind, \\"getNullableObject\\", \\"()Lcom/facebook/react/bridge/WritableMap;\\", args, count);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableGenericObject(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
return static_cast<JavaTurboModule &>(turboModule).invokeJavaMethod(rt, ObjectKind, \\"getNullableGenericObject\\", \\"()Lcom/facebook/react/bridge/WritableMap;\\", args, count);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableArray(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
return static_cast<JavaTurboModule &>(turboModule).invokeJavaMethod(rt, ArrayKind, \\"getNullableArray\\", \\"()Lcom/facebook/react/bridge/WritableArray;\\", args, count);
}
NativeSampleTurboModuleSpecJSI::NativeSampleTurboModuleSpecJSI(const JavaTurboModule::InitParams &params)
: JavaTurboModule(params) {
methodMap_[\\"difficult\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_difficult};
methodMap_[\\"optionals\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_optionals};
methodMap_[\\"optionalMethod\\"] = MethodMetadata {3, __hostFunction_NativeSampleTurboModuleSpecJSI_optionalMethod};
methodMap_[\\"getArrays\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_getArrays};
methodMap_[\\"getNullableObject\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableObject};
methodMap_[\\"getNullableGenericObject\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableGenericObject};
methodMap_[\\"getNullableArray\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableArray};
}
std::shared_ptr<TurboModule> complex_objects_ModuleProvider(const std::string moduleName, const JavaTurboModule::InitParams &params) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ namespace facebook {
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, VoidKind, \\"getArrays\\", @selector(getArrays:), args, count);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableObject(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, ObjectKind, \\"getNullableObject\\", @selector(getNullableObject), args, count);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableGenericObject(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, ObjectKind, \\"getNullableGenericObject\\", @selector(getNullableGenericObject), args, count);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableArray(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, ArrayKind, \\"getNullableArray\\", @selector(getNullableArray), args, count);
}
NativeSampleTurboModuleSpecJSI::NativeSampleTurboModuleSpecJSI(const ObjCTurboModule::InitParams &params)
: ObjCTurboModule(params) {
Expand All @@ -118,6 +130,15 @@ namespace facebook {
methodMap_[\\"getArrays\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_getArrays};
setMethodArgConversionSelector(@\\"getArrays\\", 0, @\\"JS_NativeSampleTurboModule_SpecGetArraysOptions:\\");
methodMap_[\\"getNullableObject\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableObject};
methodMap_[\\"getNullableGenericObject\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableGenericObject};
methodMap_[\\"getNullableArray\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableArray};
}
} // namespace react
} // namespace facebook
Expand Down

0 comments on commit ffaa5d6

Please sign in to comment.