Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CPPSDK: parameter and result handling changes #136

Merged
merged 7 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion languages/cpp/templates/json-types/anyOf.c
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/* AnyOf is not supported in C: ${title} */
WPEFramework::Core::JSON::VariantContainer
1 change: 1 addition & 0 deletions languages/cpp/templates/json-types/anyOfSchema.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SKIP
2 changes: 2 additions & 0 deletions languages/cpp/templates/parameter-serialization/generic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
WPEFramework::Core::JSON::Variant ${Property} = ${property};
jsonParameters.Set(_T("${property}"), ${Property});
Empty file.
1 change: 0 additions & 1 deletion languages/cpp/templates/parameters/result.h

This file was deleted.

2 changes: 1 addition & 1 deletion languages/cpp/templates/types/anyOf.c
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/* AnyOf is not supported in CPP: ${title} */
string
1 change: 1 addition & 0 deletions languages/cpp/templates/types/anyOfSchema.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SKIP
2 changes: 1 addition & 1 deletion languages/cpp/templates/types/tuple.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/* ${title} */
std::pair<${type},${type}> ${title};
std::pair<${items}> ${title};
6 changes: 4 additions & 2 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ function generateSchemas(json, templates, options) {
else {
content = content.replace(/.*\$\{schema.seeAlso\}/, '')
}
// content = content.trim().length ? content.trimEnd() : content.trim()
content = content.trim().length ? content : content.trim()
jlacivita marked this conversation as resolved.
Show resolved Hide resolved

const isEnum = x => x.type === 'string' && Array.isArray(x.enum) && x.title

Expand Down Expand Up @@ -1245,6 +1245,7 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {})
.replace(/\$\{method\.params\.count}/g, methodObj.params ? methodObj.params.length : 0)
.replace(/\$\{if\.params\}(.*?)\$\{end\.if\.params\}/gms, method.params.length ? '$1' : '')
.replace(/\$\{if\.result\}(.*?)\$\{end\.if\.result\}/gms, resultType ? '$1' : '')
.replace(/\$\{if\.result.nonvoid\}(.*?)\$\{end\.if\.result.nonvoid\}/gms, resultType && resultType !== 'void' ? '$1' : '')
.replace(/\$\{if\.result\.properties\}(.*?)\$\{end\.if\.result\.properties\}/gms, resultParams ? '$1' : '')
.replace(/\$\{if\.params\.empty\}(.*?)\$\{end\.if\.params\.empty\}/gms, method.params.length === 0 ? '$1' : '')
.replace(/\$\{if\.signature\.empty\}(.*?)\$\{end\.if\.signature\.empty\}/gms, (method.params.length === 0 && resultType === '') ? '$1' : '')
Expand All @@ -1253,6 +1254,7 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {})
.replace(/\$\{method\.params\.serialization\.with\.indent\}/g, indent(serializedParams, ' '))
// Typed signature stuff
.replace(/\$\{method\.signature\.params\}/g, types.getMethodSignatureParams(methodObj, json, { destination: state.destination, section: state.section }))
.replace(/\$\{method\.signature\.result\}/g, types.getMethodSignatureResult(methodObj, json, { destination: state.destination, section: state.section }))
.replace(/\$\{method\.context\}/g, method.context.join(', '))
.replace(/\$\{method\.context\.array\}/g, JSON.stringify(method.context))
.replace(/\$\{method\.context\.count}/g, method.context ? method.context.length : 0)
Expand Down Expand Up @@ -1287,7 +1289,7 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {})
.replace(/\$\{method\.result\.type\}/g, types.getSchemaType(result.schema, json, { name: result.name, templateDir: state.typeTemplateDir, title: true, asPath: false, destination: state.destination, result: true })) //, baseUrl: options.baseUrl
.replace(/\$\{method\.result\.json\}/, types.getSchemaType(result.schema, json, { name: result.name, 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\}/, isEventMethod(methodObj) ? types.getSchemaType(result.schema, json, { name: result.name, prefix: method.alternative, templateDir: state.typeTemplateDir, destination: state.destination, event: true, description: methodObj.result.summary, asPath: false }) : '')
.replace(/\$\{event\.result\.type\}/g, isEventMethod(methodObj) ? types.getMethodSignatureResult(event, json, { destination: state.destination, section: state.section }) : '')
.replace(/\$\{event\.result\.json\.type\}/g, resultJsonType)
.replace(/\$\{event\.result\.json\.type\}/g, callbackResultJsonType)
.replace(/\$\{event\.pulls\.param\.name\}/g, pullsEventParamName)
Expand Down
90 changes: 70 additions & 20 deletions src/macrofier/types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const primitives = {
"string": "string"
}

const isVoid = type => (type === 'void') ? true : false
const isPrimitiveType = type => primitives[type] ? true : false
const allocatedPrimitiveProxies = {}

function setTemplates(t) {
Expand All @@ -53,17 +55,48 @@ const safeName = value => value.split(':').pop().replace(/[\.\-]/g, '_').replace

// TODO: This is what's left of getMethodSignatureParams. We need to figure out / handle C's `FireboltTypes_StringHandle`
function getMethodSignatureParams(method, module, { destination, callback }) {
const paramRequired = getTemplate('/parameters/default')
const paramOptional = getTemplate('/parameters/optional')
return method.params.map(param => {
let type = getSchemaType(param.schema, module, { destination })
let type = getSchemaType(param.schema, module, { destination, namespace : true })
if (callback && allocatedPrimitiveProxies[type]) {
type = allocatedPrimitiveProxies[type]
}

let paramRequired = ''
let jsonType = getJsonType(param.schema, module, { destination })
if (!isPrimitiveType(jsonType) && getTemplate('/parameters/nonprimitive')) {
paramRequired = getTemplate('/parameters/nonprimitive')
}
else if ((jsonType === 'string') && getTemplate('/parameters/string')) {
paramRequired = getTemplate('/parameters/string')
}
else {
paramRequired = getTemplate('/parameters/default')
}

return (param.required ? paramRequired : paramOptional).replace(/\$\{method\.param\.name\}/g, param.name).replace(/\$\{method\.param\.type\}/g, type)
}).join(', ')
}

function getMethodSignatureResult(method, module, { destination, callback }) {
let type = getSchemaType(method.result.schema, module, { destination, namespace : true })

let result = ''
let jsonType = getJsonType(method.result.schema, module, { destination })

if (!isVoid(type) && !isPrimitiveType(jsonType) && getTemplate('/result/nonprimitive')) {
result = getTemplate('/result/nonprimitive')
}
else if ((jsonType === 'string') && getTemplate('/result/string')) {
result = getTemplate('/result/string')
}
else {
result = getTemplate('/result/default')
}

return result.replace(/\$\{method\.result\.type\}/g, type)
}

const getTemplate = (name) => {
if (name[0] !== '/') {
name = '/' + name
Expand Down Expand Up @@ -120,8 +153,6 @@ const insertConstMacros = (content, schema, module, name) => {
return content
}



const insertEnumMacros = (content, schema, module, name) => {
const template = content.split('\n')

Expand Down Expand Up @@ -251,6 +282,7 @@ const insertTupleMacros = (content, schema, module, title, options) => {
const itemsTemplate = getTemplate(path.join(options.templateDir, 'items'))
const propIndent = (content.split('\n').find(line => line.includes("${properties}")) || '').match(/^\s+/) || [''][0]
const itemsIndent = (content.split('\n').find(line => line.includes("${items}")) || '').match(/^\s+/) || [''][0]
const delimiter = getTemplate(path.join(options.templateDir, 'delimiter'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delimiter is a pretty generic name. Maybe tuple-delimiter would be safer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


const doMacroWork = (str, prop, i, indent) => {
const schemaShape = getSchemaShape(prop, module, options)
Expand All @@ -267,8 +299,8 @@ const insertTupleMacros = (content, schema, module, title, options) => {
.replace(/\$\{if\.optional\}(.*?)\$\{end\.if\.optional\}/gms, '')
}

content = content.replace(/\$\{properties\}/g, schema.items.map((prop, i) => doMacroWork(propTemplate, prop, i, propIndent)).join('\n'))
content = content.replace(/\$\{items\}/g, schema.items.map((prop, i) => doMacroWork(itemsTemplate, prop, i, itemsIndent)).join('\n'))
content = content.replace(/\$\{properties\}/g, schema.items.map((prop, i) => doMacroWork(propTemplate, prop, i, propIndent)).join(delimiter))
content = content.replace(/\$\{items\}/g, schema.items.map((prop, i) => doMacroWork(itemsTemplate, prop, i, itemsIndent)).join(delimiter))

return content
}
Expand All @@ -287,10 +319,13 @@ const insertPrimitiveMacros = (content, schema, module, name, templateDir) => {

const insertAnyOfMacros = (content, schema, module, name) => {
const itemTemplate = content
content = schema.anyOf.map((item, i) => itemTemplate
.replace(/\$\{type\}/g, getSchemaType(item, module))
.replace(/\$\{delimiter\}(.*?)\$\{end.delimiter\}/g, i === schema.anyOf.length - 1 ? '' : '$1')
).join('')
if (content.split('\n').find(line => line.includes("${type}"))) {
content = schema.anyOf.map((item, i) => itemTemplate
.replace(/\$\{type\}/g, getSchemaType(item, module))
.replace(/\$\{delimiter\}(.*?)\$\{end.delimiter\}/g, i === schema.anyOf.length - 1 ? '' : '$1')
).join('')
}

return content
}

Expand Down Expand Up @@ -359,14 +394,27 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', name
return insertSchemaMacros(result, schema, module, theTitle, parent, property)
}
else if (schema.anyOf || schema.oneOf) {
// borrow anyOf logic, note that schema is a copy, so we're not breaking it.
if (!schema.anyOf) {
schema.anyOf = schema.oneOf
const template = getTemplate(path.join(templateDir, 'anyOfSchema' + suffix))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need both an anyOfSchema template and an anyOf template?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In CPP, shape and type has to be handled differently. so two templates are required. Currently anyOf is treating as string, so shape is not required. so adding one more two handle shape part

let shape
if (template) {
if (!template.includes('SKIP')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We generally just use empty templates for this... what's this 'SKIP' doing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SKIP is added to skip anyOf schema shape generation, since for CPP it is treating as string currently, so shape generation is not required. Updated template with comments

shape = insertAnyOfMacros(template, schema, module, theTitle)
}
}
else {
// borrow anyOf logic, note that schema is a copy, so we're not breaking it.
if (!schema.anyOf) {
schema.anyOf = schema.oneOf
}
shape = insertAnyOfMacros(getTemplate(path.join(templateDir, 'anyOf' + suffix)), schema, module, theTitle)
}
if (shape) {
result = result.replace(/\$\{shape\}/g, shape)
return insertSchemaMacros(result, schema, module, theTitle, parent, property)
}
else {
return ''
}
const shape = insertAnyOfMacros(getTemplate(path.join(templateDir, 'anyOf' + suffix)), schema, module, theTitle)

result = result.replace(/\$\{shape\}/g, shape)
return insertSchemaMacros(result, schema, module, theTitle, parent, property)
}
else if (schema.allOf) {
const merger = (key) => function (a, b) {
Expand Down Expand Up @@ -464,7 +512,7 @@ const isSupportedTuple = schema => {
}
}

function getSchemaType(schema, module, { destination, templateDir = 'types', link = false, code = false, asPath = false, event = false, result = false, expandEnums = true, baseUrl = '', namespace = false } = {}) {
function getSchemaType(schema, module, { destination, templateDir = 'types', link = false, code = false, asPath = false, event = false, result = false, expandEnums = true, baseUrl = '', namespace = false, name = '' } = {}) {
const wrap = (str, wrapper) => wrapper + str + wrapper

schema = sanitize(schema)
Expand All @@ -479,8 +527,9 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin
if (schema['$ref']) {
if (schema['$ref'][0] === '#') {
const refSchema = getPath(schema['$ref'], module)
const refName = refSchema.title || schema['$ref'].split('/').pop()
const includeNamespace = (module.info.title !== getXSchemaGroup(refSchema, module))
return getSchemaType(refSchema, module, {destination, templateDir, link, title, code, asPath, event, result, expandEnums, baseUrl, namespace:includeNamespace })// { link: link, code: code, destination })
return getSchemaType(refSchema, module, {destination, templateDir, link, title, code, asPath, event, result, expandEnums, baseUrl, namespace:includeNamespace, name:refName })// { link: link, code: code, destination })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spent a lot of time removing the name param from getSchemaType.

There's a method to generate names for every un-titled schema that runs before all of the code generation. Is it missing a use case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

}
else {
// TODO: This never happens... but might be worth keeping in case we link to an opaque external schema at some point?
Expand Down Expand Up @@ -640,8 +689,9 @@ export default {
setConvertTuples,
setAllocatedPrimitiveProxies,
getMethodSignatureParams,
getMethodSignatureResult,
getSchemaShape,
getSchemaType,
getJsonType,
getSchemaInstantiation
}
}