Skip to content

Commit

Permalink
Types and Accessors: fixes to generate proper format and anyOf suppor…
Browse files Browse the repository at this point in the history
…t added
  • Loading branch information
HaseenaSainul committed May 25, 2023
1 parent 13b1e6c commit da0284b
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 131 deletions.
281 changes: 213 additions & 68 deletions languages/c/Types.mjs

Large diffs are not rendered by default.

99 changes: 47 additions & 52 deletions languages/c/src/types/ImplHelpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -99,77 +99,72 @@ const getPropertyAccessorsImpl = (objName, modulePropertyType, subPropertyType,

return result
}
const getArrayAccessorsImpl = (objName, modulePropertyType, objHandleType, subPropertyType, subPropertyName, accessorPropertyType, json = {}) => {

const getArrayAccessorsImpl = (objName, propertyName, propertyType, json = {}, options = {readonly:false, optional:false}) => {
let result = `
uint32_t ${objName}_${propertyName}Array_Size(${objName}::${propertyName}ArrayHandle handle) {
let propertyName
if (subPropertyName) {
propertyName = '(*var)->' + `${subPropertyName}`
objName = objName + '_' + subPropertyName
}
else {
propertyName = '(*(*var))'
}

let result = `uint32_t ${objName}Array_Size(${objHandleType} handle) {
ASSERT(handle != NULL);
WPEFramework::Core::ProxyType<${objName}::${propertyName}>* var = static_cast<WPEFramework::Core::ProxyType<${objName}::${propertyName}>*>(handle);
WPEFramework::Core::ProxyType<${modulePropertyType}>* var = static_cast<WPEFramework::Core::ProxyType<${modulePropertyType}>*>(handle);
ASSERT(var->IsValid());
return ((*var)->Length());
return (${propertyName}.Length());
}` + '\n'

if (json.type === 'object') {
result += `${objName}_${propertyType}Handle ${objName}_${propertyName}Array_Get(${objName}_${propertyName}ArrayHandle handle, uint32_t index) {
ASSERT(handle != NULL);
WPEFramework::Core::ProxyType<WPEFramework::Core::JSON::ArrayType<${objName}::${propertyName}>>* var = static_cast<WPEFramework::Core::ProxyType<WPEFramework::Core::JSON::ArrayType<${objName}::${propertyName}>>*>(handle);
ASSERT(var->IsValid());
WPEFramework::Core::ProxyType<${objName}::${propertyType}>* object = new WPEFramework::Core::ProxyType<${objName}::${propertyType}>();
*object = WPEFramework::Core::ProxyType<${objName}::${propertyName}>::Create();
*(*object) = (*var)->Get(index);
return (static_cast<${objName}_${propertyType}Handle>(object));` + '\n'
} else if (json.enum) {
result += `${objName}_${propertyType} ${objName}_${propertyName}Array_Get(${objName}_${propertyName}ArrayHandle handle, uint32_t index) {
ASSERT(handle != NULL);
WPEFramework::Core::ProxyType<WPEFramework::Core::JSON::ArrayType<${objName}::${propertyName}>>* var = static_cast<WPEFramework::Core::ProxyType<WPEFramework::Core::JSON::ArrayType<${objName}::${propertyName}>>*>(handle);
ASSERT(var->IsValid());
return (static_cast<${propertyType}>((*var)->Get(index)));` + '\n'
} else {
result += `${propertyType} ${objName}_${propertyName}Array_Get(${objName}_${propertyName}ArrayHandle handle, uint32_t index) {
result += `${accessorPropertyType} ${objName}Array_Get(${objHandleType} handle, uint32_t index)
{
ASSERT(handle != NULL);
WPEFramework::Core::ProxyType<WPEFramework::Core::JSON::ArrayType<${objName}::${propertyName}>>* var = static_cast<WPEFramework::Core::ProxyType<WPEFramework::Core::JSON::ArrayType<${objName}::${propertyName}>>*>(handle);
WPEFramework::Core::ProxyType<${modulePropertyType}>* var = static_cast<WPEFramework::Core::ProxyType<${modulePropertyType}>*>(handle);
ASSERT(var->IsValid());` + '\n'

if (json.type === 'string') {
result += `return (static_cast<${propertyType}>((*var)->Get(index).Value().c_str()));` + '\n'
} else {
result += `return (static_cast<${propertyType}>((*var)->Get(index)));` + '\n'
}
if ((json.type === 'object') || (json.type === 'array')) {
result += `WPEFramework::Core::ProxyType<${subPropertyType}>* object = new WPEFramework::Core::ProxyType<${subPropertyType}>();
*object = WPEFramework::Core::ProxyType<${subPropertyType}>::Create();
*(*object) = ${propertyName}.Get(index);
return (static_cast<${accessorPropertyType}>(object));` + '\n'
}
else {
if ((typeof json.const === 'string') || (json.type === 'string' && !json.enum)) {
result += ` return (const_cast<${accessorPropertyType}>(${propertyName}.Get(index).Value().c_str()));` + '\n'
}
else {
result += ` return (static_cast<${accessorPropertyType}>(${propertyName}.Get(index)));` + '\n'
}
}
result += `}` + '\n'

if (json.type === 'object') {
result += `void ${objName}_${propertyName}Array_Add(${objName}_${propertyName}ArrayHandle handle, ${objName}_${propertyType}Handle value) {
ASSERT(handle != NULL);
WPEFramework::Core::ProxyType<WPEFramework::Core::JSON::ArrayType<${objName}::${propertyName}>>* var = static_cast<WPEFramework::Core::ProxyType<WPEFramework::Core::JSON::ArrayType<${objName}::${propertyName}>>*>(handle);
ASSERT(var->IsValid());
WPEFramework::Core::ProxyType<${objName}::${propertyType}>* object = static_cast<WPEFramework::Core::ProxyType<${objName}::${propertyType}>*>(value);
(*var)->Add(*(*object));` + '\n'
} else {
result += `void ${objName}_${propertyName}Array_Add(${objName}_${propertyName}ArrayHandle handle, ${propertyType} value) {
let type = (accessorPropertyType === getFireboltStringType()) ? 'char*' : accessorPropertyType
result += `void ${objName}Array_Add(${objHandleType} handle, ${type} value)
{
ASSERT(handle != NULL);
WPEFramework::Core::ProxyType<WPEFramework::Core::JSON::ArrayType<${objName}::${propertyName}>>* var = static_cast<WPEFramework::Core::ProxyType<WPEFramework::Core::JSON::ArrayType<${objName}::${propertyName}>>*>(handle);
WPEFramework::Core::ProxyType<${modulePropertyType}>* var = static_cast<WPEFramework::Core::ProxyType<${modulePropertyType}>*>(handle);
ASSERT(var->IsValid());` + '\n'
if (json.type === 'string') {
result += `WPEFramework::Core::JSON::String element(value);` + '\n'
} else if (json.type === 'number') {
result += `WPEFramework::Core::JSON::Number element(value);` + '\n'
} else if (json.enum) {
result += `WPEFramework::Core::JSON::EnumType<${propertyType}> element(value);` + '\n'
}
result += `(*var)->Add(element);` + '\n'

if ((json.type === 'object') || (json.type === 'array')) {
result += ` ${subPropertyType}& element = *(*(static_cast<WPEFramework::Core::ProxyType<${subPropertyType}>*>(value)));` + '\n'
}
result += `}` + '\n'
else {
result += ` ${subPropertyType} element(value);` + '\n'
}
result += `
${propertyName}.Add(element);
}` + '\n'

result += `void ${objName}_${propertyName}Array_Clear(${objName}_${propertyName}ArrayHandle handle) {
result += `void ${objName}Array_Clear(${objHandleType} handle)
{
ASSERT(handle != NULL);
WPEFramework::Core::ProxyType<WPEFramework::Core::JSON::ArrayType<${objName}::${propertyName}>>* var = static_cast<WPEFramework::Core::ProxyType<WPEFramework::Core::JSON::ArrayType<${objName}::${propertyName}>>*>(handle);
WPEFramework::Core::ProxyType<${modulePropertyType}>* var = static_cast<WPEFramework::Core::ProxyType<${modulePropertyType}>*>(handle);
ASSERT(var->IsValid());
(*var)->Clear();
${propertyName}.Clear();
}` + '\n'

return result
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, prefixName = '') => {
let result =((prefixName.length > 0) && (prefixName != name)) ? `${capitalize(modName)}::${getJsonDataPrefix()}${capitalize(prefixName)}${capitalize(name)}` : `${capitalize(modName)}::${getJsonDataPrefix()}${capitalize(name)}`
let result =((prefixName.length > 0) && (capitalize(prefixName) != capitalize(name))) ? `${capitalize(modName)}::${getJsonDataPrefix()}${capitalize(prefixName)}${capitalize(name)}` : `${capitalize(modName)}::${getJsonDataPrefix()}${capitalize(name)}`

return ((result.includes(wpeJsonNameSpace()) === true) ? result : `${getSdkNameSpace()}::${result}`)
}
Expand Down
46 changes: 39 additions & 7 deletions languages/c/src/types/NativeHelpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,39 @@ 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 => {
let type
let type = ''
let jsonType = json.const ? typeof json.const : json.type
if (jsonType === 'string') {
type = 'char*'
Expand Down Expand Up @@ -153,12 +184,12 @@ const getTypeName = (moduleName, varName, prefixName = '', upperCase = false, ca
return name
}

const getArrayAccessors = (arrayName, valueType) => {
const getArrayAccessors = (arrayName, propertyType, valueType) => {

let res = `uint32_t ${arrayName}_Size(${arrayName}Handle handle);` + '\n'
res += `${valueType} ${arrayName}_Get(${arrayName}Handle handle, uint32_t index);` + '\n'
res += `void ${arrayName}_Add(${arrayName}Handle handle, ${valueType} value);` + '\n'
res += `void ${arrayName}_Clear(${arrayName}Handle handle);` + '\n'
let res = `uint32_t ${arrayName}Array_Size(${propertyType}Handle handle);` + '\n'
res += `${valueType} ${arrayName}Array_Get(${propertyType}Handle handle, uint32_t index);` + '\n'
res += `void ${arrayName}Array_Add(${propertyType}Handle handle, ${valueType} value);` + '\n'
res += `void ${arrayName}Array_Clear(${propertyType}Handle handle);` + '\n'

return res
}
Expand Down Expand Up @@ -233,5 +264,6 @@ export {
getObjectHandleManagement,
getPropertyAccessors,
isOptional,
generateEnum
generateEnum,
getArrayElementSchema
}
1 change: 0 additions & 1 deletion languages/c/templates/modules/src/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
namespace FireboltSDK {
namespace ${info.title} {
// Types

/* ${TYPES} */
}
}
Expand Down
1 change: 0 additions & 1 deletion languages/c/templates/sections/accessors.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
// Accessors

${schema.list}
2 changes: 1 addition & 1 deletion src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ function getRelatedSchemaLinks(schema = {}, json = {}, templates = {}, options =
.map(path => path.substring(2).split('/'))
.map(path => getPathOr(null, path, json))
.filter(schema => schema.title)
.map(schema => '[' + types.getSchemaType(schema, json, { destination: state.destination, section: state.section }) + '](' + getLinkForSchema(schema, json, { name: schema.title }) + ')') // need full module here, not just the schema
.map(schema => '[' + types.getSchemaType(schema, json, { name: schema.title, destination: state.destination, section: state.section }) + '](' + getLinkForSchema(schema, json, { name: schema.title }) + ')') // need full module here, not just the schema
.filter(link => link)
.join('\n')

Expand Down

0 comments on commit da0284b

Please sign in to comment.