Skip to content

Commit 0314305

Browse files
elicwhitefacebook-github-bot
authored andcommitted
Support string command arguments
Summary: Support command arguments that are strings Reviewed By: JoshuaGross Differential Revision: D16509728 fbshipit-source-id: 003aba66231d204071d043c01cb0781150d0edb9
1 parent a174647 commit 0314305

9 files changed

+42
-11
lines changed

packages/react-native-codegen/src/CodegenSchema.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ export type CommandsFunctionTypeParamAnnotation = $ReadOnly<{|
2222

2323
export type CommandsTypeAnnotation =
2424
| BooleanTypeAnnotation
25-
| Int32TypeAnnotation;
25+
| Int32TypeAnnotation
26+
| StringTypeAnnotation;
2627

2728
export type BooleanTypeAnnotation = $ReadOnly<{|
2829
type: 'BooleanTypeAnnotation',
@@ -32,6 +33,10 @@ export type Int32TypeAnnotation = $ReadOnly<{|
3233
type: 'Int32TypeAnnotation',
3334
|}>;
3435

36+
export type StringTypeAnnotation = $ReadOnly<{|
37+
type: 'StringTypeAnnotation',
38+
|}>;
39+
3540
export type ObjectPropertyType =
3641
| $ReadOnly<{|
3742
type: 'BooleanTypeAnnotation',

packages/react-native-codegen/src/generators/components/GenerateComponentHObjCpp.js

+8
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ function getObjCParamType(param: CommandsFunctionTypeParamAnnotation): string {
107107
return 'BOOL';
108108
case 'Int32TypeAnnotation':
109109
return 'NSInteger';
110+
case 'StringTypeAnnotation':
111+
return 'NSString *';
110112
default:
111113
(param.typeAnnotation.type: empty);
112114
throw new Error('Received invalid param type annotation');
@@ -121,6 +123,8 @@ function getObjCExpectedKindParamType(
121123
return '[NSNumber class]';
122124
case 'Int32TypeAnnotation':
123125
return '[NSNumber class]';
126+
case 'StringTypeAnnotation':
127+
return '[NSString class]';
124128
default:
125129
(param.typeAnnotation.type: empty);
126130
throw new Error('Received invalid param type annotation');
@@ -135,6 +139,8 @@ function getReadableExpectedKindParamType(
135139
return 'boolean';
136140
case 'Int32TypeAnnotation':
137141
return 'number';
142+
case 'StringTypeAnnotation':
143+
return 'string';
138144
default:
139145
(param.typeAnnotation.type: empty);
140146
throw new Error('Received invalid param type annotation');
@@ -150,6 +156,8 @@ function getObjCRightHandAssignmentParamType(
150156
return `[(NSNumber *)arg${index} boolValue]`;
151157
case 'Int32TypeAnnotation':
152158
return `[(NSNumber *)arg${index} intValue]`;
159+
case 'StringTypeAnnotation':
160+
return `(NSString *)arg${index}`;
153161
default:
154162
(param.typeAnnotation.type: empty);
155163
throw new Error('Received invalid param type annotation');

packages/react-native-codegen/src/generators/components/GeneratePropsJavaDelegate.js

+2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ function getCommandArgJavaType(param) {
122122
return 'getBoolean';
123123
case 'Int32TypeAnnotation':
124124
return 'getInt';
125+
case 'StringTypeAnnotation':
126+
return 'getString';
125127
default:
126128
(param.typeAnnotation.type: empty);
127129
throw new Error('Receieved invalid typeAnnotation');

packages/react-native-codegen/src/generators/components/GeneratePropsJavaInterface.js

+2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ function getCommandArgJavaType(param) {
9797
return 'boolean';
9898
case 'Int32TypeAnnotation':
9999
return 'int';
100+
case 'StringTypeAnnotation':
101+
return 'String';
100102
default:
101103
(param.typeAnnotation.type: empty);
102104
throw new Error('Receieved invalid typeAnnotation');

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

+6
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,12 @@ const COMMANDS: SchemaType = {
941941
type: 'Int32TypeAnnotation',
942942
},
943943
},
944+
{
945+
name: 'message',
946+
typeAnnotation: {
947+
type: 'StringTypeAnnotation',
948+
},
949+
},
944950
{
945951
name: 'animated',
946952
typeAnnotation: {

packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GenerateComponentHObjCpp-test.js.snap

+14-6
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ NS_ASSUME_NONNULL_BEGIN
8686
8787
@protocol CommandNativeComponentViewProtocol <NSObject>
8888
- (void)flashScrollIndicators;
89-
- (void)allTypes:(NSInteger)x animated:(BOOL)animated;
89+
- (void)allTypes:(NSInteger)x message:(NSString *)message animated:(BOOL)animated;
9090
@end
9191
9292
RCT_EXTERN inline void CommandNativeComponentHandleCommand(
@@ -110,8 +110,8 @@ RCT_EXTERN inline void CommandNativeComponentHandleCommand(
110110
111111
if ([commandName isEqualToString:@\\"allTypes\\"]) {
112112
#if RCT_DEBUG
113-
if ([args count] != 2) {
114-
RCTLogError(@\\"%@ command %@ received %d arguments, expected %d.\\", @\\"CommandNativeComponent\\", commandName, (int)[args count], 2);
113+
if ([args count] != 3) {
114+
RCTLogError(@\\"%@ command %@ received %d arguments, expected %d.\\", @\\"CommandNativeComponent\\", commandName, (int)[args count], 3);
115115
return;
116116
}
117117
#endif
@@ -126,13 +126,21 @@ if ([commandName isEqualToString:@\\"allTypes\\"]) {
126126
127127
#if RCT_DEBUG
128128
NSObject *arg1 = args[1];
129-
if (!RCTValidateTypeOfViewCommandArgument(arg1, [NSNumber class], @\\"boolean\\", @\\"CommandNativeComponent\\", commandName, @\\"2nd\\")) {
129+
if (!RCTValidateTypeOfViewCommandArgument(arg1, [NSString class], @\\"string\\", @\\"CommandNativeComponent\\", commandName, @\\"2nd\\")) {
130+
return;
131+
}
132+
#endif
133+
NSString * message = (NSString *)arg1;
134+
135+
#if RCT_DEBUG
136+
NSObject *arg2 = args[2];
137+
if (!RCTValidateTypeOfViewCommandArgument(arg2, [NSNumber class], @\\"boolean\\", @\\"CommandNativeComponent\\", commandName, @\\"3rd\\")) {
130138
return;
131139
}
132140
#endif
133-
BOOL animated = [(NSNumber *)arg1 boolValue];
141+
BOOL animated = [(NSNumber *)arg2 boolValue];
134142
135-
[componentView allTypes:x animated:animated]
143+
[componentView allTypes:x message:message animated:animated]
136144
return;
137145
}
138146

packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GeneratePropsJavaDelegate-test.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public class CommandNativeComponentViewManagerDelegate<T extends View> {
100100
viewManager.flashScrollIndicators(view);
101101
break;
102102
case \\"allTypes\\":
103-
viewManager.allTypes(view, args.getInt(0), args.getBoolean(1));
103+
viewManager.allTypes(view, args.getInt(0), args.getString(1), args.getBoolean(2));
104104
break;
105105
}
106106
}

packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GeneratePropsJavaInterface-test.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ import android.view.View;
6262
public interface CommandNativeComponentViewManagerInterface<T extends View> {
6363
// No props
6464
void flashScrollIndicators(T view);
65-
void allTypes(T view, int x, boolean animated);
65+
void allTypes(T view, int x, String message, boolean animated);
6666
}
6767
",
6868
}

packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GenerateViewConfigJs-test.js.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,11 @@ export const Commands = {
152152
);
153153
},
154154
155-
allTypes(ref, x, animated) {
155+
allTypes(ref, x, message, animated) {
156156
UIManager.dispatchViewCommand(
157157
findNodeHandle(ref),
158158
UIManager.getViewManagerConfig(\\"CommandNativeComponent\\").Commands.allTypes,
159-
[x, animated]
159+
[x, message, animated]
160160
);
161161
}
162162
};

0 commit comments

Comments
 (0)