Skip to content

Commit

Permalink
enum handling: handling unset field + delimeter
Browse files Browse the repository at this point in the history
  • Loading branch information
HaseenaSainul committed Oct 24, 2023
1 parent d7bef1d commit 5bde444
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 22 deletions.
1 change: 0 additions & 1 deletion languages/cpp/language.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"unwrapResultObjects": false,
"createPolymorphicMethods": true,
"excludeDeclarations":true,
"enumSuffix": ".cpp",
"aggregateFiles": [
"/include/firebolt.h",
"/src/firebolt.cpp"
Expand Down
2 changes: 1 addition & 1 deletion languages/cpp/templates/types/enum.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* ${title} ${description} */
enum class ${name} {
${key},
${key}${delimiter},${end.delimiter}
};
10 changes: 4 additions & 6 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ const promoteAndNameSubSchemas = (obj) => {
}
})
}
})
})

if (!schema.title) {
schema.title = capitalize(key)
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 0 additions & 2 deletions src/macrofier/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ const macrofy = async (
additionalSchemaTemplates,
additionalMethodTemplates,
excludeDeclarations,
enumSuffix,
aggregateFiles,
operators,
primitives,
Expand Down Expand Up @@ -98,7 +97,6 @@ const macrofy = async (
additionalSchemaTemplates,
additionalMethodTemplates,
excludeDeclarations,
enumSuffix,
operators
})

Expand Down
26 changes: 15 additions & 11 deletions src/macrofier/types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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(/,*$/, '');
}
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/sdk/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 5bde444

Please sign in to comment.