Skip to content

Commit 825e1c0

Browse files
JoshuaGrossfacebook-github-bot
authored andcommitted
Commands: support Float arguments
Summary: Support codegen'ing commands with Float arguments. Reviewed By: mdvacca Differential Revision: D16785534 fbshipit-source-id: 8174ae40762c1114b87a023cb2b69b2515dc6e23
1 parent 724fe11 commit 825e1c0

File tree

12 files changed

+56
-13
lines changed

12 files changed

+56
-13
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@ export type CommandsFunctionTypeParamAnnotation = $ReadOnly<{|
2323
export type CommandsTypeAnnotation =
2424
| BooleanTypeAnnotation
2525
| Int32TypeAnnotation
26+
| FloatTypeAnnotation
2627
| StringTypeAnnotation;
2728

29+
export type FloatTypeAnnotation = $ReadOnly<{|
30+
type: 'FloatTypeAnnotation',
31+
|}>;
32+
2833
export type BooleanTypeAnnotation = $ReadOnly<{|
2934
type: 'BooleanTypeAnnotation',
3035
|}>;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ function getObjCParamType(param: CommandsFunctionTypeParamAnnotation): string {
105105
switch (param.typeAnnotation.type) {
106106
case 'BooleanTypeAnnotation':
107107
return 'BOOL';
108+
case 'FloatTypeAnnotation':
109+
return 'float';
108110
case 'Int32TypeAnnotation':
109111
return 'NSInteger';
110112
case 'StringTypeAnnotation':
@@ -121,6 +123,8 @@ function getObjCExpectedKindParamType(
121123
switch (param.typeAnnotation.type) {
122124
case 'BooleanTypeAnnotation':
123125
return '[NSNumber class]';
126+
case 'FloatTypeAnnotation':
127+
return '[NSNumber class]';
124128
case 'Int32TypeAnnotation':
125129
return '[NSNumber class]';
126130
case 'StringTypeAnnotation':
@@ -137,6 +141,8 @@ function getReadableExpectedKindParamType(
137141
switch (param.typeAnnotation.type) {
138142
case 'BooleanTypeAnnotation':
139143
return 'boolean';
144+
case 'FloatTypeAnnotation':
145+
return 'float';
140146
case 'Int32TypeAnnotation':
141147
return 'number';
142148
case 'StringTypeAnnotation':
@@ -154,6 +160,8 @@ function getObjCRightHandAssignmentParamType(
154160
switch (param.typeAnnotation.type) {
155161
case 'BooleanTypeAnnotation':
156162
return `[(NSNumber *)arg${index} boolValue]`;
163+
case 'FloatTypeAnnotation':
164+
return `[(NSNumber *)arg${index} floatValue]`;
157165
case 'Int32TypeAnnotation':
158166
return `[(NSNumber *)arg${index} intValue]`;
159167
case 'StringTypeAnnotation':

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ function getCommandArgJavaType(param) {
129129
switch (param.typeAnnotation.type) {
130130
case 'BooleanTypeAnnotation':
131131
return 'getBoolean';
132+
case 'FloatTypeAnnotation':
133+
return 'getFloat';
132134
case 'Int32TypeAnnotation':
133135
return 'getInt';
134136
case 'StringTypeAnnotation':

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ function getCommandArgJavaType(param) {
9999
switch (param.typeAnnotation.type) {
100100
case 'BooleanTypeAnnotation':
101101
return 'boolean';
102+
case 'FloatTypeAnnotation':
103+
return 'float';
102104
case 'Int32TypeAnnotation':
103105
return 'int';
104106
case 'StringTypeAnnotation':

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,12 @@ const COMMANDS: SchemaType = {
10681068
type: 'Int32TypeAnnotation',
10691069
},
10701070
},
1071+
{
1072+
name: 'y',
1073+
typeAnnotation: {
1074+
type: 'FloatTypeAnnotation',
1075+
},
1076+
},
10711077
{
10721078
name: 'message',
10731079
typeAnnotation: {

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ NS_ASSUME_NONNULL_BEGIN
8686
8787
@protocol RCTCommandNativeComponentViewProtocol <NSObject>
8888
- (void)flashScrollIndicators;
89-
- (void)allTypes:(NSInteger)x message:(NSString *)message animated:(BOOL)animated;
89+
- (void)allTypes:(NSInteger)x y:(float)y message:(NSString *)message animated:(BOOL)animated;
9090
@end
9191
9292
RCT_EXTERN inline void RCTCommandNativeComponentHandleCommand(
@@ -110,8 +110,8 @@ RCT_EXTERN inline void RCTCommandNativeComponentHandleCommand(
110110
111111
if ([commandName isEqualToString:@\\"allTypes\\"]) {
112112
#if RCT_DEBUG
113-
if ([args count] != 3) {
114-
RCTLogError(@\\"%@ command %@ received %d arguments, expected %d.\\", @\\"CommandNativeComponent\\", commandName, (int)[args count], 3);
113+
if ([args count] != 4) {
114+
RCTLogError(@\\"%@ command %@ received %d arguments, expected %d.\\", @\\"CommandNativeComponent\\", commandName, (int)[args count], 4);
115115
return;
116116
}
117117
#endif
@@ -126,21 +126,29 @@ if ([commandName isEqualToString:@\\"allTypes\\"]) {
126126
127127
#if RCT_DEBUG
128128
NSObject *arg1 = args[1];
129-
if (!RCTValidateTypeOfViewCommandArgument(arg1, [NSString class], @\\"string\\", @\\"CommandNativeComponent\\", commandName, @\\"2nd\\")) {
129+
if (!RCTValidateTypeOfViewCommandArgument(arg1, [NSNumber class], @\\"float\\", @\\"CommandNativeComponent\\", commandName, @\\"2nd\\")) {
130130
return;
131131
}
132132
#endif
133-
NSString * message = (NSString *)arg1;
133+
float y = [(NSNumber *)arg1 floatValue];
134134
135135
#if RCT_DEBUG
136136
NSObject *arg2 = args[2];
137-
if (!RCTValidateTypeOfViewCommandArgument(arg2, [NSNumber class], @\\"boolean\\", @\\"CommandNativeComponent\\", commandName, @\\"3rd\\")) {
137+
if (!RCTValidateTypeOfViewCommandArgument(arg2, [NSString class], @\\"string\\", @\\"CommandNativeComponent\\", commandName, @\\"3rd\\")) {
138138
return;
139139
}
140140
#endif
141-
BOOL animated = [(NSNumber *)arg2 boolValue];
141+
NSString * message = (NSString *)arg2;
142142
143-
[componentView allTypes:x message:message animated:animated]
143+
#if RCT_DEBUG
144+
NSObject *arg3 = args[3];
145+
if (!RCTValidateTypeOfViewCommandArgument(arg3, [NSNumber class], @\\"boolean\\", @\\"CommandNativeComponent\\", commandName, @\\"4th\\")) {
146+
return;
147+
}
148+
#endif
149+
BOOL animated = [(NSNumber *)arg3 boolValue];
150+
151+
[componentView allTypes:x y:y message:message animated:animated]
144152
return;
145153
}
146154

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public class CommandNativeComponentManagerDelegate<T extends View, U extends Bas
138138
viewManager.flashScrollIndicators(view);
139139
break;
140140
case \\"allTypes\\":
141-
viewManager.allTypes(view, args.getInt(0), args.getString(1), args.getBoolean(2));
141+
viewManager.allTypes(view, args.getInt(0), args.getFloat(1), args.getString(2), args.getBoolean(3));
142142
break;
143143
}
144144
}

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

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ export const Commands = {
147147
dispatchCommand(ref, \\"flashScrollIndicators\\", []);
148148
},
149149
150-
allTypes(ref, x, message, animated) {
151-
dispatchCommand(ref, \\"allTypes\\", [x, message, animated]);
150+
allTypes(ref, x, y, message, animated) {
151+
dispatchCommand(ref, \\"allTypes\\", [x, y, message, animated]);
152152
}
153153
};
154154
",

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,14 +670,15 @@ const COMMANDS_DEFINED_WITH_ALL_TYPES = `
670670
const codegenNativeCommands = require('codegenNativeCommands');
671671
const codegenNativeComponent = require('codegenNativeComponent');
672672
673-
import type {Int32} from 'CodegenTypes';
673+
import type {Int32, Float} from 'CodegenTypes';
674674
import type {ViewProps} from 'ViewPropTypes';
675675
import type {NativeComponent} from 'codegenNativeComponent';
676676
677677
interface NativeCommands {
678678
+hotspotUpdate: (viewRef: React.Ref<'RCTView'>, x: Int32, y: Int32) => void;
679679
+scrollTo: (
680680
viewRef: React.Ref<'RCTView'>,
681+
x: Float,
681682
y: Int32,
682683
animated: boolean,
683684
) => void;

0 commit comments

Comments
 (0)