diff --git a/languages/cpp/language.config.json b/languages/cpp/language.config.json index 541f0258..debc539d 100644 --- a/languages/cpp/language.config.json +++ b/languages/cpp/language.config.json @@ -6,7 +6,6 @@ "unwrapResultObjects": false, "createPolymorphicMethods": true, "excludeDeclarations":true, - "enumSuffix": ".cpp", "aggregateFiles": [ "/include/firebolt.h", "/src/firebolt.cpp" diff --git a/languages/cpp/templates/types/enum.h b/languages/cpp/templates/types/enum.h index 166fb231..6ec88816 100644 --- a/languages/cpp/templates/types/enum.h +++ b/languages/cpp/templates/types/enum.h @@ -1,4 +1,4 @@ /* ${title} ${description} */ enum class ${name} { - ${key}, + ${key}${delimiter},${end.delimiter} }; diff --git a/src/macrofier/engine.mjs b/src/macrofier/engine.mjs index cb550f87..c853e324 100644 --- a/src/macrofier/engine.mjs +++ b/src/macrofier/engine.mjs @@ -428,7 +428,7 @@ const promoteAndNameSubSchemas = (obj) => { } }) } - }) + }) if (!schema.title) { schema.title = capitalize(key) @@ -741,14 +741,12 @@ const convertEnumTemplate = (schema, templateName, templates) => { const template = getTemplate(templateName, templates).split('\n') for (var i = 0; i < template.length; i++) { if (template[i].indexOf('${key}') >= 0) { - template[i] = enumSchema.enum.filter(value => value).map(value => { + template[i] = enumSchema.enum.map((value, id) => { const safeName = value.split(':').pop().replace(/[\.\-]/g, '_').replace(/\+/g, '_plus').replace(/([a-z])([A-Z0-9])/g, '$1_$2').toUpperCase() return template[i].replace(/\$\{key\}/g, safeName) .replace(/\$\{value\}/g, value) + .replace(/\$\{delimiter\}(.*?)\$\{end.delimiter\}/g, id === enumSchema.enum.length - 1 ? '' : '$1') }).join('\n') - if (!templateName.includes(config.enumSuffix)) { - template[i] = template[i].replace(/,*$/, ''); - } } } return template.join('\n') @@ -881,7 +879,7 @@ function generateSchemas(json, templates, options) { else { content = content.replace(/\$\{if\.description\}(.*?)\{end\.if\.description\}/gms, '$1') } - const schemaShape = types.getSchemaShape(schema, json, { templateDir: state.typeTemplateDir, destination: state.destination, section: options.section, enumSuffix: config.enumSuffix }) + const schemaShape = types.getSchemaShape(schema, json, { templateDir: state.typeTemplateDir, destination: state.destination, section: options.section}) content = content .replace(/\$\{schema.title\}/, (schema.title || name)) diff --git a/src/macrofier/index.mjs b/src/macrofier/index.mjs index 6550a9b6..04cdd9fb 100644 --- a/src/macrofier/index.mjs +++ b/src/macrofier/index.mjs @@ -54,7 +54,6 @@ const macrofy = async ( additionalSchemaTemplates, additionalMethodTemplates, excludeDeclarations, - enumSuffix, aggregateFiles, operators, primitives, @@ -98,7 +97,6 @@ const macrofy = async ( additionalSchemaTemplates, additionalMethodTemplates, excludeDeclarations, - enumSuffix, operators }) diff --git a/src/macrofier/types.mjs b/src/macrofier/types.mjs index 275da98d..f25a9689 100644 --- a/src/macrofier/types.mjs +++ b/src/macrofier/types.mjs @@ -157,18 +157,22 @@ const insertConstMacros = (content, schema, module, name) => { return content } -const insertEnumMacros = (content, schema, module, name, suffix, enumSuffix) => { +const insertEnumMacros = (content, schema, module, name, suffix, templateDir = "types") => { const template = content.split('\n') for (var i = 0; i < template.length; i++) { if (template[i].indexOf('${key}') >= 0) { - template[i] = schema.enum.filter(value => value).map(value => { - return template[i].replace(/\$\{key\}/g, safeName(value)) - .replace(/\$\{value\}/g, value) + let values = [] + schema.enum.map(value => { + if (!value) { + value = getTemplate(path.join(templateDir, 'unset' + suffix)) + } + value ? values.push(template[i].replace(/\$\{key\}/g, safeName(value)) + .replace(/\$\{value\}/g, value)) : '' + }) + template[i] = values.map((value, id) => { + return value.replace(/\$\{delimiter\}(.*?)\$\{end.delimiter\}/g, id === values.length - 1 ? '' : '$1') }).join('\n') - if (suffix !== enumSuffix) { - template[i] = template[i].replace(/,*$/, ''); - } } } @@ -365,7 +369,7 @@ const sanitize = (schema) => { return result } -function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', name = '', parent = '', property = '', level = 0, summary, descriptions = true, destination, section, enums = true, enumSuffix = '', skipTitleOnce = false } = {}) { +function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', name = '', parent = '', property = '', level = 0, summary, descriptions = true, destination, section, enums = true, skipTitleOnce = false } = {}) { schema = sanitize(schema) state.destination = destination @@ -382,13 +386,13 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', name if (enums && level === 0 && Array.isArray(schema.enum) && ((schema.type === "string") || (schema.type[0] === "string"))) { result = getTemplate(path.join(templateDir, 'enum' + suffix)) - return insertSchemaMacros(insertEnumMacros(result, schema, module, theTitle, suffix, enumSuffix), schema, module, theTitle, parent, property) + return insertSchemaMacros(insertEnumMacros(result, schema, module, theTitle, suffix, templateDir), schema, module, theTitle, parent, property) } if (schema['$ref']) { const someJson = getPath(schema['$ref'], module) if (someJson) { - return getSchemaShape(someJson, module, { templateDir, name, parent, property, level, summary, descriptions, destination, enums, enumSuffix }) + return getSchemaShape(someJson, module, { templateDir, name, parent, property, level, summary, descriptions, destination, enums }) } throw "Unresolvable $ref: " + schema['ref'] + ", in " + module.info.title } @@ -400,7 +404,7 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', name else if (!skipTitleOnce && (level > 0) && schema.title) { let enumType = (schema.type === 'string' && Array.isArray(schema.enum)) // TODO: allow the 'ref' template to actually insert the shape using getSchemaShape - const innerShape = getSchemaShape(schema, module, { skipTitleOnce: true, templateDir, name, parent, property, level, summary, descriptions, destination, enums: enumType, enumSuffix }) + const innerShape = getSchemaShape(schema, module, { skipTitleOnce: true, templateDir, name, parent, property, level, summary, descriptions, destination, enums: enumType }) const shape = getTemplate(path.join(templateDir, 'ref' + suffix)) .replace(/\$\{shape\}/g, innerShape) diff --git a/src/sdk/index.mjs b/src/sdk/index.mjs index 029f457b..763b6c9e 100755 --- a/src/sdk/index.mjs +++ b/src/sdk/index.mjs @@ -70,7 +70,6 @@ const run = async ({ additionalSchemaTemplates: config.additionalSchemaTemplates, additionalMethodTemplates: config.additionalMethodTemplates, excludeDeclarations: config.excludeDeclarations, - enumSuffix: config.enumSuffix, staticModuleNames: staticModuleNames, hideExcluded: true, aggregateFiles: config.aggregateFiles,