Skip to content

Commit

Permalink
CPPSDK:
Browse files Browse the repository at this point in the history
1. add overrideRule to handle template generation for object/primitives based on language requiredment
2. remove enum level checking inside getSchemaShape, to generate enums shape for all levels
  • Loading branch information
HaseenaSainul committed Oct 25, 2023
1 parent 24eedc4 commit 8d62732
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 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,
"overrideRule": true,
"aggregateFiles": [
"/include/firebolt.h",
"/src/firebolt.cpp"
Expand Down
9 changes: 5 additions & 4 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -879,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})
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 Expand Up @@ -943,7 +943,7 @@ function getRelatedSchemaLinks(schema = {}, json = {}, templates = {}, options =
.map(path => path.substring(2).split('/'))
.map(path => getPathOr(null, path, json))
.filter(schema => schema.title)
.map(schema => '[' + types.getSchemaType(schema, json, { templateDir: state.typeTemplateDir, destination: state.destination, section: state.section }) + '](' + getLinkForSchema(schema, json, { name: schema.title }) + ')') // need full module here, not just the schema
.map(schema => '[' + types.getSchemaType(schema, json, { templateDir: state.typeTemplateDir, destination: state.destination, section: state.section }) + '](' + getLinkForSchema(schema, json) + ')') // need full module here, not just the schema
.filter(link => link)
.join('\n')

Expand Down Expand Up @@ -1349,7 +1349,7 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {})
.replace(/\$\{method\.result\.summary\}/g, result.summary)
.replace(/\$\{method\.result\.link\}/g, getLinkForSchema(result.schema, json)) //, baseUrl: options.baseUrl
.replace(/\$\{method\.result\.type\}/g, types.getSchemaType(result.schema, json, { templateDir: state.typeTemplateDir, title: true, asPath: false, destination: state.destination, result: true })) //, baseUrl: options.baseUrl
.replace(/\$\{method\.result\.json\}/, types.getSchemaType(result.schema, json, { templateDir: 'json-types', destination: state.destination, section: state.section, title: true, code: false, link: false, asPath: false, expandEnums: false, namespace: true }))
.replace(/\$\{method\.result\.json\}/g, types.getSchemaType(result.schema, json, { templateDir: 'json-types', destination: state.destination, section: state.section, title: true, code: false, link: false, asPath: false, expandEnums: false, namespace: true }))
// todo: what does prefix do?
.replace(/\$\{event\.result\.type\}/g, isEventMethod(methodObj) ? types.getMethodSignatureResult(event, json, { destination: state.destination, section: state.section, callback: true }) : '')
.replace(/\$\{event\.result\.json\.type\}/g, resultJsonType)
Expand All @@ -1358,6 +1358,7 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {})
.replace(/\$\{method\.result\}/g, generateResult(result.schema, json, templates, { name: result.name }))
.replace(/\$\{method\.result\.json\.type\}/g, resultJsonType)
.replace(/\$\{method\.result\.instantiation\}/g, resultInst)
.replace(/\$\{method\.result\.initialization\}/g, resultInit)
.replace(/\$\{method\.result\.properties\}/g, resultParams)
.replace(/\$\{method\.result\.instantiation\.with\.indent\}/g, indent(resultInst, ' '))
.replace(/\$\{method\.example\.value\}/g, JSON.stringify(methodObj.examples[0].result.value))
Expand Down Expand Up @@ -1603,7 +1604,7 @@ function insertParameterMacros(template, param, method, module) {
.replace(/\$\{method.param.type\}/g, type)
.replace(/\$\{json.param.type\}/g, jsonType)
.replace(/\$\{method.param.link\}/g, getLinkForSchema(param.schema, module)) //getType(param))
.replace(/\$\{method.param.constraints\}/g, constraints) //getType(param))
.replace(/\$\{method.param.constraints\}/g, constraints) //getType(param))
}

