Skip to content

Commit

Permalink
Make enum types annotation objects instead of literal strings and num…
Browse files Browse the repository at this point in the history
…bers (#47349)

Summary:

This is needed to be able to recurse into the literals and compare them.

I'm primarily unsure if there is a problem representing doubles/floats as numbers instead of strings though.

Changelog: [Internal]

Reviewed By: makovkastar

Differential Revision: D65284058
  • Loading branch information
elicwhite authored and facebook-github-bot committed Nov 28, 2024
1 parent ce9f534 commit 7b2e6f0
Show file tree
Hide file tree
Showing 12 changed files with 302 additions and 77 deletions.
2 changes: 1 addition & 1 deletion packages/react-native-codegen/src/CodegenSchema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ export interface NativeModuleBooleanTypeAnnotation {

export type NativeModuleEnumMember = {
readonly name: string;
readonly value: string | number;
readonly value: NativeModuleStringLiteralTypeAnnotation | NativeModuleNumberLiteralTypeAnnotation,
};

export type NativeModuleEnumMemberType =
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-codegen/src/CodegenSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ export type NativeModuleNumberTypeAnnotation = $ReadOnly<{

export type NativeModuleEnumMember = {
name: string,
value: string | number,
value: StringLiteralTypeAnnotation | NumberLiteralTypeAnnotation,
};

export type NativeModuleEnumMemberType =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,14 @@ struct Bridging<${enumName}> {
};`;
};

function getMemberValueAppearance(member: NativeModuleEnumMember['value']) {
if (member.type === 'StringLiteralTypeAnnotation') {
return `"${member.value}"`;
} else {
return member.value;
}
}

function generateEnum(
hasteModuleName: string,
origEnumName: string,
Expand All @@ -417,9 +425,6 @@ function generateEnum(
const nativeEnumMemberType: NativeEnumMemberValueType =
memberType === 'StringTypeAnnotation' ? 'std::string' : 'int32_t';

const getMemberValueAppearance = (value: string | number) =>
memberType === 'StringTypeAnnotation' ? `"${value}"` : `${value}`;

const fromCases =
members
.map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,17 @@ const SIMPLE_NATIVE_MODULES: SchemaType = {
members: [
{
name: 'ONE',
value: '1',
value: {
type: 'NumberLiteralTypeAnnotation',
value: 1,
},
},
{
name: 'TWO',
value: '2',
value: {
type: 'NumberLiteralTypeAnnotation',
value: 2,
},
},
],
},
Expand All @@ -178,15 +184,24 @@ const SIMPLE_NATIVE_MODULES: SchemaType = {
members: [
{
name: 'POINT_ZERO',
value: '0.0',
value: {
type: 'NumberLiteralTypeAnnotation',
value: 0.0,
},
},
{
name: 'POINT_ONE',
value: '0.1',
value: {
type: 'NumberLiteralTypeAnnotation',
value: 0.1,
},
},
{
name: 'POINT_TWO',
value: '0.2',
value: {
type: 'NumberLiteralTypeAnnotation',
value: 0.2,
},
},
],
},
Expand All @@ -197,11 +212,17 @@ const SIMPLE_NATIVE_MODULES: SchemaType = {
members: [
{
name: 'HELLO',
value: 'hello',
value: {
type: 'StringLiteralTypeAnnotation',
value: 'hello',
},
},
{
name: 'GoodBye',
value: 'goodbye',
value: {
type: 'StringLiteralTypeAnnotation',
value: 'goodbye',
},
},
],
},
Expand Down Expand Up @@ -1926,11 +1947,17 @@ const CXX_ONLY_NATIVE_MODULES: SchemaType = {
members: [
{
name: 'IA',
value: '23',
value: {
type: 'NumberLiteralTypeAnnotation',
value: 23,
},
},
{
name: 'IB',
value: '42',
value: {
type: 'NumberLiteralTypeAnnotation',
value: 42,
},
},
],
},
Expand All @@ -1941,11 +1968,17 @@ const CXX_ONLY_NATIVE_MODULES: SchemaType = {
members: [
{
name: 'FA',
value: '1.23',
value: {
type: 'NumberLiteralTypeAnnotation',
value: 1.23,
},
},
{
name: 'FB',
value: '4.56',
value: {
type: 'NumberLiteralTypeAnnotation',
value: 4.56,
},
},
],
},
Expand All @@ -1956,11 +1989,17 @@ const CXX_ONLY_NATIVE_MODULES: SchemaType = {
members: [
{
name: 'NA',
value: 'NA',
value: {
type: 'StringLiteralTypeAnnotation',
value: 'NA',
},
},
{
name: 'NB',
value: 'NB',
value: {
type: 'StringLiteralTypeAnnotation',
value: 'NB',
},
},
],
},
Expand All @@ -1971,11 +2010,17 @@ const CXX_ONLY_NATIVE_MODULES: SchemaType = {
members: [
{
name: 'SA',
value: 's---a',
value: {
type: 'StringLiteralTypeAnnotation',
value: 's---a',
},
},
{
name: 'SB',
value: 's---b',
value: {
type: 'StringLiteralTypeAnnotation',
value: 's---b',
},
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2047,7 +2047,7 @@ template <>
struct Bridging<NativeSampleTurboModuleFloatEnum> {
static NativeSampleTurboModuleFloatEnum fromJs(jsi::Runtime &rt, const jsi::Value &rawValue) {
double value = (double)rawValue.asNumber();
if (value == 0.0) {
if (value == 0) {
return NativeSampleTurboModuleFloatEnum::POINT_ZERO;
} else if (value == 0.1) {
return NativeSampleTurboModuleFloatEnum::POINT_ONE;
Expand All @@ -2060,7 +2060,7 @@ struct Bridging<NativeSampleTurboModuleFloatEnum> {

static jsi::Value toJs(jsi::Runtime &rt, NativeSampleTurboModuleFloatEnum value) {
if (value == NativeSampleTurboModuleFloatEnum::POINT_ZERO) {
return bridging::toJs(rt, 0.0);
return bridging::toJs(rt, 0);
} else if (value == NativeSampleTurboModuleFloatEnum::POINT_ONE) {
return bridging::toJs(rt, 0.1);
} else if (value == NativeSampleTurboModuleFloatEnum::POINT_TWO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,11 +478,17 @@ describe('typeEnumResolution', () => {
members: [
{
name: 'Hello',
value: 'hello',
value: {
type: 'StringLiteralTypeAnnotation',
value: 'hello',
},
},
{
name: 'Goodbye',
value: 'goodbye',
value: {
type: 'StringLiteralTypeAnnotation',
value: 'goodbye',
},
},
],
},
Expand Down Expand Up @@ -521,11 +527,17 @@ describe('typeEnumResolution', () => {
members: [
{
name: 'On',
value: '1',
value: {
type: 'NumberLiteralTypeAnnotation',
value: 1,
},
},
{
name: 'Off',
value: '0',
value: {
type: 'NumberLiteralTypeAnnotation',
value: 0,
},
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,17 @@ exports[`RN Codegen Flow Parser can generate fixture CXX_ONLY_NATIVE_MODULE 1`]
'members': [
{
'name': 'SD',
'value': 'SD'
'value': {
'type': 'StringLiteralTypeAnnotation',
'value': 'SD'
}
},
{
'name': 'HD',
'value': 'HD'
'value': {
'type': 'StringLiteralTypeAnnotation',
'value': 'HD'
}
}
]
},
Expand All @@ -162,15 +168,24 @@ exports[`RN Codegen Flow Parser can generate fixture CXX_ONLY_NATIVE_MODULE 1`]
'members': [
{
'name': 'Corrupted',
'value': -1
'value': {
'type': 'NumberLiteralTypeAnnotation',
'value': -1
}
},
{
'name': 'Low',
'value': 720
'value': {
'type': 'NumberLiteralTypeAnnotation',
'value': 720
}
},
{
'name': 'High',
'value': 1080
'value': {
'type': 'NumberLiteralTypeAnnotation',
'value': 1080
}
}
]
},
Expand All @@ -181,15 +196,24 @@ exports[`RN Codegen Flow Parser can generate fixture CXX_ONLY_NATIVE_MODULE 1`]
'members': [
{
'name': 'One',
'value': 'one'
'value': {
'type': 'StringLiteralTypeAnnotation',
'value': 'one'
}
},
{
'name': 'Two',
'value': 'two'
'value': {
'type': 'StringLiteralTypeAnnotation',
'value': 'two'
}
},
{
'name': 'Three',
'value': 'three'
'value': {
'type': 'StringLiteralTypeAnnotation',
'value': 'three'
}
}
]
}
Expand Down Expand Up @@ -471,11 +495,17 @@ exports[`RN Codegen Flow Parser can generate fixture IOS_ONLY_NATIVE_MODULE 1`]
'members': [
{
'name': 'SD',
'value': 'SD'
'value': {
'type': 'StringLiteralTypeAnnotation',
'value': 'SD'
}
},
{
'name': 'HD',
'value': 'HD'
'value': {
'type': 'StringLiteralTypeAnnotation',
'value': 'HD'
}
}
]
},
Expand All @@ -486,11 +516,17 @@ exports[`RN Codegen Flow Parser can generate fixture IOS_ONLY_NATIVE_MODULE 1`]
'members': [
{
'name': 'Low',
'value': 720
'value': {
'type': 'NumberLiteralTypeAnnotation',
'value': 720
}
},
{
'name': 'High',
'value': 1080
'value': {
'type': 'NumberLiteralTypeAnnotation',
'value': 1080
}
}
]
},
Expand All @@ -501,15 +537,24 @@ exports[`RN Codegen Flow Parser can generate fixture IOS_ONLY_NATIVE_MODULE 1`]
'members': [
{
'name': 'One',
'value': 'one'
'value': {
'type': 'StringLiteralTypeAnnotation',
'value': 'one'
}
},
{
'name': 'Two',
'value': 'two'
'value': {
'type': 'StringLiteralTypeAnnotation',
'value': 'two'
}
},
{
'name': 'Three',
'value': 'three'
'value': {
'type': 'StringLiteralTypeAnnotation',
'value': 'three'
}
}
]
}
Expand Down
Loading

0 comments on commit 7b2e6f0

Please sign in to comment.