Skip to content

Commit

Permalink
CPPSDK: review comments updated
Browse files Browse the repository at this point in the history
  • Loading branch information
HaseenaSainul committed Oct 24, 2023
1 parent a51ea59 commit 8882b2c
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
3 changes: 2 additions & 1 deletion languages/cpp/language.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"extractSubSchemas": true,
"unwrapResultObjects": false,
"createPolymorphicMethods": true,
"excludeDeclarations":true,
"excludeDeclarations": true,
"enumSuffix": ".cpp",
"templatesPerModule": [
"/include/module.h",
"/src/module.cpp"
Expand Down
4 changes: 2 additions & 2 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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?
Expand Down
2 changes: 2 additions & 0 deletions src/macrofier/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const macrofy = async (
convertTuplesToArraysOrObjects,
additionalSchemaTemplates,
excludeDeclarations,
enumSuffix,
aggregateFile,
operators,
primitives,
Expand Down Expand Up @@ -95,6 +96,7 @@ const macrofy = async (
allocatedPrimitiveProxies,
additionalSchemaTemplates,
excludeDeclarations,
enumSuffix,
operators
})

Expand Down
12 changes: 6 additions & 6 deletions src/macrofier/types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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(/,*$/, '');
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/sdk/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 8882b2c

Please sign in to comment.