Skip to content

Commit

Permalink
chore: test pr
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinshahfws committed Jun 23, 2024
1 parent cabc683 commit 41e5071
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/macrofier/types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ const insertEnumMacros = (content, schema, module, name, suffix, templateDir = "
if (!value) {
value = getTemplate(path.join(templateDir, 'unset' + suffix))
}
value ? values.push(template[i].replace(/\$\{key\}/g, getSafeEnumKeyName(value))
value ? values.push(template[i].replace(/\$\{key\}/g, getSafeEnumKeyName(value, schema.enumKeyPrefix))
.replace(/\$\{value\}/g, value)) : ''
})
template[i] = values.map((value, id) => {
Expand Down
19 changes: 13 additions & 6 deletions src/shared/json-schema.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -426,12 +426,19 @@ function mergeOneOf(schema) {
return union(schema.oneOf)
}

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()
const getSafeEnumKeyName = function(value, keyPrefix = '') {
if (keyPrefix != '') {
value = keyPrefix + '_' + value
}

let key = value.split(':').pop() // use last portion of urn:style:values
.replace(/\+/g, '_plus') // change + to _plus
.replace(/[\.\-\/\;]/g, '_') // replace special characters
.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

return key.toUpperCase()
}

export {
getSchemaConstraints,
Expand Down
2 changes: 1 addition & 1 deletion src/validate/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const run = async ({

addFormats(ajv)
// explicitly add our custom extensions so we can keep strict mode on (TODO: put these in a JSON config?)
ajv.addVocabulary(['x-method', 'x-this-param', 'x-additional-params', 'x-schemas', 'components', 'x-property'])
ajv.addVocabulary(['x-method', 'x-this-param', 'x-additional-params', 'x-schemas', 'components', 'x-property', 'enumKeyPrefix'])

const firebolt = ajv.compile(fireboltOpenRpcSpec)
const jsonschema = ajv.compile(jsonSchemaSpec)
Expand Down

0 comments on commit 41e5071

Please sign in to comment.