Skip to content

Commit

Permalink
fixes to generate array inside subschemas
Browse files Browse the repository at this point in the history
  • Loading branch information
HaseenaSainul committed Jul 12, 2023
1 parent 4d22a4d commit b7e8758
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 65 deletions.
35 changes: 17 additions & 18 deletions languages/c/Types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import deepmerge from 'deepmerge'
import { getPath } from '../../src/shared/json-schema.mjs'
import { getTypeName, getModuleName, description, getObjectHandleManagement, getNativeType, getPropertyAccessors, capitalize, isOptional, generateEnum, getMapAccessors, getArrayAccessors, getArrayElementSchema, getPropertyGetterSignature, getPropertyEventCallbackSignature, getPropertyEventRegisterSignature, getPropertyEventUnregisterSignature, getPropertySetterSignature, getFireboltStringType } from './src/types/NativeHelpers.mjs'
import { getTypeName, getModuleName, description, getObjectHandleManagement, getNativeType, getPropertyAccessors, capitalize, isOptional, generateEnum, getMapAccessors, getArrayAccessors, getPropertyGetterSignature, getPropertyEventCallbackSignature, getPropertyEventRegisterSignature, getPropertyEventUnregisterSignature, getPropertySetterSignature, getFireboltStringType } from './src/types/NativeHelpers.mjs'
import { getArrayAccessorsImpl, getMapAccessorsImpl, getObjectHandleManagementImpl, getParameterInstantiation, getPropertyAccessorsImpl, getResultInstantiation, getCallbackParametersInstantiation, getCallbackResultInstantiation, getCallbackResponseInstantiation } from './src/types/ImplHelpers.mjs'
import { getJsonContainerDefinition, getJsonDataStructName, getJsonDataPrefix } from './src/types/JSONHelpers.mjs'

Expand Down Expand Up @@ -220,7 +220,8 @@ function getMethodSignatureParams(method, module, { destination, callback = fals
if ((callback === true) && (type === 'char*')) {
type = getFireboltStringType()
}
signatureParams += type + (!param.required ? '* ' : ' ') + param.name

signatureParams += type + ((!param.required && !type.includes('_t') && (type !== 'char*')) ? '* ' : ' ') + param.name
})
return signatureParams
}
Expand Down Expand Up @@ -302,8 +303,10 @@ function getSchemaTypeInfo(module = {}, json = {}, name = '', schemas = {}, pref
res = getSchemaTypeInfo(module, json.items, json.items.name || name, schemas, prefix)
}


