From 0314305e1202e48c74091e15da8574f1b92ce441 Mon Sep 17 00:00:00 2001 From: Eli White Date: Mon, 29 Jul 2019 14:35:08 -0700 Subject: [PATCH] Support string command arguments Summary: Support command arguments that are strings Reviewed By: JoshuaGross Differential Revision: D16509728 fbshipit-source-id: 003aba66231d204071d043c01cb0781150d0edb9 --- .../react-native-codegen/src/CodegenSchema.js | 7 ++++++- .../components/GenerateComponentHObjCpp.js | 8 ++++++++ .../components/GeneratePropsJavaDelegate.js | 2 ++ .../components/GeneratePropsJavaInterface.js | 2 ++ .../components/__test_fixtures__/fixtures.js | 6 ++++++ .../GenerateComponentHObjCpp-test.js.snap | 20 +++++++++++++------ .../GeneratePropsJavaDelegate-test.js.snap | 2 +- .../GeneratePropsJavaInterface-test.js.snap | 2 +- .../GenerateViewConfigJs-test.js.snap | 4 ++-- 9 files changed, 42 insertions(+), 11 deletions(-) diff --git a/packages/react-native-codegen/src/CodegenSchema.js b/packages/react-native-codegen/src/CodegenSchema.js index c66dc81b9b1..8067ac5e65f 100644 --- a/packages/react-native-codegen/src/CodegenSchema.js +++ b/packages/react-native-codegen/src/CodegenSchema.js @@ -22,7 +22,8 @@ export type CommandsFunctionTypeParamAnnotation = $ReadOnly<{| export type CommandsTypeAnnotation = | BooleanTypeAnnotation - | Int32TypeAnnotation; + | Int32TypeAnnotation + | StringTypeAnnotation; export type BooleanTypeAnnotation = $ReadOnly<{| type: 'BooleanTypeAnnotation', @@ -32,6 +33,10 @@ export type Int32TypeAnnotation = $ReadOnly<{| type: 'Int32TypeAnnotation', |}>; +export type StringTypeAnnotation = $ReadOnly<{| + type: 'StringTypeAnnotation', +|}>; + export type ObjectPropertyType = | $ReadOnly<{| type: 'BooleanTypeAnnotation', diff --git a/packages/react-native-codegen/src/generators/components/GenerateComponentHObjCpp.js b/packages/react-native-codegen/src/generators/components/GenerateComponentHObjCpp.js index 1e920a80ad1..1f31c396d58 100644 --- a/packages/react-native-codegen/src/generators/components/GenerateComponentHObjCpp.js +++ b/packages/react-native-codegen/src/generators/components/GenerateComponentHObjCpp.js @@ -107,6 +107,8 @@ function getObjCParamType(param: CommandsFunctionTypeParamAnnotation): string { return 'BOOL'; case 'Int32TypeAnnotation': return 'NSInteger'; + case 'StringTypeAnnotation': + return 'NSString *'; default: (param.typeAnnotation.type: empty); throw new Error('Received invalid param type annotation'); @@ -121,6 +123,8 @@ function getObjCExpectedKindParamType( return '[NSNumber class]'; case 'Int32TypeAnnotation': return '[NSNumber class]'; + case 'StringTypeAnnotation': + return '[NSString class]'; default: (param.typeAnnotation.type: empty); throw new Error('Received invalid param type annotation'); @@ -135,6 +139,8 @@ function getReadableExpectedKindParamType( return 'boolean'; case 'Int32TypeAnnotation': return 'number'; + case 'StringTypeAnnotation': + return 'string'; default: (param.typeAnnotation.type: empty); throw new Error('Received invalid param type annotation'); @@ -150,6 +156,8 @@ function getObjCRightHandAssignmentParamType( return `[(NSNumber *)arg${index} boolValue]`; case 'Int32TypeAnnotation': return `[(NSNumber *)arg${index} intValue]`; + case 'StringTypeAnnotation': + return `(NSString *)arg${index}`; default: (param.typeAnnotation.type: empty); throw new Error('Received invalid param type annotation'); diff --git a/packages/react-native-codegen/src/generators/components/GeneratePropsJavaDelegate.js b/packages/react-native-codegen/src/generators/components/GeneratePropsJavaDelegate.js index 955686b8b87..ad16a1f614b 100644 --- a/packages/react-native-codegen/src/generators/components/GeneratePropsJavaDelegate.js +++ b/packages/react-native-codegen/src/generators/components/GeneratePropsJavaDelegate.js @@ -122,6 +122,8 @@ function getCommandArgJavaType(param) { return 'getBoolean'; case 'Int32TypeAnnotation': return 'getInt'; + case 'StringTypeAnnotation': + return 'getString'; default: (param.typeAnnotation.type: empty); throw new Error('Receieved invalid typeAnnotation'); diff --git a/packages/react-native-codegen/src/generators/components/GeneratePropsJavaInterface.js b/packages/react-native-codegen/src/generators/components/GeneratePropsJavaInterface.js index e662477c08b..27846e460a4 100644 --- a/packages/react-native-codegen/src/generators/components/GeneratePropsJavaInterface.js +++ b/packages/react-native-codegen/src/generators/components/GeneratePropsJavaInterface.js @@ -97,6 +97,8 @@ function getCommandArgJavaType(param) { return 'boolean'; case 'Int32TypeAnnotation': return 'int'; + case 'StringTypeAnnotation': + return 'String'; default: (param.typeAnnotation.type: empty); throw new Error('Receieved invalid typeAnnotation'); diff --git a/packages/react-native-codegen/src/generators/components/__test_fixtures__/fixtures.js b/packages/react-native-codegen/src/generators/components/__test_fixtures__/fixtures.js index 84a9ffaf289..5cdbb151059 100644 --- a/packages/react-native-codegen/src/generators/components/__test_fixtures__/fixtures.js +++ b/packages/react-native-codegen/src/generators/components/__test_fixtures__/fixtures.js @@ -941,6 +941,12 @@ const COMMANDS: SchemaType = { type: 'Int32TypeAnnotation', }, }, + { + name: 'message', + typeAnnotation: { + type: 'StringTypeAnnotation', + }, + }, { name: 'animated', typeAnnotation: { diff --git a/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GenerateComponentHObjCpp-test.js.snap b/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GenerateComponentHObjCpp-test.js.snap index 03707d2a32d..21825524674 100644 --- a/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GenerateComponentHObjCpp-test.js.snap +++ b/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GenerateComponentHObjCpp-test.js.snap @@ -86,7 +86,7 @@ NS_ASSUME_NONNULL_BEGIN @protocol CommandNativeComponentViewProtocol - (void)flashScrollIndicators; -- (void)allTypes:(NSInteger)x animated:(BOOL)animated; +- (void)allTypes:(NSInteger)x message:(NSString *)message animated:(BOOL)animated; @end RCT_EXTERN inline void CommandNativeComponentHandleCommand( @@ -110,8 +110,8 @@ RCT_EXTERN inline void CommandNativeComponentHandleCommand( if ([commandName isEqualToString:@\\"allTypes\\"]) { #if RCT_DEBUG - if ([args count] != 2) { - RCTLogError(@\\"%@ command %@ received %d arguments, expected %d.\\", @\\"CommandNativeComponent\\", commandName, (int)[args count], 2); + if ([args count] != 3) { + RCTLogError(@\\"%@ command %@ received %d arguments, expected %d.\\", @\\"CommandNativeComponent\\", commandName, (int)[args count], 3); return; } #endif @@ -126,13 +126,21 @@ if ([commandName isEqualToString:@\\"allTypes\\"]) { #if RCT_DEBUG NSObject *arg1 = args[1]; - if (!RCTValidateTypeOfViewCommandArgument(arg1, [NSNumber class], @\\"boolean\\", @\\"CommandNativeComponent\\", commandName, @\\"2nd\\")) { + if (!RCTValidateTypeOfViewCommandArgument(arg1, [NSString class], @\\"string\\", @\\"CommandNativeComponent\\", commandName, @\\"2nd\\")) { return; } #endif - BOOL animated = [(NSNumber *)arg1 boolValue]; + NSString * message = (NSString *)arg1; - [componentView allTypes:x animated:animated] +#if RCT_DEBUG + NSObject *arg2 = args[2]; + if (!RCTValidateTypeOfViewCommandArgument(arg2, [NSNumber class], @\\"boolean\\", @\\"CommandNativeComponent\\", commandName, @\\"3rd\\")) { + return; + } +#endif + BOOL animated = [(NSNumber *)arg2 boolValue]; + + [componentView allTypes:x message:message animated:animated] return; } diff --git a/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GeneratePropsJavaDelegate-test.js.snap b/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GeneratePropsJavaDelegate-test.js.snap index 13d5a9bd046..ee6971107d2 100644 --- a/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GeneratePropsJavaDelegate-test.js.snap +++ b/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GeneratePropsJavaDelegate-test.js.snap @@ -100,7 +100,7 @@ public class CommandNativeComponentViewManagerDelegate { viewManager.flashScrollIndicators(view); break; case \\"allTypes\\": - viewManager.allTypes(view, args.getInt(0), args.getBoolean(1)); + viewManager.allTypes(view, args.getInt(0), args.getString(1), args.getBoolean(2)); break; } } diff --git a/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GeneratePropsJavaInterface-test.js.snap b/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GeneratePropsJavaInterface-test.js.snap index cc7b4feca83..4a51dd66ee9 100644 --- a/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GeneratePropsJavaInterface-test.js.snap +++ b/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GeneratePropsJavaInterface-test.js.snap @@ -62,7 +62,7 @@ import android.view.View; public interface CommandNativeComponentViewManagerInterface { // No props void flashScrollIndicators(T view); - void allTypes(T view, int x, boolean animated); + void allTypes(T view, int x, String message, boolean animated); } ", } diff --git a/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GenerateViewConfigJs-test.js.snap b/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GenerateViewConfigJs-test.js.snap index b4897e9f697..4591b8137da 100644 --- a/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GenerateViewConfigJs-test.js.snap +++ b/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GenerateViewConfigJs-test.js.snap @@ -152,11 +152,11 @@ export const Commands = { ); }, - allTypes(ref, x, animated) { + allTypes(ref, x, message, animated) { UIManager.dispatchViewCommand( findNodeHandle(ref), UIManager.getViewManagerConfig(\\"CommandNativeComponent\\").Commands.allTypes, - [x, animated] + [x, message, animated] ); } };