From 65090660165ab0d57f180177c56299f54481ecc1 Mon Sep 17 00:00:00 2001 From: HaseenaSainul <41037131+HaseenaSainul@users.noreply.github.com> Date: Tue, 11 Jul 2023 21:58:32 +0530 Subject: [PATCH] Default template implementation added (#103) Default template implementation added --- languages/c/Types.mjs | 62 ++++++++++++-------- languages/c/src/types/ImplHelpers.mjs | 26 ++++---- languages/c/src/types/NativeHelpers.mjs | 4 +- languages/c/templates/codeblocks/setter.c | 6 +- languages/c/templates/declarations/default.c | 3 + languages/c/templates/methods/default.c | 33 +++++------ languages/c/templates/methods/property.c | 2 +- src/macrofier/engine.mjs | 6 +- 8 files changed, 83 insertions(+), 59 deletions(-) diff --git a/languages/c/Types.mjs b/languages/c/Types.mjs index edf817c9..2a8f5aa0 100644 --- a/languages/c/Types.mjs +++ b/languages/c/Types.mjs @@ -170,6 +170,9 @@ const hasTag = (method, tag) => { return method.tags && method.tags.filter(t => t.name === tag).length > 0 } +const IsResultConstNullSuccess = (schema, name) => (name === 'success' && !schema.const && !schema.type) +const IsResultBooleanSuccess = (schema, name) => (name === 'success' && schema.type === 'boolean') + function getParamList(schema, module) { let paramList = [] if (schema.params.length > 0) { @@ -229,13 +232,13 @@ function getSchemaType(schema, module, { name, prefix = '', destination, resultS return info.type } -function getSchemaTypeInfo(module = {}, json = {}, name = '', schemas = {}, prefix = '', options = {level: 0, descriptions: true, title: false, resultSchema: false, event: false}) { +function getSchemaTypeInfo(module = {}, json = {}, name = '', schemas = {}, prefix = '', options = { level: 0, descriptions: true, title: false, resultSchema: false, event: false}) { if (json.schema) { json = json.schema } - let stringAsHandle = options.resultSchema || options.event + let fireboltString = options.resultSchema || options.event let structure = {} structure["type"] = '' @@ -263,7 +266,7 @@ function getSchemaTypeInfo(module = {}, json = {}, name = '', schemas = {}, pref } } else if (json.const) { - structure.type = getNativeType(json, stringAsHandle) + structure.type = getNativeType(json, fireboltString) structure.json = json return structure } @@ -320,7 +323,7 @@ function getSchemaTypeInfo(module = {}, json = {}, name = '', schemas = {}, pref return getSchemaTypeInfo(module, union, '', schemas, '', options) } else if (json.oneOf) { - structure.type = 'char*' + structure.type = fireboltString ? getFireboltStringType() : 'char*' structure.json.type = 'string' return structure } @@ -337,7 +340,7 @@ function getSchemaTypeInfo(module = {}, json = {}, name = '', schemas = {}, pref structure.namespace = (json.namespace ? json.namespace : getModuleName(module)) } else { - structure.type = 'char*' + structure.type = fireboltString ? getFireboltStringType() : 'char*' } if (name) { structure.name = capitalize(name) @@ -346,14 +349,14 @@ function getSchemaTypeInfo(module = {}, json = {}, name = '', schemas = {}, pref return structure } else if (json.type) { - structure.type = getNativeType(json, stringAsHandle) - structure.json = json - if (name || json.title) { - structure.name = capitalize(name || json.title) + if (!IsResultBooleanSuccess(json, name) && !IsResultConstNullSuccess(json, name)) { + structure.type = getNativeType(json, fireboltString) + structure.json = json + if (name || json.title) { + structure.name = capitalize(name || json.title) + } + structure.namespace = getModuleName(module) } - structure.namespace = getModuleName(module) - - return structure } return structure } @@ -377,13 +380,12 @@ function getSchemaShapeInfo(json, module, schemas = {}, { name = '', prefix = '' if (json['$ref'][0] === '#') { //Ref points to local schema //Get Path to ref in this module and getSchemaType - const schema = getPath(json['$ref'], module, schemas) - const tname = schema.title || json['$ref'].split('/').pop() + let schema = getPath(json['$ref'], module, schemas) + const tName = schema.title || json['$ref'].split('/').pop() if (json['$ref'].includes('x-schemas')) { schema = (getRefModule(json['$ref'].split('/')[2])) } - - shape = getSchemaShapeInfo(schema, module, schemas, { name, prefix, merged, level, title, summary, descriptions, destination, section, enums }) + shape = getSchemaShapeInfo(schema, module, schemas, { name: tName, prefix, merged, level, title, summary, descriptions, destination, section, enums }) } } //If the schema is a const, @@ -429,7 +431,6 @@ function getSchemaShapeInfo(json, module, schemas = {}, { name = '', prefix = '' let info = getSchemaTypeInfo(module, items, items.name || pname, schemas, prefix, {level : level, descriptions: descriptions, title: true}) if (info.type && info.type.length > 0) { let objName = tName + '_' + capitalize(prop.title || pname) - let moduleName = info.namespace info.json.namespace = info.namespace let moduleProperty = getJsonTypeInfo(module, json, json.title || name, schemas, prefix) let prefixName = ((prefix.length > 0) && items['$ref']) ? '' : prefix @@ -539,10 +540,14 @@ function getSchemaShapeInfo(json, module, schemas = {}, { name = '', prefix = '' if (info.type && info.type.length > 0) { let type = getArrayElementSchema(json, module, schemas, info.name) - let arrayName = capitalize(name) + capitalize(type.type) - let objName = getTypeName(info.namespace, arrayName, prefix) + 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 tName = objName + 'Array' - let moduleName = info.namespace + info.json.namespace = info.namespace let moduleProperty = getJsonTypeInfo(module, json, json.title || name, schemas, prefix) let subModuleProperty = getJsonTypeInfo(module, j, j.title, schemas, prefix) @@ -712,6 +717,9 @@ function getJsonTypeInfo(module = {}, json = {}, name = '', schemas, prefix = '' structure.type = getJsonNativeType(json) return structure } + else { + structure.type = 'JsonObject' + } return structure } @@ -739,9 +747,17 @@ function getSchemaInstantiation(schema, module, name, { instantiationType = '' } return getParameterInstantiation(getParamList(schema, module)) } else if (instantiationType === 'result') { - let resultType = getSchemaType(schema, module, { title: true, name: name, resultSchema: true}) || '' - let resultJsonType = getJsonType(schema, module, {name: name}) || '' - return getResultInstantiation(name, resultType, resultJsonType) + let result = '' + + if (!IsResultConstNullSuccess(schema, name)) { + let resultJsonType = getJsonType(schema, module, {name: name}) || '' + let resultType = '' + if (!IsResultBooleanSuccess(schema, name)) { + resultType = getSchemaType(schema, module, { title: true, name: name, resultSchema: true}) || '' + } + result = getResultInstantiation(name, resultType, resultJsonType) + } + return result } else if (instantiationType === 'callback.params') { let resultJsonType = getJsonType(schema.result.schema, module, { name: schema.result.name }) || '' diff --git a/languages/c/src/types/ImplHelpers.mjs b/languages/c/src/types/ImplHelpers.mjs index 6509375a..3d5403d4 100644 --- a/languages/c/src/types/ImplHelpers.mjs +++ b/languages/c/src/types/ImplHelpers.mjs @@ -455,16 +455,22 @@ function getResultInstantiation (name, nativeType, container, indentLevel = 3) { let impl = '' - if (nativeType === 'char*' || nativeType === 'FireboltTypes_StringHandle') { - impl += `${' '.repeat(indentLevel)}${container}* strResult = new ${container}(jsonResult);` + '\n' - impl += `${' '.repeat(indentLevel)}*${name} = static_cast<${getFireboltStringType()}>(strResult);` - } else if (nativeType.includes('Handle')) { - impl += `${' '.repeat(indentLevel)}WPEFramework::Core::ProxyType<${container}>* resultPtr = new WPEFramework::Core::ProxyType<${container}>();\n` - impl += `${' '.repeat(indentLevel)}*resultPtr = WPEFramework::Core::ProxyType<${container}>::Create();\n` - impl += `${' '.repeat(indentLevel)}*(*resultPtr) = jsonResult;\n` - impl += `${' '.repeat(indentLevel)}*${name} = static_cast<${nativeType}>(resultPtr);` - } else { - impl += `${' '.repeat(indentLevel)}*${name} = jsonResult.Value();` + if (nativeType) { + impl += `${' '.repeat(indentLevel)}if (${name} != nullptr) {` + '\n' + if (nativeType === 'char*' || nativeType === 'FireboltTypes_StringHandle') { + impl += `${' '.repeat(indentLevel + 1)}${container}* strResult = new ${container}(jsonResult);` + '\n' + impl += `${' '.repeat(indentLevel + 1)}*${name} = static_cast<${getFireboltStringType()}>(strResult);` + } else if (nativeType.includes('Handle')) { + impl += `${' '.repeat(indentLevel + 1)}WPEFramework::Core::ProxyType<${container}>* resultPtr = new WPEFramework::Core::ProxyType<${container}>();\n` + impl += `${' '.repeat(indentLevel + 1)}*resultPtr = WPEFramework::Core::ProxyType<${container}>::Create();\n` + impl += `${' '.repeat(indentLevel + 1)}*(*resultPtr) = jsonResult;\n` + impl += `${' '.repeat(indentLevel + 1)}*${name} = static_cast<${nativeType}>(resultPtr);` + } else { + impl += `${' '.repeat(indentLevel + 1)}*${name} = jsonResult.Value();` + } + impl += `${' '.repeat(indentLevel)}}` + '\n' + } else if (name === 'success') { + impl += `${' '.repeat(indentLevel)}status = (jsonResult.Value() == true) ? FireboltSDKErrorNone : FireboltSDKErrorNotSupported;` } return impl diff --git a/languages/c/src/types/NativeHelpers.mjs b/languages/c/src/types/NativeHelpers.mjs index 022437e8..1460e22e 100644 --- a/languages/c/src/types/NativeHelpers.mjs +++ b/languages/c/src/types/NativeHelpers.mjs @@ -115,12 +115,12 @@ const getArrayElementSchema = (json, module, schemas = {}, name) => { return result } -const getNativeType = (json, stringAsHandle = false) => { +const getNativeType = (json, fireboltString = false) => { let type let jsonType = json.const ? typeof json.const : json.type if (jsonType === 'string') { type = 'char*' - if (stringAsHandle) { + if (fireboltString) { type = getFireboltStringType() } } diff --git a/languages/c/templates/codeblocks/setter.c b/languages/c/templates/codeblocks/setter.c index 9f496543..342a98d6 100644 --- a/languages/c/templates/codeblocks/setter.c +++ b/languages/c/templates/codeblocks/setter.c @@ -1,9 +1,7 @@ /* ${method.name} - ${method.description} */ -uint32_t ${info.title}_${method.Name}( ${method.signature.params} ) +uint32_t ${info.Title}_${method.Name}( ${method.signature.params} ) { const string method = _T("${info.title}.${method.name}"); - ${if.params} -${method.params.serialization} - ${end.if.params} +${if.params}${method.params.serialization}${end.if.params} return FireboltSDK::Properties::Set(method, jsonParameters); } diff --git a/languages/c/templates/declarations/default.c b/languages/c/templates/declarations/default.c index e69de29b..ac4e7f78 100644 --- a/languages/c/templates/declarations/default.c +++ b/languages/c/templates/declarations/default.c @@ -0,0 +1,3 @@ +/* ${method.name} - ${method.description} +${method.params.annotations}${if.deprecated} * @deprecated ${method.deprecation}${end.if.deprecated} */ +uint32_t ${info.Title}_${method.Name}( ${method.signature.params}${if.result}${if.params}, ${end.if.params}${method.result.type}* ${method.result.name}${end.if.result}${if.signature.empty}void${end.if.signature.empty} ); diff --git a/languages/c/templates/methods/default.c b/languages/c/templates/methods/default.c index 6a023534..6fbca47e 100644 --- a/languages/c/templates/methods/default.c +++ b/languages/c/templates/methods/default.c @@ -1,24 +1,21 @@ /* ${method.name} - ${method.description} */ -uint32_t ${info.title}_${method.Name}(${method.params.list}${if.params}, ${end.if.params}${method.result.type}* ${method.result.name}) { - uint32_t status = FireboltSDKErrorUnavailable; - FireboltSDK::Transport* transport = FireboltSDK::Accessor::Instance().GetTransport(); - if (transport != nullptr) { - - JsonObject jsonParameters; +uint32_t ${info.Title}_${method.Name}( ${method.signature.params}${if.result}${if.params}, ${end.if.params}${method.result.type}* ${method.result.name}${end.if.result}${if.signature.empty}void${end.if.signature.empty} ) { - ${if.params} -${method.params.json} - ${end.if.params} + uint32_t status = FireboltSDKErrorUnavailable; + FireboltSDK::Transport* transport = FireboltSDK::Accessor::Instance().GetTransport(); + if (transport != nullptr) { - WPEFramework::Core::JSON::Boolean jsonResult; - status = transport->Invoke("${info.title}.${method.name}", jsonParameters, jsonResult); - if (status == FireboltSDKErrorNone) { - *success = jsonResult.Value(); - } + ${method.params.serialization.with.indent} + ${method.result.json.type} jsonResult; + status = transport->Invoke("${info.title}.${method.name}", jsonParameters, jsonResult); + if (status == FireboltSDKErrorNone) { + FIREBOLT_LOG_INFO(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module(), "${info.Title}.${method.name} is successfully invoked"); +${method.result.instantiation} + } - } else { - FIREBOLT_LOG_ERROR(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module(), "Error in getting Transport err = %d", status); - } + } else { + FIREBOLT_LOG_ERROR(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module(), "Error in getting Transport err = %d", status); + } - return status; + return status; } diff --git a/languages/c/templates/methods/property.c b/languages/c/templates/methods/property.c index c7510da9..9a92dce4 100644 --- a/languages/c/templates/methods/property.c +++ b/languages/c/templates/methods/property.c @@ -1,5 +1,5 @@ /* ${method.name} - ${method.description} */ -uint32_t ${info.title}_Get${method.Name}( ${method.signature.params}${if.params}, ${end.if.params}${method.result.type}* ${method.result.name} ) +uint32_t ${info.Title}_Get${method.Name}( ${method.signature.params}${if.params}, ${end.if.params}${method.result.type}* ${method.result.name} ) { const string method = _T("${info.title}.${method.name}"); ${if.params}${method.params.serialization}${end.if.params} diff --git a/src/macrofier/engine.mjs b/src/macrofier/engine.mjs index 18091b7e..b4f4f9c8 100644 --- a/src/macrofier/engine.mjs +++ b/src/macrofier/engine.mjs @@ -1123,9 +1123,12 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {}) .replace(/\$\{method\.params\.array\}/g, JSON.stringify(methodObj.params.map(p => p.name))) .replace(/\$\{method\.params\.count}/g, methodObj.params ? methodObj.params.length : 0) .replace(/\$\{if\.params\}(.*?)\$\{end\.if\.params\}/gms, method.params.length ? '$1' : '') - .replace(/\$\{if\.params.empty\}(.*?)\$\{end\.if\.params.empty\}/gms, method.params.length === 0 ? '$1' : '') + .replace(/\$\{if\.result\}(.*?)\$\{end\.if\.result\}/gms, resultType ? '$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' : '') .replace(/\$\{if\.context\}(.*?)\$\{end\.if\.context\}/gms, event && event.params.length ? '$1' : '') .replace(/\$\{method\.params\.serialization\}/g, serializedParams) + .replace(/\$\{method\.params\.serialization\.with\.indent\}/g, indent(serializedParams, ' ')) // Typed signature stuff .replace(/\$\{method\.signature\}/g, types.getMethodSignature(methodObj, json, { isInterface: false, destination: state.destination, section: state.section })) .replace(/\$\{method\.signature\.params\}/g, types.getMethodSignatureParams(methodObj, json, { destination: state.destination, section: state.section })) @@ -1164,6 +1167,7 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {}) .replace(/\$\{event\.result\.json\.type\}/g, resultJsonType) .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) .replace(/\$\{method\.result\.instantiation\}/g, resultInst) .replace(/\$\{method\.example\.value\}/g, JSON.stringify(methodObj.examples[0].result.value)) .replace(/\$\{method\.alternative\}/g, method.alternative)