Skip to content

Commit

Permalink
cppsdK: use primitive types from templates, instead defined variable
Browse files Browse the repository at this point in the history
  • Loading branch information
HaseenaSainul committed Oct 30, 2023
1 parent 2c64bf9 commit ba417b3
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 27 deletions.
8 changes: 1 addition & 7 deletions 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,
"overrideRule": true,
"aggregateFiles": [
"/include/firebolt.h",
"/src/firebolt.cpp"
Expand All @@ -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" ]
Expand Down
1 change: 1 addition & 0 deletions languages/cpp/templates/types/boolean.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bool
1 change: 1 addition & 0 deletions languages/cpp/templates/types/integer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int32_t
1 change: 1 addition & 0 deletions languages/cpp/templates/types/number.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
float
1 change: 1 addition & 0 deletions languages/cpp/templates/types/string.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std::string
8 changes: 4 additions & 4 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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?
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,
overrideRule,
aggregateFiles,
operators,
primitives,
Expand Down Expand Up @@ -111,7 +110,6 @@ const macrofy = async (
typer.setPrimitives(primitives)
typer.setAllocatedPrimitiveProxies(allocatedPrimitiveProxies)
typer.setConvertTuples(convertTuplesToArraysOrObjects)
typer.setOverrideRule(overrideRule)

let templatesPermission = {}
if (persistPermission) {
Expand Down
23 changes: 10 additions & 13 deletions src/macrofier/types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,28 @@ 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) {
Object.assign(templates, t)
}

function setPrimitives(p) {
Object.assign(primitives, p)
if (p) {
primitives = p
}
}

function setConvertTuples(t) {
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -799,7 +797,6 @@ export default {
setPrimitives,
setConvertTuples,
setAllocatedPrimitiveProxies,
setOverrideRule,
getMethodSignatureParams,
getMethodSignatureResult,
getSchemaShape,
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,
overrideRule: config.overrideRule,
staticModuleNames: staticModuleNames,
hideExcluded: true,
aggregateFiles: config.aggregateFiles,
Expand Down

0 comments on commit ba417b3

Please sign in to comment.