diff --git a/languages/cpp/language.config.json b/languages/cpp/language.config.json index 491e9559..9069edd0 100644 --- a/languages/cpp/language.config.json +++ b/languages/cpp/language.config.json @@ -5,7 +5,8 @@ "extractSubSchemas": true, "unwrapResultObjects": false, "createPolymorphicMethods": true, - "excludeDeclarations":true, + "excludeDeclarations": true, + "enumSuffix": ".cpp", "templatesPerModule": [ "/include/module.h", "/src/module.cpp" diff --git a/src/macrofier/engine.mjs b/src/macrofier/engine.mjs index 3489a5c9..24209c92 100644 --- a/src/macrofier/engine.mjs +++ b/src/macrofier/engine.mjs @@ -826,7 +826,7 @@ function generateSchemas(json, templates, options) { else { content = content.replace(/\$\{if\.description\}(.*?)\{end\.if\.description\}/gms, '$1') } - const schemaShape = types.getSchemaShape(schema, json, { name, prefix, templateDir: state.typeTemplateDir, destination: state.destination, section: options.section }) + const schemaShape = types.getSchemaShape(schema, json, { name, prefix, templateDir: state.typeTemplateDir, destination: state.destination, section: options.section, enumSuffix: config.enumSuffix }) content = content .replace(/\$\{schema.title\}/, (schema.title || name)) @@ -1195,7 +1195,7 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {}) const resultInst = types.getSchemaShape(flattenedMethod.result.schema, json, { templateDir: 'result-instantiation', property: flattenedMethod.result.name, destination: state.destination, section: state.section, level: 1, skipTitleOnce: true }) // w/out level: 1, getSchemaShape skips anonymous types, like primitives const serializedEventParams = event ? flattenedMethod.params.filter(p => p.name !== 'listen').map(param => types.getSchemaShape(param.schema, json, {templateDir: 'parameter-serialization', property: param.name, destination: state.destination, section: state.section, level: 1, skipTitleOnce: true })).join('\n') : '' // this was wrong... check when we merge if it was fixed - const callbackSerializedParams = event ? types.getSchemaShape(event.result.schema, json, { templateDir: 'parameter-serialization', property: result.name, destination: state.destination, section: state.section, level: 1, skipTitleOnce: true }) : '' + const callbackSerializedParams = event ? types.getSchemaShape(event.result.schema, json, { templateDir: 'parameter-serialization', property: result.name, destination: state.destination, section: state.section, level:1, skipTitleOnce: true }) : '' const callbackResultInst = event ? types.getSchemaShape(event, json, { name: event.name, templateDir: 'result-instantiation' }) : '' // const callbackResponseInst = event ? types.getSchemaInstantiation(event, json, event.name, { instantiationType: 'callback.response' }) : '' // hmm... how is this different from callbackSerializedParams? i guess they get merged? diff --git a/src/macrofier/index.mjs b/src/macrofier/index.mjs index 1899f310..b1fb86a4 100644 --- a/src/macrofier/index.mjs +++ b/src/macrofier/index.mjs @@ -53,6 +53,7 @@ const macrofy = async ( convertTuplesToArraysOrObjects, additionalSchemaTemplates, excludeDeclarations, + enumSuffix, aggregateFile, operators, primitives, @@ -95,6 +96,7 @@ const macrofy = async ( allocatedPrimitiveProxies, additionalSchemaTemplates, excludeDeclarations, + enumSuffix, operators }) diff --git a/src/macrofier/types.mjs b/src/macrofier/types.mjs index 39ca82ba..275da98d 100644 --- a/src/macrofier/types.mjs +++ b/src/macrofier/types.mjs @@ -157,7 +157,7 @@ const insertConstMacros = (content, schema, module, name) => { return content } -const insertEnumMacros = (content, schema, module, name, suffix) => { +const insertEnumMacros = (content, schema, module, name, suffix, enumSuffix) => { const template = content.split('\n') for (var i = 0; i < template.length; i++) { @@ -166,7 +166,7 @@ const insertEnumMacros = (content, schema, module, name, suffix) => { return template[i].replace(/\$\{key\}/g, safeName(value)) .replace(/\$\{value\}/g, value) }).join('\n') - if (suffix !== ".cpp") { + if (suffix !== enumSuffix) { template[i] = template[i].replace(/,*$/, ''); } } @@ -365,7 +365,7 @@ const sanitize = (schema) => { return result } -function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', name = '', parent = '', property = '', level = 0, summary, descriptions = true, destination, section, enums = true, skipTitleOnce = false } = {}) { +function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', name = '', parent = '', property = '', level = 0, summary, descriptions = true, destination, section, enums = true, enumSuffix = '', skipTitleOnce = false } = {}) { schema = sanitize(schema) state.destination = destination @@ -382,13 +382,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), schema, module, theTitle, parent, property) + return insertSchemaMacros(insertEnumMacros(result, schema, module, theTitle, suffix, enumSuffix), 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 }) + return getSchemaShape(someJson, module, { templateDir, name, parent, property, level, summary, descriptions, destination, enums, enumSuffix }) } throw "Unresolvable $ref: " + schema['ref'] + ", in " + module.info.title } @@ -400,7 +400,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 }) + const innerShape = getSchemaShape(schema, module, { skipTitleOnce: true, templateDir, name, parent, property, level, summary, descriptions, destination, enums: enumType, enumSuffix }) 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 8e17366a..a8343215 100755 --- a/src/sdk/index.mjs +++ b/src/sdk/index.mjs @@ -69,6 +69,7 @@ const run = async ({ allocatedPrimitiveProxies: config.allocatedPrimitiveProxies, additionalSchemaTemplates: config.additionalSchemaTemplates, excludeDeclarations: config.excludeDeclarations, + enumSuffix: config.enumSuffix, staticModuleNames: staticModuleNames, hideExcluded: true, aggregateFile: config.aggregateFile,