Skip to content

Commit

Permalink
react-native-code-gen Add Enum Type support for iOS/Android TurboModules
Browse files Browse the repository at this point in the history
Summary:
Adding enum support to Android/iOS generated code

Changelog:
[General][Added] - react-native-code-gen Add Enum Type support for iOS/Android TurboModules

Reviewed By: javache

Differential Revision: D38967241

fbshipit-source-id: d8bb3019dab198e16905a6422e877cd751119122
  • Loading branch information
christophpurrer authored and facebook-github-bot committed Oct 25, 2022
1 parent e9b89b5 commit 745f3ee
Show file tree
Hide file tree
Showing 19 changed files with 347 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ function translateFunctionParamToJavaType(
return !isRequired ? 'Double' : 'double';
case 'BooleanTypeAnnotation':
return !isRequired ? 'Boolean' : 'boolean';
case 'EnumDeclaration':
switch (realTypeAnnotation.memberType) {
case 'NumberTypeAnnotation':
return !isRequired ? 'Double' : 'double';
case 'StringTypeAnnotation':
return wrapIntoNullableIfNeeded('String');
default:
throw new Error(createErrorMessage(realTypeAnnotation.type));
}
case 'ObjectTypeAnnotation':
imports.add('com.facebook.react.bridge.ReadableMap');
if (typeAnnotation.type === 'TypeAliasTypeAnnotation') {
Expand Down Expand Up @@ -213,6 +222,15 @@ function translateFunctionReturnTypeToJavaType(
return nullable ? 'Double' : 'double';
case 'BooleanTypeAnnotation':
return nullable ? 'Boolean' : 'boolean';
case 'EnumDeclaration':
switch (realTypeAnnotation.memberType) {
case 'NumberTypeAnnotation':
return nullable ? 'Double' : 'double';
case 'StringTypeAnnotation':
return wrapIntoNullableIfNeeded('String');
default:
throw new Error(createErrorMessage(realTypeAnnotation.type));
}
case 'ObjectTypeAnnotation':
imports.add('com.facebook.react.bridge.WritableMap');
return wrapIntoNullableIfNeeded('WritableMap');
Expand Down Expand Up @@ -269,6 +287,15 @@ function getFalsyReturnStatementFromReturnType(
return nullable ? 'return null;' : 'return 0;';
case 'BooleanTypeAnnotation':
return nullable ? 'return null;' : 'return false;';
case 'EnumDeclaration':
switch (realTypeAnnotation.memberType) {
case 'NumberTypeAnnotation':
return nullable ? 'return null;' : 'return 0;';
case 'StringTypeAnnotation':
return nullable ? 'return null;' : 'return "";';
default:
throw new Error(createErrorMessage(realTypeAnnotation.type));
}
case 'StringTypeAnnotation':
return nullable ? 'return null;' : 'return "";';
case 'ObjectTypeAnnotation':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ function translateReturnTypeToKind(
return 'StringKind';
case 'BooleanTypeAnnotation':
return 'BooleanKind';
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
return 'NumberKind';
case 'StringTypeAnnotation':
return 'StringKind';
default:
throw new Error(
`Unknown enum prop type for returning value, found: ${realTypeAnnotation.type}"`,
);
}
case 'NumberTypeAnnotation':
return 'NumberKind';
case 'DoubleTypeAnnotation':
Expand Down Expand Up @@ -212,6 +223,17 @@ function translateParamTypeToJniType(
return 'Ljava/lang/String;';
case 'BooleanTypeAnnotation':
return !isRequired ? 'Ljava/lang/Boolean;' : 'Z';
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
return !isRequired ? 'Ljava/lang/Double;' : 'D';
case 'StringTypeAnnotation':
return 'Ljava/lang/String;';
default:
throw new Error(
`Unknown enum prop type for method arg, found: ${realTypeAnnotation.type}"`,
);
}
case 'NumberTypeAnnotation':
return !isRequired ? 'Ljava/lang/Double;' : 'D';
case 'DoubleTypeAnnotation':
Expand Down Expand Up @@ -267,6 +289,17 @@ function translateReturnTypeToJniType(
return 'Ljava/lang/String;';
case 'BooleanTypeAnnotation':
return nullable ? 'Ljava/lang/Boolean;' : 'Z';
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
return nullable ? 'Ljava/lang/Double;' : 'D';
case 'StringTypeAnnotation':
return 'Ljava/lang/String;';
default:
throw new Error(
`Unknown enum prop type for method return type, found: ${realTypeAnnotation.type}"`,
);
}
case 'NumberTypeAnnotation':
return nullable ? 'Ljava/lang/Double;' : 'D';
case 'DoubleTypeAnnotation':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
NativeModuleDoubleTypeAnnotation,
NativeModuleFloatTypeAnnotation,
NativeModuleBooleanTypeAnnotation,
NativeModuleEnumDeclaration,
NativeModuleGenericObjectTypeAnnotation,
ReservedTypeAnnotation,
NativeModuleTypeAliasTypeAnnotation,
Expand Down Expand Up @@ -63,6 +64,7 @@ export type StructTypeAnnotation =
| NativeModuleDoubleTypeAnnotation
| NativeModuleFloatTypeAnnotation
| NativeModuleBooleanTypeAnnotation
| NativeModuleEnumDeclaration
| NativeModuleGenericObjectTypeAnnotation
| ReservedTypeAnnotation
| NativeModuleTypeAliasTypeAnnotation
Expand Down Expand Up @@ -113,7 +115,7 @@ class StructCollector {
return wrapNullable(nullable, typeAnnotation);
}
case 'EnumDeclaration':
throw new Error('Enum types are unsupported in structs');
return wrapNullable(nullable, typeAnnotation);
case 'MixedTypeAnnotation':
throw new Error('Mixed types are unsupported in structs');
case 'UnionTypeAnnotation':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ function toObjCType(
return wrapOptional('double');
case 'BooleanTypeAnnotation':
return wrapOptional('bool');
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
return wrapOptional('double');
case 'StringTypeAnnotation':
return 'NSString *';
default:
throw new Error(
`Couldn't convert enum into ObjC type: ${typeAnnotation.type}"`,
);
}
case 'GenericObjectTypeAnnotation':
return isRequired ? 'id<NSObject> ' : 'id<NSObject> _Nullable ';
case 'ArrayTypeAnnotation':
Expand Down Expand Up @@ -171,6 +182,17 @@ function toObjCValue(
return wrapPrimitive('double');
case 'BooleanTypeAnnotation':
return wrapPrimitive('BOOL');
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
return wrapPrimitive('double');
case 'StringTypeAnnotation':
return value;
default:
throw new Error(
`Couldn't convert enum into ObjC value: ${typeAnnotation.type}"`,
);
}
case 'GenericObjectTypeAnnotation':
return value;
case 'ArrayTypeAnnotation':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ function toObjCType(
return wrapOptional('double');
case 'BooleanTypeAnnotation':
return wrapOptional('bool');
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
return wrapOptional('double');
case 'StringTypeAnnotation':
return 'NSString *';
default:
throw new Error(
`Couldn't convert enum into ObjC type: ${typeAnnotation.type}"`,
);
}
case 'GenericObjectTypeAnnotation':
return isRequired ? 'id<NSObject> ' : 'id<NSObject> _Nullable';
case 'ArrayTypeAnnotation':
Expand Down Expand Up @@ -161,6 +172,17 @@ function toObjCValue(
return RCTBridgingTo('Double');
case 'BooleanTypeAnnotation':
return RCTBridgingTo('Bool');
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
return RCTBridgingTo('Double');
case 'StringTypeAnnotation':
return RCTBridgingTo('String');
default:
throw new Error(
`Couldn't convert enum into ObjC value: ${typeAnnotation.type}"`,
);
}
case 'GenericObjectTypeAnnotation':
return value;
case 'ArrayTypeAnnotation':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,17 @@ function getParamObjCType(
return notStruct(notRequired ? 'NSNumber *' : 'double');
case 'BooleanTypeAnnotation':
return notStruct(notRequired ? 'NSNumber *' : 'BOOL');
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
return notStruct(notRequired ? 'NSNumber *' : 'double');
case 'StringTypeAnnotation':
return notStruct(wrapIntoNullableIfNeeded('NSString *'));
default:
throw new Error(
`Unsupported enum type for param "${paramName}" in ${methodName}. Found: ${typeAnnotation.type}`,
);
}
case 'GenericObjectTypeAnnotation':
return notStruct(wrapIntoNullableIfNeeded('NSDictionary *'));
default:
Expand Down Expand Up @@ -335,6 +346,17 @@ function getReturnObjCType(
return wrapIntoNullableIfNeeded('NSNumber *');
case 'BooleanTypeAnnotation':
return wrapIntoNullableIfNeeded('NSNumber *');
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
return wrapIntoNullableIfNeeded('NSNumber *');
case 'StringTypeAnnotation':
return wrapIntoNullableIfNeeded('NSString *');
default:
throw new Error(
`Unsupported enum return type for ${methodName}. Found: ${typeAnnotation.type}`,
);
}
case 'GenericObjectTypeAnnotation':
return wrapIntoNullableIfNeeded('NSDictionary *');
default:
Expand Down Expand Up @@ -380,6 +402,17 @@ function getReturnJSType(
return 'BooleanKind';
case 'GenericObjectTypeAnnotation':
return 'ObjectKind';
case 'EnumDeclaration':
switch (typeAnnotation.memberType) {
case 'NumberTypeAnnotation':
return 'NumberKind';
case 'StringTypeAnnotation':
return 'StringKind';
default:
throw new Error(
`Unsupported return type for ${methodName}. Found: ${typeAnnotation.type}`,
);
}
default:
(typeAnnotation.type:
| 'EnumDeclaration'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,42 @@ const SIMPLE_NATIVE_MODULES: SchemaType = {
],
},
},
{
name: 'getEnums',
optional: false,
typeAnnotation: {
type: 'FunctionTypeAnnotation',
returnTypeAnnotation: {
type: 'StringTypeAnnotation',
},
params: [
{
name: 'enumInt',
optional: false,
typeAnnotation: {
type: 'EnumDeclaration',
memberType: 'NumberTypeAnnotation',
},
},
{
name: 'enumFloat',
optional: false,
typeAnnotation: {
type: 'EnumDeclaration',
memberType: 'NumberTypeAnnotation',
},
},
{
name: 'enumString',
optional: false,
typeAnnotation: {
type: 'EnumDeclaration',
memberType: 'StringTypeAnnotation',
},
},
],
},
},
],
},
moduleNames: ['SampleTurboModule'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getValueWithC
static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getValueWithPromise(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
return static_cast<NativeSampleTurboModuleCxxSpecJSI *>(&turboModule)->getValueWithPromise(rt, args[0].asBool());
}
static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getEnums(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
return static_cast<NativeSampleTurboModuleCxxSpecJSI *>(&turboModule)->getEnums(rt, args[0].asNumber(), args[1].asNumber(), args[2].asString(rt));
}
NativeSampleTurboModuleCxxSpecJSI::NativeSampleTurboModuleCxxSpecJSI(std::shared_ptr<CallInvoker> jsInvoker)
: TurboModule(\\"SampleTurboModule\\", jsInvoker) {
Expand All @@ -351,6 +354,7 @@ NativeSampleTurboModuleCxxSpecJSI::NativeSampleTurboModuleCxxSpecJSI(std::shared
methodMap_[\\"getValue\\"] = MethodMetadata {3, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getValue};
methodMap_[\\"getValueWithCallback\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getValueWithCallback};
methodMap_[\\"getValueWithPromise\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getValueWithPromise};
methodMap_[\\"getEnums\\"] = MethodMetadata {3, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getEnums};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,7 @@ public:
virtual jsi::Object getValue(jsi::Runtime &rt, double x, jsi::String y, jsi::Object z) = 0;
virtual void getValueWithCallback(jsi::Runtime &rt, jsi::Function callback) = 0;
virtual jsi::Value getValueWithPromise(jsi::Runtime &rt, bool error) = 0;
virtual jsi::String getEnums(jsi::Runtime &rt, double enumInt, double enumFloat, jsi::String enumString) = 0;
};
Expand Down Expand Up @@ -777,6 +778,14 @@ private:
return bridging::callFromJs<jsi::Value>(
rt, &T::getValueWithPromise, jsInvoker_, instance_, std::move(error));
}
jsi::String getEnums(jsi::Runtime &rt, double enumInt, double enumFloat, jsi::String enumString) override {
static_assert(
bridging::getParameterCount(&T::getEnums) == 4,
\\"Expected getEnums(...) to have 4 parameters\\");
return bridging::callFromJs<jsi::String>(
rt, &T::getEnums, jsInvoker_, instance_, std::move(enumInt), std::move(enumFloat), std::move(enumString));
}
private:
T *instance_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,9 @@ namespace JS {
- (void)getValueWithPromise:(BOOL)error
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject;
- (NSString *)getEnums:(double)enumInt
enumFloat:(double)enumFloat
enumString:(NSString *)enumString;
- (facebook::react::ModuleConstants<JS::NativeSampleTurboModule::Constants::Builder>)constantsToExport;
- (facebook::react::ModuleConstants<JS::NativeSampleTurboModule::Constants::Builder>)getConstants;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,10 @@ public abstract class NativeSampleTurboModuleSpec extends ReactContextBaseJavaMo
@ReactMethod
@DoNotStrip
public abstract void getValueWithPromise(boolean error, Promise promise);
@ReactMethod(isBlockingSynchronousMethod = true)
@DoNotStrip
public abstract String getEnums(double enumInt, double enumFloat, String enumString);
}
",
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,11 @@ static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getVal
return static_cast<JavaTurboModule &>(turboModule).invokeJavaMethod(rt, PromiseKind, \\"getValueWithPromise\\", \\"(ZLcom/facebook/react/bridge/Promise;)V\\", args, count, cachedMethodId);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getEnums(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
static jmethodID cachedMethodId = nullptr;
return static_cast<JavaTurboModule &>(turboModule).invokeJavaMethod(rt, StringKind, \\"getEnums\\", \\"(DDLjava/lang/String;)Ljava/lang/String;\\", args, count, cachedMethodId);
}
NativeSampleTurboModuleSpecJSI::NativeSampleTurboModuleSpecJSI(const JavaTurboModule::InitParams &params)
: JavaTurboModule(params) {
methodMap_[\\"getConstants\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleSpecJSI_getConstants};
Expand All @@ -397,6 +402,7 @@ NativeSampleTurboModuleSpecJSI::NativeSampleTurboModuleSpecJSI(const JavaTurboMo
methodMap_[\\"getValue\\"] = MethodMetadata {3, __hostFunction_NativeSampleTurboModuleSpecJSI_getValue};
methodMap_[\\"getValueWithCallback\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_getValueWithCallback};
methodMap_[\\"getValueWithPromise\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_getValueWithPromise};
methodMap_[\\"getEnums\\"] = MethodMetadata {3, __hostFunction_NativeSampleTurboModuleSpecJSI_getEnums};
}
std::shared_ptr<TurboModule> simple_native_modules_ModuleProvider(const std::string &moduleName, const JavaTurboModule::InitParams &params) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,10 @@ namespace facebook {
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, PromiseKind, \\"getValueWithPromise\\", @selector(getValueWithPromise:resolve:reject:), args, count);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getEnums(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, StringKind, \\"getEnums\\", @selector(getEnums:enumFloat:enumString:), args, count);
}
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getConstants(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, ObjectKind, \\"getConstants\\", @selector(getConstants), args, count);
}
Expand Down Expand Up @@ -490,6 +494,9 @@ namespace facebook {
methodMap_[\\"getValueWithPromise\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_getValueWithPromise};
methodMap_[\\"getEnums\\"] = MethodMetadata {3, __hostFunction_NativeSampleTurboModuleSpecJSI_getEnums};
methodMap_[\\"getConstants\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleSpecJSI_getConstants};
}
Expand Down
Loading

0 comments on commit 745f3ee

Please sign in to comment.