prefix = prefix ? prefix + name : name
let n = getTypeName(getModuleName(module), capitalize(res.name), prefix)
structure.name = res.name || name && (capitalize(name))
structure.name = name ? name + capitalize(res.name) : res.name
structure.type = n + 'Array_t'
structure.json = json
structure.namespace = getModuleName(module)
Expand Down Expand Up @@ -525,24 +528,19 @@ function getSchemaShapeInfo(json, module, schemas = {}, { name = '', prefix = ''
else {
j = json.items
}
shape += getSchemaShapeInfo(j, module, schemas, { name: j.title || name, prefix, merged, level, title, summary, descriptions, destination, section, enums })

if (!isCPP) {
let info = getSchemaTypeInfo(module, j, j.title || name, schemas, prefix, {level : level, descriptions: descriptions, title: true})
let subPrefix = prefix ? prefix + name : name
let info = getSchemaTypeInfo(module, j, j.title || name, schemas, subPrefix, {level : level, descriptions: descriptions, title: true})

if (info.type && info.type.length > 0) {
let type = getArrayElementSchema(json, module, schemas, info.name)
let arrayName = capitalize(info.name) + capitalize(type.type)
let namespace = info.namespace
if (type && type.type === 'object') {
namespace = getModuleName(module)
}
let objName = getTypeName(namespace, arrayName, prefix)
let arrayName = capitalize(info.name);
let objName = getTypeName(getModuleName(module), arrayName, subPrefix)
let tName = objName + 'Array'

info.json.namespace = info.namespace
let moduleProperty = getJsonTypeInfo(module, json, json.title || name, schemas, prefix)
let subModuleProperty = getJsonTypeInfo(module, j, j.title, schemas, prefix)
let subModuleProperty = getJsonTypeInfo(module, j, j.title || name, schemas, prefix)
let t = ''
if (level === 0) {
t += description(capitalize(info.name), json.description) + '\n'
Expand Down Expand Up @@ -666,6 +664,7 @@ function getJsonTypeInfo(module = {}, json = {}, name = '', schemas, prefix = ''
items = json.items
// grab the type for the non-array schema
}

res = getJsonTypeInfo(module, items, items.name || name, schemas, prefix)
structure.deps = res.deps
structure.type.push(`WPEFramework::Core::JSON::ArrayType<${res.type}>`)
Expand Down Expand Up @@ -733,7 +732,7 @@ const enumReducer = (acc, val, i, arr) => {
return acc
}

function getSchemaInstantiation(schema, module, name, { instantiationType = '' } = {}) {
function getSchemaInstantiation(schema, module, name, { instantiationType = '', prefix = '' } = {}) {

if (instantiationType === 'params') {
return getParameterInstantiation(getParamList(schema, module))
Expand All @@ -752,17 +751,17 @@ function getSchemaInstantiation(schema, module, name, { instantiationType = '' }
return result
}
else if (instantiationType === 'callback.params') {
let resultJsonType = getJsonType(schema.result.schema, module, { name: schema.result.name }) || ''
let resultJsonType = getJsonType(schema.result.schema, module, { name: schema.result.name, prefix }) || ''
return getCallbackParametersInstantiation(getParamList(schema, module), resultJsonType)
}
else if (instantiationType === 'callback.result') {
let resultType = getSchemaType(schema.result.schema, module, { title: true, name: schema.result.name, resultSchema: true}) || ''
let resultType = getSchemaType(schema.result.schema, module, { title: true, name: schema.result.name, prefix, resultSchema: true}) || ''
let resultJsonType = getJsonType(schema.result.schema, module, { name: schema.result.name }) || ''
return getCallbackResultInstantiation(resultType, resultJsonType)
}
else if (instantiationType === 'callback.response') {
let resultType = getSchemaType(schema.result.schema, module, { title: true, name: schema.result.name, resultSchema: true}) || ''
let resultJsonType = getJsonType(schema.result.schema, module, { name: schema.result.name }) || ''
let resultType = getSchemaType(schema.result.schema, module, { title: true, name: schema.result.name, prefix, resultSchema: true}) || ''
let resultJsonType = getJsonType(schema.result.schema, module, { name: schema.result.name, prefix }) || ''
return getCallbackResponseInstantiation(getParamList(schema, module), resultType, resultJsonType)
}
else if (instantiationType === 'pull.param.name') {
Expand Down
4 changes: 2 additions & 2 deletions languages/c/src/types/ImplHelpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const getPropertyAccessorsImpl = (objName, modulePropertyType, subPropertyType,
WPEFramework::Core::ProxyType<${modulePropertyType}>* var = reinterpret_cast<WPEFramework::Core::ProxyType<${modulePropertyType}>*>(handle);
ASSERT(var->IsValid());
` + '\n'
if ((json.type === 'object') && (accessorPropertyType !== 'char*')) {
if (((json.type === 'object') || (json.type === 'array')) && (accessorPropertyType !== 'char*')) {
result += ` WPEFramework::Core::ProxyType<${subPropertyType}>* element = new WPEFramework::Core::ProxyType<${subPropertyType}>();
*element = WPEFramework::Core::ProxyType<${subPropertyType}>::Create();
*(*element) = (*var)->${subPropertyName};
Expand All @@ -73,7 +73,7 @@ const getPropertyAccessorsImpl = (objName, modulePropertyType, subPropertyType,
ASSERT(var->IsValid());
` + '\n'

if (json.type === 'object' && (accessorPropertyType !== 'char*')) {
if (((json.type === 'object') || (json.type === 'array')) && (accessorPropertyType !== 'char*')) {
result += ` WPEFramework::Core::ProxyType<${subPropertyType}>* object = reinterpret_cast<WPEFramework::Core::ProxyType<${subPropertyType}>*>(value);
(*var)->${subPropertyName} = *(*object);` + '\n'
}
Expand Down
2 changes: 1 addition & 1 deletion languages/c/src/types/JSONHelpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const getJsonDataPrefix = () => 'JsonData_'
const wpeJsonNameSpace = () => 'WPEFramework::Core::JSON'

const getJsonDataStructName = (modName, name, prefix = '') => {
let result =((prefix.length > 0) && (!name.startsWith(prefix))) ? `${capitalize(modName)}::${getJsonDataPrefix()}${capitalize(prefix)}_${capitalize(name)}` : `${capitalize(modName)}::${getJsonDataPrefix()}${capitalize(name)}`
let result =((prefix.length > 0) && (prefix !== name)) ? `${capitalize(modName)}::${getJsonDataPrefix()}${capitalize(prefix)}${capitalize(name)}` : `${capitalize(modName)}::${getJsonDataPrefix()}${capitalize(name)}`

return ((result.includes(wpeJsonNameSpace()) === true) ? result : `${getSdkNameSpace()}::${result}`)
}
Expand Down
36 changes: 2 additions & 34 deletions languages/c/src/types/NativeHelpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,37 +50,6 @@ const SdkTypesPrefix = 'Firebolt'

const Indent = ' '

const getArrayElementSchema = (json, module, schemas = {}, name) => {
let result = ''
if (json.type === 'array' && json.items) {
if (Array.isArray(json.items)) {
result = json.items[0]
}
else {
// grab the type for the non-array schema
result = json.items
}
if (result['$ref']) {
result = getPath(result['$ref'], module, schemas)
}
}
else if (json.type == 'object') {
if (json.properties) {
Object.entries(json.properties).every(([pname, prop]) => {
if (prop.type === 'array') {
result = getArrayElementSchema(prop, module, schemas)
if (name === capitalize(pname)) {
return false
}
}
return true
})
}
}

return result
}

const getNativeType = (json, fireboltString = false) => {
let type
let jsonType = json.const ? typeof json.const : json.type
Expand Down Expand Up @@ -150,10 +119,10 @@ const getTypeName = (moduleName, varName, prefix = '', upperCase = false, capita
let mName = upperCase ? moduleName.toUpperCase() : capitalize(moduleName)
let vName = upperCase ? varName.toUpperCase() : capitalCase ? capitalize(varName) : varName
if (prefix.length > 0) {
prefix = (!varName.startsWith(prefix)) ? (upperCase ? prefix.toUpperCase() : capitalize(prefix)) : ''
prefix = (prefix !== varName) ? (upperCase ? prefix.toUpperCase() : capitalize(prefix)) : ''
}
prefix = (prefix.length > 0) ?(upperCase ? prefix.toUpperCase() : capitalize(prefix)) : prefix
let name = (prefix.length > 0) ? `${mName}_${prefix}_${vName}` : `${mName}_${vName}`
let name = (prefix.length > 0) ? `${mName}_${prefix}${vName}` : `${mName}_${vName}`
return name
}

Expand Down Expand Up @@ -252,6 +221,5 @@ export {
getPropertyAccessors,
isOptional,
generateEnum,
getArrayElementSchema,
getFireboltStringType
}
1 change: 0 additions & 1 deletion languages/c/templates/codeblocks/export.c
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
export { default as ${info.title} } from './${info.title}/index.mjs'
25 changes: 16 additions & 9 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const getComponentExternalSchema = (json) => {
let title = ''
if (ref.includes('x-schemas')) {
if (ref.split('/')[2] !== json.info.title) {
title = ref.split('/')[2]
title = ref.split('/')[2].toLowerCase()
}
}
title && !refSchemas.includes(title) ? refSchemas.push(title) : null
Expand Down Expand Up @@ -384,7 +384,7 @@ const promoteAndNameSubSchemas = (obj) => {
while (more) {
more = false
Object.entries(obj.components.schemas).forEach(([key, schema]) => {
if (schema.type === "object" && schema.properties) {
if ((schema.type === "object" || schema.type === "array") && schema.properties) {
Object.entries(schema.properties).forEach(([name, propSchema]) => {
if (isSubSchema(propSchema)) {
more = true
Expand Down Expand Up @@ -584,10 +584,11 @@ function insertTableofContents(content) {
}

const convertEnumTemplate = (schema, templateName, templates) => {
let enm = getEnum(schema)
const template = getTemplate(templateName, templates).split('\n')
for (var i = 0; i < template.length; i++) {
if (template[i].indexOf('${key}') >= 0) {
template[i] = schema.enum.map(value => {
template[i] = enm.enum.map(value => {
const safeName = value.split(':').pop().replace(/[\.\-]/g, '_').replace(/\+/g, '_plus').replace(/([a-z])([A-Z0-9])/g, '$1_$2').toUpperCase()
return template[i].replace(/\$\{key\}/g, safeName)
.replace(/\$\{value\}/g, value)
Expand Down Expand Up @@ -693,7 +694,12 @@ function sortSchemasByReference(schemas = []) {
return schemas
}

const isEnum = x => x.type && x.type === 'string' && Array.isArray(x.enum) && x.title
const getEnum = x => x.type && x.type === 'array' ? x.items : x

const isEnum = x => {
let enm = getEnum(x)
return enm.type && enm.type === 'string' && Array.isArray(enm.enum) && x.title
}

function generateSchemas(json, templates, options) {
let results = []
Expand Down Expand Up @@ -1089,9 +1095,10 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {})
const serializedParams = types.getSchemaInstantiation(methodObj, json, methodObj.name, { instantiationType: 'params' })
const resultInst = types.getSchemaInstantiation(result.schema, json, result.name, { instantiationType: 'result' } )
const serializedEventParams = event ? indent(types.getSchemaInstantiation(event, json, event.name, { instantiationType: 'params' }), ' ') : ''
const callbackSerializedParams = event ? types.getSchemaInstantiation(event, json, event.name, { instantiationType: 'callback.params' }) : ''
const callbackResultInst = event ? types.getSchemaInstantiation(event, json, event.name, { instantiationType: 'callback.result' }) : ''
const callbackResponseInst = event ? types.getSchemaInstantiation(event, json, event.name, { instantiationType: 'callback.response' }) : ''
const callbackSerializedParams = event ? types.getSchemaInstantiation(event, json, event.name, { instantiationType: 'callback.params', prefix: method.alternative }) : ''
const callbackResultInst = event ? types.getSchemaInstantiation(event, json, event.name, { instantiationType: 'callback.result', prefix: method.alternative }) : ''
const callbackResponseInst = event ? types.getSchemaInstantiation(event, json, event.name, { instantiationType: 'callback.response', prefix: method.alternative }) : ''
const callbackResultJsonType = event && result.schema ? types.getJsonType(result.schema, json, { name: result.name, prefix: method.alternative }) : ''
const resultType = result.schema ? types.getSchemaType(result.schema, json, { name: result.name }) : ''
const resultJsonType = result.schema ? types.getJsonType(result.schema, json, { name: result.name }) : ''

Expand Down Expand Up @@ -1166,8 +1173,8 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {})
.replace(/\$\{method\.result\.link\}/g, getLinkForSchema(result.schema, json, { name: result.name })) //, baseUrl: options.baseUrl
.replace(/\$\{method\.result\.type\}/g, types.getSchemaType(result.schema, json, { name: result.name, title: true, asPath: false, destination: state.destination, resultSchema: true })) //, baseUrl: options.baseUrl
.replace(/\$\{method\.result\.json\}/, types.getJsonType(result.schema, json, { name: result.name, destination: state.destination, section: state.section, code: false, link: false, title: true, asPath: false, expandEnums: false }))
.replace(/\$\{event\.result\.type\}/, isEventMethod(methodObj) ? types.getSchemaType(result.schema, json, { name: result.name, destination: state.destination, event: true, description: methodObj.result.summary, asPath: false }) : '')
.replace(/\$\{event\.result\.json\.type\}/g, resultJsonType)
.replace(/\$\{event\.result\.type\}/, isEventMethod(methodObj) ? types.getSchemaType(result.schema, json, { name: result.name, prefix: method.alternative, destination: state.destination, event: true, description: methodObj.result.summary, asPath: false }) : '')
.replace(/\$\{event\.result\.json\.type\}/g, callbackResultJsonType)
.replace(/\$\{event\.pulls\.param\.name\}/g, pullsEventParamName)
.replace(/\$\{method\.result\}/g, generateResult(result.schema, json, templates, { name: result.name }))
.replace(/\$\{method\.result\.json\.type\}/g, resultJsonType)
Expand Down

0 comments on commit b7e8758

Please sign in to comment.