diff --git a/languages/c/Types.mjs b/languages/c/Types.mjs index 1ff0fdf9..35c46ba2 100644 --- a/languages/c/Types.mjs +++ b/languages/c/Types.mjs @@ -17,7 +17,7 @@ */ import deepmerge from 'deepmerge' -import { getPath } from '../../src/shared/json-schema.mjs' +import { getPath, getSafeEnumKeyName } from '../../src/shared/json-schema.mjs' import { getTypeName, getModuleName, description, getObjectManagement, getNativeType, getPropertyAccessors, capitalize, isOptional, generateEnum, getMapAccessors, getArrayAccessors, getPropertyGetterSignature, getFireboltStringType } from './src/types/NativeHelpers.mjs' import { getArrayAccessorsImpl, getMapAccessorsImpl, getObjectManagementImpl, getParameterInstantiation, getPropertyAccessorsImpl, getResultInstantiation, getCallbackParametersInstantiation, getCallbackResultInstantiation, getCallbackResponseInstantiation } from './src/types/ImplHelpers.mjs' import { getJsonContainerDefinition, getJsonDataStructName, getJsonDataPrefix } from './src/types/JSONHelpers.mjs' @@ -717,24 +717,6 @@ function getJsonTypeInfo(module = {}, json = {}, name = '', schemas, prefix = '' return structure } -function getTypeScriptType(jsonType) { - if (jsonType === 'integer') { - return 'number' - } - else { - return jsonType - } -} - -const enumReducer = (acc, val, i, arr) => { - const keyName = val.split(':').pop().replace(/[\.\-]/g, '_').replace(/\+/g, '_plus').replace(/([a-z])([A-Z0-9])/g, '$1_$2').toUpperCase() - acc = acc + ` ${keyName} = '${val}'` - if (i < arr.length - 1) { - acc = acc.concat(',\n') - } - return acc -} - function getSchemaInstantiation(schema, module, name, { instantiationType = '', prefix = '' } = {}) { if (instantiationType === 'params') { diff --git a/languages/c/src/types/NativeHelpers.mjs b/languages/c/src/types/NativeHelpers.mjs index d2540279..061f30b8 100644 --- a/languages/c/src/types/NativeHelpers.mjs +++ b/languages/c/src/types/NativeHelpers.mjs @@ -22,7 +22,7 @@ import safe from 'crocks/Maybe/safe.js' import pointfree from 'crocks/pointfree/index.js' const { chain, filter, reduce, option, map } = pointfree import predicates from 'crocks/predicates/index.js' -import { getPath, getExternalSchemaPaths } from '../../../../src/shared/json-schema.mjs' +import { getPath, getExternalSchemaPaths, getSafeEnumKeyName } from '../../../../src/shared/json-schema.mjs' import deepmerge from 'deepmerge' const { isObject, isArray, propEq, pathSatisfies, hasProp, propSatisfies } = predicates @@ -126,7 +126,7 @@ const getArrayAccessors = (arrayName, propertyType, valueType) => { } const enumValue = (val,prefix) => { - const keyName = val.replace(/[\.\-:]/g, '_').replace(/\+/g, '_plus').replace(/([a-z])([A-Z0-9])/g, '$1_$2').toUpperCase() + const keyName = getSafeEnumKeyName(val) return ` ${prefix.toUpperCase()}_${keyName.toUpperCase()}` } diff --git a/src/macrofier/engine.mjs b/src/macrofier/engine.mjs index d0535d00..6332ba39 100644 --- a/src/macrofier/engine.mjs +++ b/src/macrofier/engine.mjs @@ -31,7 +31,7 @@ const { isObject, isArray, propEq, pathSatisfies, propSatisfies } = predicates import { isRPCOnlyMethod, isProviderInterfaceMethod, getProviderInterface, getPayloadFromEvent, providerHasNoParameters, isTemporalSetMethod, isCallsMetricsMethod, isExcludedMethod, hasMethodAttributes, getMethodAttributes, isEventMethodWithContext, getSemanticVersion, getSetterFor, getProvidedCapabilities, isPolymorphicPullMethod, hasPublicAPIs, createPolymorphicMethods } from '../shared/modules.mjs' import isEmpty from 'crocks/core/isEmpty.js' -import { getLinkedSchemaPaths, getSchemaConstraints, isSchema, localizeDependencies, isDefinitionReferencedBySchema } from '../shared/json-schema.mjs' +import { getLinkedSchemaPaths, getSchemaConstraints, isSchema, localizeDependencies, isDefinitionReferencedBySchema, getSafeEnumKeyName } from '../shared/json-schema.mjs' // util for visually debugging crocks ADTs const _inspector = obj => { @@ -634,7 +634,7 @@ const convertEnumTemplate = (schema, templateName, templates) => { for (var i = 0; i < template.length; i++) { if (template[i].indexOf('${key}') >= 0) { template[i] = enumSchema.enum.map(value => { - const safeName = value.split(':').pop().replace(/[\.\-]/g, '_').replace(/\+/g, '_plus').replace(/([a-z])([A-Z0-9])/g, '$1_$2').toUpperCase() + const safeName = getSafeEnumKeyName(value) return template[i].replace(/\$\{key\}/g, safeName) .replace(/\$\{value\}/g, value) }).join('\n') diff --git a/src/shared/json-schema.mjs b/src/shared/json-schema.mjs index e7bb1d86..76a5739f 100644 --- a/src/shared/json-schema.mjs +++ b/src/shared/json-schema.mjs @@ -355,8 +355,16 @@ const isDefinitionReferencedBySchema = (name = '', moduleJson = {}) => { return (refs.length > 0) } +const getSafeEnumKeyName = (value) => value.split(':').pop() // use last portion of urn:style:values + .replace(/[\.\-]/g, '_') // replace dots and dashes + .replace(/\+/g, '_plus') // change + to _plus + .replace(/([a-z])([A-Z0-9])/g, '$1_$2') // camel -> snake case + .replace(/^([0-9]+(\.[0-9]+)?)/, 'v$1') // insert `v` in front of things that look like version numbers + .toUpperCase() + export { getSchemaConstraints, + getSafeEnumKeyName, getExternalSchemaPaths, getLocalSchemas, getLocalSchemaPaths, diff --git a/src/shared/typescript.mjs b/src/shared/typescript.mjs index 5982137e..28e80086 100644 --- a/src/shared/typescript.mjs +++ b/src/shared/typescript.mjs @@ -17,7 +17,7 @@ */ import deepmerge from 'deepmerge' -import { getPath, localizeDependencies } from './json-schema.mjs' +import { getPath, getSafeEnumKeyName, localizeDependencies } from './json-schema.mjs' const isSynchronous = m => !m.tags ? false : m.tags.map(t => t.name).find(s => s === 'synchronous') @@ -45,7 +45,7 @@ function getSchemaShape(schema = {}, module = {}, { name = '', level = 0, title, let theTitle = (level === 0 ? schema.title || name : name) if (enums && level === 0 && schema.type === "string" && Array.isArray(schema.enum)) { - return `enum ${schema.title || name} {\n\t` + schema.enum.map(value => value.split(':').pop().replace(/[\.\-]/g, '_').replace(/\+/g, '_plus').replace(/([a-z])([A-Z0-9])/g, '$1_$2').toUpperCase() + ` = '${value}'`).join(',\n\t') + '\n}\n' + return `enum ${schema.title || name} {\n\t` + schema.enum.map(value => getSafeEnumKeyName(value) + ` = '${value}'`).join(',\n\t') + '\n}\n' } if (!theTitle) { @@ -338,7 +338,7 @@ function getSchemaShape(schema = {}, module = {}, { name = '', level = 0, title, } const enumReducer = (acc, val, i, arr) => { - const keyName = val.split(':').pop().replace(/[\.\-]/g, '_').replace(/\+/g, '_plus').replace(/([a-z])([A-Z0-9])/g, '$1_$2').toUpperCase() + const keyName = getSafeEnumKeyName(val) acc = acc + ` ${keyName} = '${val}'` if (i < arr.length-1) { acc = acc.concat(',\n')