nitro-codegen
Version:
The code-generator for react-native-nitro-modules.
53 lines (49 loc) • 1.5 kB
JavaScript
import { NitroConfig } from '../../config/NitroConfig.js';
import { indent, toLowerCamelCase } from '../../utils.js';
import { createFileMetadataString } from '../helpers.js';
export function createSwiftEnumBridge(enumType) {
const fullName = NitroConfig.getCxxNamespace('swift', enumType.enumName);
const initializeCases = enumType.enumMembers.map((m) => `
case "${m.stringValue}":
self = .${toLowerCamelCase(m.name)}
`.trim());
const toStringCases = enumType.enumMembers.map((m) => `
case .${toLowerCamelCase(m.name)}:
return "${m.stringValue}"
`.trim());
const code = `
${createFileMetadataString(`${enumType.enumName}.swift`)}
/**
* Represents the JS ${enumType.jsType} \`${enumType.enumName}\`, backed by a C++ enum.
*/
public typealias ${enumType.enumName} = ${fullName}
public extension ${enumType.enumName} {
/**
* Get a ${enumType.enumName} for the given String value, or
* return \`nil\` if the given value was invalid/unknown.
*/
init?(fromString string: String) {
switch string {
${indent(initializeCases.join('\n'), ' ')}
default:
return nil
}
}
/**
* Get the String value this ${enumType.enumName} represents.
*/
var stringValue: String {
switch self {
${indent(toStringCases.join('\n'), ' ')}
}
}
}
`.trim();
return {
content: code,
language: 'swift',
name: `${enumType.enumName}.swift`,
platform: 'ios',
subdirectory: [],
};
}