Skip to content

Commit

Permalink
CPPSDK: handle primitive type with level 0 using primitive flag & rem…
Browse files Browse the repository at this point in the history
…ove override rule for properties insertion to object template
  • Loading branch information
HaseenaSainul committed Oct 27, 2023
1 parent 8d62732 commit 2c64bf9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 33 deletions.
1 change: 1 addition & 0 deletions languages/cpp/templates/json-types/object-empty-property.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
using ${title} = WPEFramework::Core::JSON::VariantContainer;
2 changes: 2 additions & 0 deletions languages/cpp/templates/types/object-empty-property.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* ${title} */
using ${title} = std::string;
14 changes: 7 additions & 7 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1252,16 +1252,16 @@ 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, level: 1, 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, level: 1, skipTitleOnce: true }) // w/out level: 1, 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, level: 1, skipTitleOnce: true }) // w/out level: 1, 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, level: 1, skipTitleOnce: true })).join('\n') : ''
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 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') : ''
// 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, destination: state.destination, section: state.section, level: 1, skipTitleOnce: true }) : ''
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 callbackResultInst = event ? types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-result-instantiation', property: result.name, destination: state.destination, section: state.section, level: 1, 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?
const callbackResponseInst = event ? types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-response-instantiation', property: result.name, destination: state.destination, section: state.section, level: 1, skipTitleOnce: true }) : ''
const callbackResponseInst = event ? types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-response-instantiation', property: result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) : ''
const resultType = result.schema ? types.getSchemaType(result.schema, json, { templateDir: state.typeTemplateDir }) : ''
const resultJsonType = result.schema ? types.getSchemaType(result.schema, json, { templateDir: 'json-types' }) : ''
const resultParams = generateResultParams(result.schema, json, templates, { name: result.name})
Expand Down
47 changes: 21 additions & 26 deletions src/macrofier/types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ function insertSchemaMacros(content, schema, module, { name = '', parent = '', p
.replace(/\$\{property\}/g, property)
.replace(/\$\{Property\}/g, capitalize(property))
.replace(/\$\{if\.namespace\.notsame}(.*?)\$\{end\.if\.namespace\.notsame\}/g, (module.info.title !== (parent || moduleTitle)) ? '$1' : '')
.replace(/\$\{parent\.title\}/g, parent)
.replace(/\$\{parent\.Title\}/g, capitalize(parent))
.replace(/\$\{parent\.title\}/g, parent || moduleTitle)
.replace(/\$\{parent\.Title\}/g, capitalize(parent || moduleTitle))
.replace(/\$\{if\.optional\}(.*?)\$\{end\.if\.optional\}/gms, required ? '' : '$1')
.replace(/\$\{if\.impl.optional\}(.*?)\$\{end\.if\.impl.optional\}/gms, required ? '' : '$1')
.replace(/\$\{description\}/g, schema.description ? schema.description : '')
Expand Down Expand Up @@ -205,7 +205,7 @@ const insertObjectAdditionalPropertiesMacros = (content, schema, module, title,
const options2 = options ? JSON.parse(JSON.stringify(options)) : {}
options2.parent = title
options2.level = options.level + 1

options2.required = options.required
const shape = getSchemaShape(schema.additionalProperties, module, options2)
let type = getSchemaType(schema.additionalProperties, module, options2).trimEnd()
const propertyNames = localizeDependencies(schema, module).propertyNames
Expand Down Expand Up @@ -238,14 +238,11 @@ const insertObjectMacros = (content, schema, module, title, property, options) =
options2.parentLevel = options.parentLevel
options2.level = options.level + 1
options2.templateDir = options.templateDir

;(['properties', 'properties.register', 'properties.assign']).forEach(macro => {
const indent = (content.split('\n').find(line => line.includes("${" + macro + "}")) || '').match(/^\s+/) || [''][0]
const templateType = macro.split('.').slice(1).join('')
const template = getTemplate(path.join(options.templateDir, 'property' + (templateType ? `-${templateType}` : ''))).replace(/\n/gms, indent + '\n')

const properties = []

if (schema.properties) {
Object.entries(schema.properties).forEach(([name, prop], i) => {
let localizedProp = localizeDependencies(prop, module)
Expand All @@ -257,7 +254,6 @@ const insertObjectMacros = (content, schema, module, title, property, options) =
const schemaShape = getSchemaShape(prop, module, options2)

const type = getSchemaType(prop, module, options2)

// don't push properties w/ unsupported types
if (type) {
let replacedTemplate = template
Expand Down Expand Up @@ -297,7 +293,7 @@ const insertObjectMacros = (content, schema, module, title, property, options) =
}
})
}

if (schema.propertyNames) {
const { propertyNames } = localizeDependencies(schema, module)
if (propertyNames.enum) {
Expand Down Expand Up @@ -339,14 +335,15 @@ const insertObjectMacros = (content, schema, module, title, property, options) =
})
}
}

const regex = new RegExp("\\$\\{" + macro + "\\}", "g")

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

if (!schema.properties && ! schema.propertyNames) {
content = !schema.additionalProperties ? getTemplate(path.join(options.templateDir, 'object-empty-property')) : ''
}
})
return content
}

