Skip to content

Commit

Permalink
Types and Accessor related fixes (#94)
Browse files Browse the repository at this point in the history
Types and Accessor related fixes:
1. Accessor creation
2. JsonContainer creation
3. Types order placed based on depenedencies
4. filtered redundant new lines
5. UnamedSchema issue fixes
6. Types and Accessors:  generation added for objects inside methods result/params
7. AnyOf support added
  • Loading branch information
HaseenaSainul committed Jun 6, 2023
1 parent 717b777 commit bd7db6c
Show file tree
Hide file tree
Showing 15 changed files with 993 additions and 637 deletions.
915 changes: 605 additions & 310 deletions languages/c/Types.mjs

Large diffs are not rendered by default.

392 changes: 197 additions & 195 deletions languages/c/src/types/ImplHelpers.mjs

Large diffs are not rendered by default.

67 changes: 45 additions & 22 deletions languages/c/src/types/JSONHelpers.mjs
Original file line number Diff line number Diff line change
@@ -1,31 +1,54 @@
const capitalize = str => str[0].toUpperCase() + str.substr(1)
const getSdkNameSpace = () => 'FireboltSDK'
const getJsonDataPrefix = () => 'JsonData_'
const wpeJsonNameSpace = () => 'WPEFramework::Core::JSON'

function getJsonContainerDefinition (name, props) {
name = capitalize(name)
let c = ` class ${name}: public Core::JSON::Container {
public:
${name}(const ${name}&) = delete;
${name}& operator=(const ${name}&) = delete;
~${name}() override = default;
public:
${name}()
: Core::JSON::Container()
{`
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)}`

return ((result.includes(wpeJsonNameSpace()) === true) ? result : `${getSdkNameSpace()}::${result}`)
}

function getJsonContainerDefinition (schema, name, props) {
let c = schema.description ? (' /*\n * ${info.title} - ' + `${schema.description}\n */\n`) : ''
name = getJsonDataPrefix() + capitalize(name)
c += ` class ${name}: public WPEFramework::Core::JSON::Container {
public:
~${name}() override = default;
props.forEach(prop => {
c += `\n Add(_T("${prop.name}"), &${capitalize(prop.name)});`
})
public:
${name}()
: WPEFramework::Core::JSON::Container()
{`

c += `\n }\n\n public:`
props.forEach(prop => {
c += `\n Add(_T("${prop.name}"), &${capitalize(prop.name)});`
})
c += `\n }\n`
c += `\n ${name}(const ${name}& copy)
{`
props.forEach(prop => {
c += `\n Add(_T("${prop.name}"), &${capitalize(prop.name)});`
c += `\n ${capitalize(prop.name)} = copy.${capitalize(prop.name)};`
})
c += `
}\n
${name}& operator=(const ${name}& rhs)
{`
props.forEach(prop => {
c += `\n ${capitalize(prop.name)} = rhs.${capitalize(prop.name)};`
})
c += `\n return (*this);
}\n
public:`

props.forEach(prop => {
c += `\n ${prop.type} ${capitalize(prop.name)};`
})
props.forEach(prop => {
c += `\n ${prop.type} ${capitalize(prop.name)};`
})

c += '\n };'
return c
}
c += '\n };'
return c
}

export {
getJsonContainerDefinition
Expand Down
141 changes: 83 additions & 58 deletions languages/c/src/types/NativeHelpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const { isObject, isArray, propEq, pathSatisfies, hasProp, propSatisfies } = pre

const getModuleName = json => getPathOr(null, ['info', 'title'], json) || json.title || 'missing'

const getFireboltStringType = () => 'FireboltTypes_StringHandle'
const getHeaderText = () => {

return `/*
Expand Down Expand Up @@ -83,35 +84,53 @@ const SdkTypesPrefix = 'Firebolt'

const Indent = ' '

const getNativeType = json => {
let type

if (json.const) {
if (typeof json.const === 'string') {
type = 'char*'
}
else if (typeof json.const === 'number') {
type = 'uint32_t'
if (json.const < 0)
type = 'int32_t'
} else if (typeof json.const === 'boolean'){
type = 'bool'
}
const getArrayElementSchema = (json, module, schemas = {}, name) => {
let result = ''
if (json.type === 'array' && json.items) {
if (Array.isArray(json.items)) {
result = json.items[0]
}
else if (json.type === 'string') {
type = 'char*'
else {
// grab the type for the non-array schema
result = json.items
}
else if (json.type === 'number' || json.type === 'integer') { //Lets keep it simple for now
type = 'uint32_t'
if ((json.minimum && json.minimum < 0)
|| (json.exclusiveMinimum && json.exclusiveMinimum < 0)) {
type = 'int32_t'
}
if (result['$ref']) {
result = getPath(result['$ref'], module, schemas)
}
else if (json.type === 'boolean') {
type = 'bool'
}
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 type
}

return result
}

const getNativeType = json => {
let type = ''
let jsonType = json.const ? typeof json.const : json.type
if (jsonType === 'string') {
type = 'char*'
}
else if (jsonType === 'number') {
type = 'float'
}
else if (jsonType === 'integer') {
type = 'int32_t'
}
else if (jsonType === 'boolean') {
type = 'bool'
}
return type
}

const getObjectHandleManagement = varName => {
Expand All @@ -126,46 +145,51 @@ bool ${varName}Handle_IsValid(${varName}Handle handle);
}

const getPropertyAccessors = (objName, propertyName, propertyType, options = {level:0, readonly:false, optional:false}) => {

let result = `${Indent.repeat(options.level)}${propertyType} ${objName}_Get_${propertyName}(${objName}Handle handle);` + '\n'

if (!options.readonly) {
result += `${Indent.repeat(options.level)}void ${objName}_Set_${propertyName}(${objName}Handle handle, ${propertyType} ${propertyName.toLowerCase()});` + '\n'
let type = (propertyType === getFireboltStringType()) ? 'char*' : propertyType
result += `${Indent.repeat(options.level)}void ${objName}_Set_${propertyName}(${objName}Handle handle, ${type} ${propertyName.toLowerCase()});` + '\n'
}

if (options.optional === true) {
result += `${Indent.repeat(options.level)}bool ${objName}_has_${propertyName}(${objName}Handle handle);` + '\n'
result += `${Indent.repeat(options.level)}void ${objName}_clear_${propertyName}(${objName}Handle handle);` + '\n'
result += `${Indent.repeat(options.level)}bool ${objName}_Has_${propertyName}(${objName}Handle handle);` + '\n'
result += `${Indent.repeat(options.level)}void ${objName}_Clear_${propertyName}(${objName}Handle handle);` + '\n'
}

return result
}

const getMapAccessors = (typeName, nativeType, level=0) => {
const getMapAccessors = (typeName, accessorPropertyType, level = 0) => {

let res

res = `${Indent.repeat(level)}uint32_t ${typeName}_KeysCount(${typeName}Handle handle);` + '\n'
res += `${Indent.repeat(level)}void ${typeName}_AddKey(${typeName}Handle handle, char* key, ${nativeType} value);` + '\n'
res += `${Indent.repeat(level)}void ${typeName}_AddKey(${typeName}Handle handle, char* key, ${accessorPropertyType} value);` + '\n'
res += `${Indent.repeat(level)}void ${typeName}_RemoveKey(${typeName}Handle handle, char* key);` + '\n'
res += `${Indent.repeat(level)}${nativeType} ${typeName}_FindKey(${typeName}Handle handle, char* key);` + '\n'
res += `${Indent.repeat(level)}${accessorPropertyType} ${typeName}_FindKey(${typeName}Handle handle, char* key);` + '\n'

return res
}

const getTypeName = (moduleName, varName, upperCase = false) => {
let mName = upperCase ? moduleName.toUpperCase() : capitalize(moduleName)
let vName = upperCase ? varName.toUpperCase() : capitalize(varName)
const getTypeName = (moduleName, varName, prefix = '', upperCase = false, capitalCase = true) => {

return `${mName}_${vName}`
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.length > 0) ?(upperCase ? prefix.toUpperCase() : capitalize(prefix)) : prefix
let name = (prefix.length > 0) ? `${mName}_${prefix}_${vName}` : `${mName}_${vName}`
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 @@ -201,25 +225,25 @@ const getIncludeDefinitions = (json = {}, jsonData = false) => {
.concat([`#include "Firebolt/Types.h"`])
}

function getPropertyGetterSignature(method, module, paramType) {
let m = `${capitalize(getModuleName(module))}_Get${capitalize(method.name)}`
return `${description(method.name, method.summary)}\nuint32 ${m}( ${paramType === 'char*' ? 'FireboltTypes_StringHandle' : paramType}* ${method.result.name || method.name} )`
}
function getPropertyGetterSignature(method, module, paramType) {
let m = `${capitalize(getModuleName(module))}_Get${capitalize(method.name)}`
return `${description(method.name, method.summary)}\nuint32 ${m}( ${paramType === 'char*' ? 'FireboltTypes_StringHandle' : paramType}* ${method.result.name || method.name} )`
}

function getPropertySetterSignature(method, module, paramType) {
let m = `${capitalize(getModuleName(module))}_Set${capitalize(method.name)}`
return `${description(method.name, method.summary)}\nuint32 ${m}( ${paramType} ${method.result.name || method.name} )`
}
function getPropertySetterSignature(method, module, paramType) {
let m = `${capitalize(getModuleName(module))}_Set${capitalize(method.name)}`
return `${description(method.name, method.summary)}\nuint32 ${m}( ${paramType} ${method.result.name || method.name} )`
}

function getPropertyEventCallbackSignature(method, module, paramType) {
return `typedef void (*On${capitalize(method.name)}Changed)(${paramType === 'char*' ? 'FireboltTypes_StringHandle' : paramType})`
}
function getPropertyEventCallbackSignature(method, module, paramType) {
return `typedef void (*On${capitalize(method.name)}Changed)(${paramType === 'char*' ? 'FireboltTypes_StringHandle' : paramType})`
}

function getPropertyEventSignature(method, module) {
return `${description(method.name, 'Listen to updates')}\n` + `uint32_t ${capitalize(getModuleName(module))}_Listen${capitalize(method.name)}Update(On${capitalize(method.name)}Changed notification, uint16_t* listenerId)`
}
function getPropertyEventSignature(method, module) {
return `${description(method.name, 'Listen to updates')}\n` + `uint32_t ${capitalize(getModuleName(module))}_Listen${capitalize(method.name)}Update(On${capitalize(method.name)}Changed notification, uint16_t* listenerId)`
}

export {
export {
getHeaderText,
getIncludeGuardOpen,
getStyleGuardOpen,
Expand All @@ -240,5 +264,6 @@ const getIncludeDefinitions = (json = {}, jsonData = false) => {
getObjectHandleManagement,
getPropertyAccessors,
isOptional,
generateEnum
}
generateEnum,
getArrayElementSchema
}
16 changes: 8 additions & 8 deletions languages/c/templates/methods/property.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* ${method.name} - ${method.description} */
uint32_t ${info.title}_Get${method.Name}(${method.params}${if.params}, ${end.if.params}${method.result.type}* ${method.result.name}) {
const string method = _T("${info.title}.${method.name}");
FireboltSDK::${info.title}::${method.result.type} jsonResult;
const string method = _T("${info.title}.${method.name}");
FireboltSDK::${info.title}::${method.result.type} jsonResult;

uint32_t status = FireboltSDK::Properties::Get(method, jsonResult);
if (status == FireboltSDKErrorNone) {
WPEFramework::Core::ProxyType<FireboltSDK::${info.title}::${method.result.type}>* resultPtr = new WPEFramework::Core::ProxyType<FireboltSDK::${info.title}::${method.result.type}>();
*${method.result.name} = static_cast<${info.title}_${method.result.type}Handle>(resultPtr);
}
return status;
uint32_t status = FireboltSDK::Properties::Get(method, jsonResult);
if (status == FireboltSDKErrorNone) {
WPEFramework::Core::ProxyType<FireboltSDK::${info.title}::${method.result.type}>* resultPtr = new WPEFramework::Core::ProxyType<FireboltSDK::${info.title}::${method.result.type}>();
*${method.result.name} = static_cast<${info.title}_${method.result.type}Handle>(resultPtr);
}
return status;
}
4 changes: 0 additions & 4 deletions languages/c/templates/modules/include/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ extern "C" {

/* ${ENUMS} */

// Types

/* ${TYPES} */

/* ${ACCESSORS} */

/* ${DECLARATIONS} */
Expand Down
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
3 changes: 0 additions & 3 deletions languages/c/templates/schemas/default.c
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
/*
* ${schema.description}
*/
${schema.shape}
2 changes: 1 addition & 1 deletion languages/c/templates/schemas/src/JsonData_Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ namespace FireboltSDK {
namespace ${info.title} {
// Types

/* ${SCHEMAS}*/
/* ${SCHEMAS} */
}
}
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}
3 changes: 2 additions & 1 deletion languages/c/templates/sections/methods.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

// Methods
// Methods

${method.list}
1 change: 1 addition & 0 deletions languages/c/templates/sections/methods_accessors.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${schema.list}
1 change: 1 addition & 0 deletions languages/c/templates/sections/methods_types.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${schema.list}
Loading

0 comments on commit bd7db6c

Please sign in to comment.