From ba417b3463d19f0e3f7dd402466e25a71d7b1fdd Mon Sep 17 00:00:00 2001 From: HaseenaSainul Date: Fri, 27 Oct 2023 11:47:59 -0400 Subject: [PATCH] cppsdK: use primitive types from templates, instead defined variable --- languages/cpp/language.config.json | 8 +------- languages/cpp/templates/types/boolean.h | 1 + languages/cpp/templates/types/integer.h | 1 + languages/cpp/templates/types/number.h | 1 + languages/cpp/templates/types/string.h | 1 + src/macrofier/engine.mjs | 8 ++++---- src/macrofier/index.mjs | 2 -- src/macrofier/types.mjs | 23 ++++++++++------------- src/sdk/index.mjs | 1 - 9 files changed, 19 insertions(+), 27 deletions(-) create mode 100644 languages/cpp/templates/types/boolean.h create mode 100644 languages/cpp/templates/types/integer.h create mode 100644 languages/cpp/templates/types/number.h create mode 100644 languages/cpp/templates/types/string.h diff --git a/languages/cpp/language.config.json b/languages/cpp/language.config.json index ef6d63b1..db9b7ddc 100644 --- a/languages/cpp/language.config.json +++ b/languages/cpp/language.config.json @@ -6,7 +6,6 @@ "unwrapResultObjects": false, "createPolymorphicMethods": true, "excludeDeclarations": true, - "overrideRule": true, "aggregateFiles": [ "/include/firebolt.h", "/src/firebolt.cpp" @@ -22,12 +21,7 @@ "/src/jsondata_module.h" ], "persistPermission": true, - "primitives": { - "boolean": "bool", - "integer": "int32_t", - "number": "float", - "string": "std::string" - }, + "primitives": {}, "langVersion" : "c++17", "additionalSchemaTemplates": [ "json-types" ], "additionalMethodTemplates": [ "declarations", "declarations-override" ] diff --git a/languages/cpp/templates/types/boolean.h b/languages/cpp/templates/types/boolean.h new file mode 100644 index 00000000..fc75f206 --- /dev/null +++ b/languages/cpp/templates/types/boolean.h @@ -0,0 +1 @@ +bool \ No newline at end of file diff --git a/languages/cpp/templates/types/integer.h b/languages/cpp/templates/types/integer.h new file mode 100644 index 00000000..33028e0f --- /dev/null +++ b/languages/cpp/templates/types/integer.h @@ -0,0 +1 @@ +int32_t \ No newline at end of file diff --git a/languages/cpp/templates/types/number.h b/languages/cpp/templates/types/number.h new file mode 100644 index 00000000..05eeb48f --- /dev/null +++ b/languages/cpp/templates/types/number.h @@ -0,0 +1 @@ +float \ No newline at end of file diff --git a/languages/cpp/templates/types/string.h b/languages/cpp/templates/types/string.h new file mode 100644 index 00000000..c6d1c815 --- /dev/null +++ b/languages/cpp/templates/types/string.h @@ -0,0 +1 @@ +std::string \ No newline at end of file diff --git a/src/macrofier/engine.mjs b/src/macrofier/engine.mjs index 2e8f6704..031e61ea 100644 --- a/src/macrofier/engine.mjs +++ b/src/macrofier/engine.mjs @@ -1252,12 +1252,12 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {}) const pullsResultType = pullsResult && types.getSchemaShape(pullsResult, json, { destination: state.destination, templateDir: state.typeTemplateDir, section: state.section }) const pullsForType = pullsResult && types.getSchemaType(pullsResult, json, { destination: state.destination, templateDir: state.typeTemplateDir, section: state.section }) const pullsParamsType = pullsParams ? types.getSchemaShape(pullsParams, json, { destination: state.destination, templateDir: state.typeTemplateDir, section: state.section }) : '' - const serializedParams = flattenedMethod.params.map(param => types.getSchemaShape(param.schema, json, { templateDir: 'parameter-serialization', property: param.name, required: param.required || false, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true })).join('\n') - const resultInst = types.getSchemaShape(flattenedMethod.result.schema, json, { templateDir: 'result-instantiation', property: flattenedMethod.result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) // w/out primitive: true, getSchemaShape skips anonymous types, like primitives + const serializedParams = flattenedMethod.params.map(param => types.getSchemaShape(param.schema, json, { templateDir: 'parameter-serialization', property: param.name, required: param.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true })).join('\n') + const resultInst = types.getSchemaShape(flattenedMethod.result.schema, json, { templateDir: 'result-instantiation', property: flattenedMethod.result.name, required: flattenedMethod.result.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) // w/out primitive: true, getSchemaShape skips anonymous types, like primitives const resultInit = types.getSchemaShape(flattenedMethod.result.schema, json, { templateDir: 'result-initialization', property: flattenedMethod.result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) // w/out primitive: true, 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, required: param.required || false, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true })).join('\n') : '' + const serializedEventParams = event ? flattenedMethod.params.filter(p => p.name !== 'listen').map(param => types.getSchemaShape(param.schema, json, {templateDir: 'parameter-serialization', property: param.name, required: param.required, destination: state.destination, section: state.section, primitive: true, 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: 'callback-parameter-serialization', property: result.name, required: event.result.schema.required || false, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) : '' + const callbackSerializedParams = event ? types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-parameter-serialization', property: result.name, required: event.result.schema.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) : '' const callbackResultInst = event ? types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-result-instantiation', property: result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) : '' // 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 6f2a6c4b..04cdd9fb 100644 --- a/src/macrofier/index.mjs +++ b/src/macrofier/index.mjs @@ -54,7 +54,6 @@ const macrofy = async ( additionalSchemaTemplates, additionalMethodTemplates, excludeDeclarations, - overrideRule, aggregateFiles, operators, primitives, @@ -111,7 +110,6 @@ 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 4dfede00..4ef38043 100644 --- a/src/macrofier/types.mjs +++ b/src/macrofier/types.mjs @@ -21,18 +21,18 @@ import { getPath, localizeDependencies } from '../shared/json-schema.mjs' import path from "path" let convertTuplesToArraysOrObjects = false -let overrideRule = false const templates = {} const state = {} -const primitives = { +let primitives = { "integer": "number", "number": "number", "boolean": "boolean", "string": "string" } +const stdPrimitives = [ "integer", "number", "boolean", "string" ] const isVoid = type => (type === 'void') ? true : false -const isPrimitiveType = type => primitives[type] ? true : false +const isPrimitiveType = type => stdPrimitives.includes(type) ? true : false const allocatedPrimitiveProxies = {} function setTemplates(t) { @@ -40,7 +40,9 @@ function setTemplates(t) { } function setPrimitives(p) { - Object.assign(primitives, p) + if (p) { + primitives = p + } } function setConvertTuples(t) { @@ -51,10 +53,6 @@ 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 @@ -144,7 +142,7 @@ const getXSchemaGroup = (schema, module) => { return group } -function insertSchemaMacros(content, schema, module, { name = '', parent = '', property = '', required = true, recursive = true, templateDir = 'types'}) { +function insertSchemaMacros(content, schema, module, { name = '', parent = '', property = '', required = false, recursive = true, templateDir = 'types'}) { const title = name || schema.title || '' let moduleTitle = getXSchemaGroup(schema, module) @@ -250,7 +248,7 @@ const insertObjectMacros = (content, schema, module, title, property, options) = options2.templateDir += subProperty ? '/sub-property' : '' const objSeparator = getTemplate(path.join(options2.templateDir, 'object-separator')) - options2.required = prop.required ? prop.required : false + options2.required = schema.required && schema.required.includes(name) const schemaShape = getSchemaShape(prop, module, options2) const type = getSchemaType(prop, module, options2) @@ -389,7 +387,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 overrideRule === true ? (template || primitives[type]) : (primitives[type] || template) + return (primitives[type] || template) } const pickBestType = types => Array.isArray(types) ? types.find(t => t !== 'null') : types @@ -427,7 +425,7 @@ const sanitize = (schema) => { return result } -function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', parent = '', property = '', required = true, parentLevel = 0, level = 0, summary, descriptions = true, destination, section, enums = true, skipTitleOnce = false, array = false, primitive = false } = {}) { +function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', parent = '', property = '', required = false, parentLevel = 0, level = 0, summary, descriptions = true, destination, section, enums = true, skipTitleOnce = false, array = false, primitive = false } = {}) { schema = sanitize(schema) state.destination = destination state.section = section @@ -799,7 +797,6 @@ export default { setPrimitives, setConvertTuples, setAllocatedPrimitiveProxies, - setOverrideRule, getMethodSignatureParams, getMethodSignatureResult, getSchemaShape, diff --git a/src/sdk/index.mjs b/src/sdk/index.mjs index a465f84c..763b6c9e 100755 --- a/src/sdk/index.mjs +++ b/src/sdk/index.mjs @@ -70,7 +70,6 @@ const run = async ({ additionalSchemaTemplates: config.additionalSchemaTemplates, additionalMethodTemplates: config.additionalMethodTemplates, excludeDeclarations: config.excludeDeclarations, - overrideRule: config.overrideRule, staticModuleNames: staticModuleNames, hideExcluded: true, aggregateFiles: config.aggregateFiles,