Expand Down Expand Up @@ -430,31 +427,29 @@ 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 } = {}) {
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 } = {}) {
schema = sanitize(schema)

state.destination = destination
state.section = section

if (level === 0 && !schema.title) {
if (level === 0 && !schema.title && !primitive) {
return ''
}

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 })

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

let genericTemplate = getTemplate(path.join(templateDir, 'generic' + suffix))
if (enums && Array.isArray(schema.enum) && ((schema.type === "string") || (schema.type[0] === "string"))) {
if (enums && level === 0 && 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 })
}

if (schema['$ref']) {
const someJson = getPath(schema['$ref'], module)
if (someJson) {
return getSchemaShape(someJson, module, { templateDir, parent, property, required, parentLevel, level, summary, descriptions, destination, enums, array })
return getSchemaShape(someJson, module, { templateDir, parent, property, required, parentLevel, level, summary, descriptions, destination, enums, array, primitive })
}
throw "Unresolvable $ref: " + schema['ref'] + ", in " + module.info.title
}
Expand All @@ -466,7 +461,7 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', paren
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, parent, property, required, parentLevel, level, summary, descriptions, destination, enums: enumType, array })
const innerShape = getSchemaShape(schema, module, { skipTitleOnce: true, templateDir, parent, property, required, parentLevel, level, summary, descriptions, destination, enums: enumType, array, primitive })

const shape = getTemplate(path.join(templateDir, 'ref' + suffix))
.replace(/\$\{shape\}/g, innerShape)
Expand All @@ -478,11 +473,11 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', paren
let shape
const additionalPropertiesTemplate = getTemplate(path.join(templateDir, 'additionalProperties'))
if (additionalPropertiesTemplate && schema.additionalProperties && (typeof schema.additionalProperties === 'object')) {
shape = insertObjectAdditionalPropertiesMacros(additionalPropertiesTemplate, schema, module, theTitle, { level, parent, templateDir, namespace: true })
shape = insertObjectAdditionalPropertiesMacros(additionalPropertiesTemplate, schema, module, theTitle, { level, parent, templateDir, namespace: true, required })
}
else {
let objectLevel = array ? 0 : level
shape = insertObjectMacros(getTemplate(path.join(templateDir, 'object' + (array ? '-array' : '') + suffix)) || genericTemplate, schema, module, theTitle, property, { parentLevel, level: objectLevel, parent, property, required, templateDir, descriptions, destination, section, enums, namespace: true })
shape = insertObjectMacros(getTemplate(path.join(templateDir, 'object' + (array ? '-array' : '') + suffix)) || genericTemplate, schema, module, theTitle, property, { parentLevel, level: objectLevel, parent, property, required, templateDir, descriptions, destination, section, enums, namespace: true, primitive })
}
result = result.replace(/\$\{shape\}/g, shape)
return insertSchemaMacros(result, schema, module, { name: theTitle, parent, property, required, templateDir })
Expand Down Expand Up @@ -530,7 +525,7 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', paren
}
delete union['$ref']

return getSchemaShape(union, module, { templateDir, parent, property, required, parentLevel, level, summary, descriptions, destination, enums: false, array })
return getSchemaShape(union, module, { templateDir, parent, property, required, parentLevel, level, summary, descriptions, destination, enums: false, array, primitive })
}
else if (schema.type === "array" && schema.items && isSupportedTuple(schema)) {
// tuple
Expand All @@ -540,8 +535,8 @@ 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 shape = insertArrayMacros(getTemplate(path.join(templateDir, 'array' + suffix)) || genericTemplate, schema, module, level, items)
const items = getSchemaShape(schema.items, module, { templateDir, parent, property, required, parentLevel: parentLevel + 1, level, summary, descriptions, destination, enums: false, array: true, primitive })
const shape = insertArrayMacros(getTemplate(path.join(templateDir, 'array' + suffix)) || genericTemplate, schema, module, level, items, schema.required)
result = result.replace(/\$\{shape\}/g, shape)
.replace(/\$\{if\.object\}(.*?)\$\{end\.if\.object\}/gms, (schema.items.type === 'object') ? '$1' : '')
.replace(/\$\{if\.non\.object\}(.*?)\$\{end\.if\.non\.object\}/gms, (schema.items.type !== 'object') ? '$1' : '')
Expand All @@ -550,7 +545,7 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', paren
else if (schema.type) {
const shape = insertPrimitiveMacros(getTemplate(path.join(templateDir, 'primitive' + suffix) || genericTemplate), schema, module, theTitle, templateDir)
result = result.replace(/\$\{shape\}/g, shape)
if (level > 0) {
if (level > 0 || primitive) {
return insertSchemaMacros(result, schema, module, { name: theTitle, parent, property, required, templateDir })
}
}
Expand Down

0 comments on commit 2c64bf9

Please sign in to comment.