Skip to content

Commit

Permalink
fix: Insert 'v' in front of version enum names
Browse files Browse the repository at this point in the history
  • Loading branch information
jlacivita committed Sep 11, 2023
1 parent e46d88c commit d8b9ada
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 26 deletions.
20 changes: 1 addition & 19 deletions languages/c/Types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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') {
Expand Down
4 changes: 2 additions & 2 deletions languages/c/src/types/NativeHelpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()}`
}

Expand Down
4 changes: 2 additions & 2 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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')
Expand Down
8 changes: 8 additions & 0 deletions src/shared/json-schema.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/shared/typescript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit d8b9ada

Please sign in to comment.