Skip to content

Commit

Permalink
Types and Accessor related fixes:
Browse files Browse the repository at this point in the history
1. Accessor creation
2. JsonContainer creation
3. Types order placed based on depenedencies
4. filtered redundant new lines
5. UnamedSchema issue fixes
  • Loading branch information
HaseenaSainul committed May 22, 2023
1 parent 8687ed8 commit 44fbbfd
Show file tree
Hide file tree
Showing 11 changed files with 757 additions and 579 deletions.
790 changes: 472 additions & 318 deletions languages/c/Types.mjs

Large diffs are not rendered by default.

295 changes: 151 additions & 144 deletions languages/c/src/types/ImplHelpers.mjs

Large diffs are not rendered by default.

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

/*
Expand Down Expand Up @@ -57,5 +80,6 @@ function getJsonEnumConversion(schema, { name }) {

export {
getJsonContainerDefinition,
getJsonEnumConversion
getJsonEnumConversion,
getJsonDataStructName
}
103 changes: 48 additions & 55 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 @@ -84,34 +85,21 @@ 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'
}
}
else if (json.type === 'string') {
type = 'char*'
}
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'
}
}
else if (json.type === 'boolean') {
type = 'bool'
}
return type
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,38 +114,43 @@ 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, prefixName = '', upperCase = false, capitalCase = true) => {

return `${mName}_${vName}`
let mName = upperCase ? moduleName.toUpperCase() : capitalize(moduleName)
let vName = upperCase ? varName.toUpperCase() : capitalCase ? capitalize(varName) : varName
if (prefixName.length > 0) {
prefixName = (prefixName !== varName) ? (upperCase ? prefixName.toUpperCase() : capitalize(prefixName)) : ''
}
prefixName = (prefixName.length > 0) ?(upperCase ? prefixName.toUpperCase() : capitalize(prefixName)) : prefixName
let name = (prefixName.length > 0) ? `${mName}_${prefixName}_${vName}` : `${mName}_${vName}`
return name
}

const getArrayAccessors = (arrayName, valueType) => {
Expand Down Expand Up @@ -201,25 +194,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 @@ -241,4 +234,4 @@ const getIncludeDefinitions = (json = {}, jsonData = false) => {
getPropertyAccessors,
isOptional,
generateEnum
}
}
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
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} */
}
}
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}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 44fbbfd

Please sign in to comment.