Accept all types for objects' and arrays' elements

Summary:
Previously we accepted only very limited set of types for schema parser. However, in many cases we want to provide more specific typings e.g. accept enum or touple.

Currently, we don't take any advantages for codegen from specifying type of elements for object or array. All of them fallback to the same cpp code.

That's why I decided not to throw exception if types of arrays' and objects' elements are different than currently supported. Then I want to fallback to `undefined`.

Reviewed By: rickhanlonii

Differential Revision: D16325028

fbshipit-source-id: 3d7990ca0207c31f0ed522e7316a9cb17b6b1bcb
This commit is contained in:
Michał Osadnik
2019-07-17 06:21:51 -07:00
committed by Facebook Github Bot
parent 7a62e7e333
commit 247fc6774f
4 changed files with 68 additions and 5 deletions
+4 -4
View File
@@ -148,11 +148,11 @@ export type FunctionTypeAnnotationParamTypeAnnotation =
|}>
| $ReadOnly<{|
type: 'ArrayTypeAnnotation',
elementType: FunctionTypeAnnotationParamTypeAnnotation,
elementType: ?FunctionTypeAnnotationParamTypeAnnotation,
|}>
| $ReadOnly<{|
type: 'ObjectTypeAnnotation',
properties: $ReadOnlyArray<ObjectParamTypeAnnotation>,
properties: ?$ReadOnlyArray<ObjectParamTypeAnnotation>,
|}>
| $ReadOnly<{|
type: 'FunctionTypeAnnotation',
@@ -174,7 +174,7 @@ export type FunctionTypeAnnotationReturn =
|}>
| $ReadOnly<{|
type: 'ArrayTypeAnnotation',
elementType: FunctionTypeAnnotationReturnArrayElementType,
elementType: ?FunctionTypeAnnotationReturnArrayElementType,
|}>
| $ReadOnly<{|
type: 'GenericPromiseTypeAnnotation',
@@ -182,7 +182,7 @@ export type FunctionTypeAnnotationReturn =
|}>
| $ReadOnly<{|
type: 'ObjectTypeAnnotation',
properties: $ReadOnlyArray<ObjectParamTypeAnnotation>,
properties: ?$ReadOnlyArray<ObjectParamTypeAnnotation>,
|}>;
export type FunctionTypeAnnotationParam = $ReadOnly<{|
@@ -291,6 +291,29 @@ export interface Spec extends TurboModule {
export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
`;
const NATIVE_MODULE_WITH_ARRAY_WITH_UNION_AND_TOUPLE = `
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
'use strict';
import type {TurboModule} from '../RCTExport';
import * as TurboModuleRegistry from '../TurboModuleRegistry';
export interface Spec extends TurboModule {
+getArray: (arg: Array<[string, string]>) => Array<string | number | boolean>;
}
export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
`;
const NATIVE_MODULE_WITH_ARRAY_WITH_ALIAS = `
@@ -400,6 +423,7 @@ export default TurboModuleRegistry.getEnforcing<Spec>('SampleTurboModule');
module.exports = {
NATIVE_MODULE_WITH_OBJECT_WITH_OBJECT_DEIFNED_IN_FILE_AS_PROPERTY,
NATIVE_MODULE_WITH_ARRAY_WITH_UNION_AND_TOUPLE,
NATIVE_MODULE_WITH_WITH_FLOAT_AND_INT32,
NATIVE_MODULE_WITH_WITH_ALIASES,
NATIVE_MODULE_WITH_PROMISE,
@@ -73,6 +73,42 @@ Object {
}
`;
exports[`RN Codegen Flow Parser can generate fixture NATIVE_MODULE_WITH_ARRAY_WITH_UNION_AND_TOUPLE 1`] = `
Object {
"modules": Object {
"NativeSampleTurboModule": Object {
"nativeModules": Object {
"SampleTurboModule": Object {
"properties": Array [
Object {
"name": "getArray",
"typeAnnotation": Object {
"optional": false,
"params": Array [
Object {
"name": "arg",
"nullable": false,
"typeAnnotation": Object {
"elementType": undefined,
"type": "ArrayTypeAnnotation",
},
},
],
"returnTypeAnnotation": Object {
"elementType": undefined,
"type": "ArrayTypeAnnotation",
},
"type": "FunctionTypeAnnotation",
},
},
],
},
},
},
},
}
`;
exports[`RN Codegen Flow Parser can generate fixture NATIVE_MODULE_WITH_BASIC_ARRAY 1`] = `
Object {
"modules": Object {
@@ -55,7 +55,7 @@ function getElementTypeForArrayOrObject(
arrayParam,
paramName,
types: TypeMap,
): FunctionTypeAnnotationParamTypeAnnotation {
): FunctionTypeAnnotationParamTypeAnnotation | typeof undefined {
const typeAnnotation = getValueFromTypes(arrayParam, types);
const type =
typeAnnotation.type === 'GenericTypeAnnotation'
@@ -113,6 +113,9 @@ function getElementTypeForArrayOrObject(
return {
type: 'FloatTypeAnnotation',
};
case 'TupleTypeAnnotation':
case 'UnionTypeAnnotation':
return undefined;
default:
throw new Error(
`Unsupported param type for method "${name}", param "${paramName}". Found ${type}`,