function insertCapabilityMacros(template, capabilities, method, module) {
Expand Down
2 changes: 2 additions & 0 deletions src/macrofier/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const macrofy = async (
additionalSchemaTemplates,
additionalMethodTemplates,
excludeDeclarations,
overrideRule,
aggregateFiles,
operators,
primitives,
Expand Down Expand Up @@ -110,6 +111,7 @@ const macrofy = async (
typer.setPrimitives(primitives)
typer.setAllocatedPrimitiveProxies(allocatedPrimitiveProxies)
typer.setConvertTuples(convertTuplesToArraysOrObjects)
typer.setOverrideRule(overrideRule)

let templatesPermission = {}
if (persistPermission) {
Expand Down
23 changes: 15 additions & 8 deletions src/macrofier/types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { getPath, localizeDependencies } from '../shared/json-schema.mjs'
import path from "path"

let convertTuplesToArraysOrObjects = false
let overrideRule = false
const templates = {}
const state = {}
const primitives = {
Expand Down Expand Up @@ -50,6 +51,10 @@ function setAllocatedPrimitiveProxies(m) {
Object.assign(allocatedPrimitiveProxies, m)
}

function setOverrideRule(rule) {
overrideRule = rule
}

const capitalize = str => str ? str[0].toUpperCase() + str.substr(1) : str
const indent = (str, padding) => {
let first = true
Expand Down Expand Up @@ -339,6 +344,7 @@ const insertObjectMacros = (content, schema, module, title, property, options) =

content = content.replace(regex, properties.join('\n'))
.replace(/\$\{level}/g, options.parentLevel > 0 ? options.parentLevel : '')
content = overrideRule ? (properties.length ? content : '') : content
})

return content
Expand Down Expand Up @@ -386,7 +392,7 @@ const insertTupleMacros = (content, schema, module, title, options) => {

const getPrimitiveType = (type, templateDir = 'types') => {
const template = getTemplate(path.join(templateDir, type)) || getTemplate(path.join(templateDir, 'generic'))
return primitives[type] || template
return overrideRule === true ? (template || primitives[type]) : (primitives[type] || template)
}

const pickBestType = types => Array.isArray(types) ? types.find(t => t !== 'null') : types
Expand Down Expand Up @@ -435,12 +441,12 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', paren
}

const suffix = destination && ('.' + destination.split('.').pop()) || ''
const theTitle = insertSchemaMacros(getTemplate(path.join(templateDir, 'title' + suffix)), schema, module, { name: schema.title, parent, property, required, recursive: false})
const theTitle = insertSchemaMacros(getTemplate(path.join(templateDir, 'title' + suffix)), schema, module, { name: schema.title, parent, property, required, recursive: false })

let result = level === 0 ? getTemplate(path.join(templateDir, 'default' + suffix)) : '${shape}'

let genericTemplate = getTemplate(path.join(templateDir, 'generic' + suffix))
if (enums && level === 0 && Array.isArray(schema.enum) && ((schema.type === "string") || (schema.type[0] === "string"))) {
if (enums && Array.isArray(schema.enum) && ((schema.type === "string") || (schema.type[0] === "string"))) {
result = getTemplate(path.join(templateDir, 'enum' + suffix)) || genericTemplate
return insertSchemaMacros(insertEnumMacros(result, schema, module, theTitle, suffix, templateDir), schema, module, { name: theTitle, parent, property, required })
}
Expand Down Expand Up @@ -534,7 +540,7 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', paren
}
else if (schema.type === "array" && schema.items && !Array.isArray(schema.items)) {
// array
const items = getSchemaShape(schema.items, module, { templateDir, parent, property, required, parentLevel: parentLevel + 1, level, summary, descriptions, destination, enums: false , array: true})
const items = getSchemaShape(schema.items, module, { templateDir, parent, property, required, parentLevel: parentLevel + 1, level, summary, descriptions, destination, enums: false, array: true })
const shape = insertArrayMacros(getTemplate(path.join(templateDir, 'array' + suffix)) || genericTemplate, schema, module, level, items)
result = result.replace(/\$\{shape\}/g, shape)
.replace(/\$\{if\.object\}(.*?)\$\{end\.if\.object\}/gms, (schema.items.type === 'object') ? '$1' : '')
Expand All @@ -545,7 +551,7 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', paren
const shape = insertPrimitiveMacros(getTemplate(path.join(templateDir, 'primitive' + suffix) || genericTemplate), schema, module, theTitle, templateDir)
result = result.replace(/\$\{shape\}/g, shape)
if (level > 0) {
return insertSchemaMacros(result, schema, module, { name: theTitle, parent, property, required, templateDir})
return insertSchemaMacros(result, schema, module, { name: theTitle, parent, property, required, templateDir })
}
}

Expand Down Expand Up @@ -607,7 +613,7 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin

const suffix = destination && ('.' + destination.split('.').pop()) || ''
const namespaceStr = namespace ? getTemplate(path.join(templateDir, 'namespace' + suffix)) : ''
const theTitle = insertSchemaMacros(namespaceStr + getTemplate(path.join(templateDir, 'title' + suffix)), schema, module, { name: schema.title, parent: getXSchemaGroup(schema, module), recursive: false})
const theTitle = insertSchemaMacros(namespaceStr + getTemplate(path.join(templateDir, 'title' + suffix)), schema, module, { name: schema.title, parent: getXSchemaGroup(schema, module), recursive: false })
const allocatedProxy = event || result

const title = schema.type === "object" || schema.enum ? true : false
Expand Down Expand Up @@ -710,7 +716,7 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin
else if (!isTuple(schema)) {
const baseDir = (templateDir !== 'json-types' ? 'types': templateDir)
template = insertArrayMacros(getTemplate(path.join(baseDir, 'array')), schema, module)
template = insertSchemaMacros(template, schema.items, module, { name: getSchemaType(schema.items, module, {destination, templateDir, link, title, code, asPath, event, result, expandEnums, baseUrl, namespace }) })
template = insertSchemaMacros(template, schema.items, module, { name: getSchemaType(schema.items, module, {destination, templateDir, link, title, code, asPath, event, result, expandEnums, baseUrl, namespace })})
}
else {
template = insertTupleMacros(getTemplate(path.join(templateDir, 'tuple')), schema, module, '', { templateDir })
Expand Down Expand Up @@ -753,7 +759,7 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin
else if (schema.type) {
const template = getTemplate(path.join(templateDir, 'additionalProperties'))
if (schema.additionalProperties && template ) {
return insertSchemaMacros(getTemplate(path.join(templateDir, 'Title')), schema, module, { name: theTitle, recursive: false})
return insertSchemaMacros(getTemplate(path.join(templateDir, 'Title')), schema, module, { name: theTitle, recursive: false })
}
else {
// TODO: this assumes that when type is an array of types, that it's one other primative & 'null', which isn't necessarily true.
Expand Down Expand Up @@ -798,6 +804,7 @@ export default {
setPrimitives,
setConvertTuples,
setAllocatedPrimitiveProxies,
setOverrideRule,
getMethodSignatureParams,
getMethodSignatureResult,
getSchemaShape,
Expand Down
1 change: 1 addition & 0 deletions src/sdk/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const run = async ({
additionalSchemaTemplates: config.additionalSchemaTemplates,
additionalMethodTemplates: config.additionalMethodTemplates,
excludeDeclarations: config.excludeDeclarations,
overrideRule: config.overrideRule,
staticModuleNames: staticModuleNames,
hideExcluded: true,
aggregateFiles: config.aggregateFiles,
Expand Down

0 comments on commit 8d62732

Please sign in to comment.