From 8d627329ff6bec7d5a64e3c18dcaea83788738a5 Mon Sep 17 00:00:00 2001 From: HaseenaSainul Date: Wed, 25 Oct 2023 05:20:36 -0400 Subject: [PATCH] CPPSDK: 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 --- languages/cpp/language.config.json | 3 ++- src/macrofier/engine.mjs | 9 +++++---- src/macrofier/index.mjs | 2 ++ src/macrofier/types.mjs | 23 +++++++++++++++-------- src/sdk/index.mjs | 1 + 5 files changed, 25 insertions(+), 13 deletions(-) diff --git a/languages/cpp/language.config.json b/languages/cpp/language.config.json index debc539d..ef6d63b1 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, + "overrideRule": true, "aggregateFiles": [ "/include/firebolt.h", "/src/firebolt.cpp" diff --git a/src/macrofier/engine.mjs b/src/macrofier/engine.mjs index 4def4f6e..4e856c61 100644 --- a/src/macrofier/engine.mjs +++ b/src/macrofier/engine.mjs @@ -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)) @@ -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') @@ -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) @@ -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)) @@ -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) { diff --git a/src/macrofier/index.mjs b/src/macrofier/index.mjs index 04cdd9fb..6f2a6c4b 100644 --- a/src/macrofier/index.mjs +++ b/src/macrofier/index.mjs @@ -54,6 +54,7 @@ const macrofy = async ( additionalSchemaTemplates, additionalMethodTemplates, excludeDeclarations, + overrideRule, aggregateFiles, operators, primitives, @@ -110,6 +111,7 @@ const macrofy = async ( typer.setPrimitives(primitives) typer.setAllocatedPrimitiveProxies(allocatedPrimitiveProxies) typer.setConvertTuples(convertTuplesToArraysOrObjects) + typer.setOverrideRule(overrideRule) let templatesPermission = {} if (persistPermission) { diff --git a/src/macrofier/types.mjs b/src/macrofier/types.mjs index e7bd5e71..d5ce60e4 100644 --- a/src/macrofier/types.mjs +++ b/src/macrofier/types.mjs @@ -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 = { @@ -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 @@ -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 @@ -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 @@ -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 }) } @@ -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' : '') @@ -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 }) } } @@ -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 @@ -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 }) @@ -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. @@ -798,6 +804,7 @@ export default { setPrimitives, setConvertTuples, setAllocatedPrimitiveProxies, + setOverrideRule, getMethodSignatureParams, getMethodSignatureResult, getSchemaShape, diff --git a/src/sdk/index.mjs b/src/sdk/index.mjs index 763b6c9e..a465f84c 100755 --- a/src/sdk/index.mjs +++ b/src/sdk/index.mjs @@ -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,