Skip to content

Commit

Permalink
review update
Browse files Browse the repository at this point in the history
  • Loading branch information
HaseenaSainul committed Jul 17, 2023
1 parent 15be71e commit 15c17a9
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 123 deletions.
4 changes: 2 additions & 2 deletions languages/c/templates/codeblocks/setter.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* ${method.json.name} - ${method.description} */
/* ${method.rpc.name} - ${method.description} */
uint32_t ${info.Title}_${method.Name}( ${method.signature.params} )
{
const string method = _T("${info.title}.${method.json.name}");
const string method = _T("${info.title.lowercase}.${method.rpc.name}");
${if.params}${method.params.serialization}${end.if.params}
return FireboltSDK::Properties::Set(method, jsonParameters);
}
6 changes: 3 additions & 3 deletions languages/c/templates/methods/default.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* ${method.json.name} - ${method.description} */
/* ${method.rpc.name} - ${method.description} */
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} ) {

uint32_t status = FireboltSDKErrorUnavailable;
Expand All @@ -7,9 +7,9 @@ uint32_t ${info.Title}_${method.Name}( ${method.signature.params}${if.result}${i

${method.params.serialization.with.indent}
${method.result.json.type} jsonResult;
status = transport->Invoke("${info.title}.${method.json.name}", jsonParameters, jsonResult);
status = transport->Invoke("${info.title.lowercase}.${method.rpc.name}", jsonParameters, jsonResult);
if (status == FireboltSDKErrorNone) {
FIREBOLT_LOG_INFO(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module<FireboltSDK::Accessor>(), "${info.Title}.${method.json.name} is successfully invoked");
FIREBOLT_LOG_INFO(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module<FireboltSDK::Accessor>(), "${info.Title}.${method.rpc.name} is successfully invoked");
${method.result.instantiation}
}

Expand Down
6 changes: 3 additions & 3 deletions languages/c/templates/methods/event.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* ${method.json.name} - ${method.description} */
/* ${method.rpc.name} - ${method.description} */
static void ${info.Title}${method.Name}InnerCallback( void* userCB, const void* userData, void* response )
{
${event.callback.params.serialization}
Expand All @@ -11,7 +11,7 @@ static void ${info.Title}${method.Name}InnerCallback( void* userCB, const void*
}
uint32_t ${info.Title}_Register_${method.Name}( ${event.signature.params}${if.event.params}, ${end.if.event.params}${info.Title}${method.Name}Callback userCB, const void* userData )
{
const string eventName = _T("${info.title}.${method.json.name}");
const string eventName = _T("${info.title.lowercase}.${method.rpc.name}");
uint32_t status = FireboltSDKErrorNone;

if (userCB != nullptr) {
Expand All @@ -22,5 +22,5 @@ uint32_t ${info.Title}_Register_${method.Name}( ${event.signature.params}${if.ev
}
uint32_t ${info.Title}_Unregister_${method.Name}( ${info.Title}${method.Name}Callback userCB)
{
return FireboltSDK::Event::Instance().Unsubscribe(_T("${info.title}.${method.json.name}"), reinterpret_cast<void*>(userCB));
return FireboltSDK::Event::Instance().Unsubscribe(_T("${info.title.lowercase}.${method.rpc.name}"), reinterpret_cast<void*>(userCB));
}
10 changes: 5 additions & 5 deletions languages/c/templates/methods/polymorphic-pull-event.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* ${method.json.name} - ${method.description} */
/* ${method.rpc.name} - ${method.description} */
static void ${info.Title}${method.Name}InnerCallback( void* userCB, const void* userData, void* response )
{
${event.callback.params.serialization}
Expand Down Expand Up @@ -27,9 +27,9 @@ static void ${info.Title}${method.Name}InnerCallback( void* userCB, const void*
FireboltSDK::Transport<WPEFramework::Core::JSON::IElement>* transport = FireboltSDK::Accessor::Instance().GetTransport();
if (transport != nullptr) {
WPEFramework::Core::JSON::Boolean jsonResult;
uint32_t status = transport->Invoke(_T("${info.title}.${method.pulls.for}"), jsonParameters, jsonResult);
uint32_t status = transport->Invoke(_T("${info.title.lowercase}.${method.pulls.for}"), jsonParameters, jsonResult);
if (status == FireboltSDKErrorNone) {
FIREBOLT_LOG_INFO(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module<FireboltSDK::Accessor>(), "${info.Title}.${method.json.name} is successfully pushed with status as %d", jsonResult.Value());
FIREBOLT_LOG_INFO(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module<FireboltSDK::Accessor>(), "${info.Title}.${method.rpc.name} is successfully pushed with status as %d", jsonResult.Value());
status = (jsonResult.Value() == true) ? FireboltSDKErrorNone : FireboltSDKErrorNotSupported;
}
} else {
Expand All @@ -39,7 +39,7 @@ static void ${info.Title}${method.Name}InnerCallback( void* userCB, const void*
}
uint32_t ${info.Title}_Register_${method.Name}( ${info.Title}${method.Name}Callback userCB, const void* userData )
{
const string eventName = _T("${info.title}.${method.json.name}");
const string eventName = _T("${info.title.lowercase}.${method.rpc.name}");
uint32_t status = FireboltSDKErrorNone;

if (userCB != nullptr) {
Expand All @@ -50,5 +50,5 @@ uint32_t ${info.Title}_Register_${method.Name}( ${info.Title}${method.Name}Callb
}
uint32_t ${info.Title}_Unregister_${method.Name}( ${info.Title}${method.Name}Callback userCB)
{
return FireboltSDK::Event::Instance().Unsubscribe(_T("${info.title}.${method.json.name}"), reinterpret_cast<void*>(userCB));
return FireboltSDK::Event::Instance().Unsubscribe(_T("${info.title.lowercase}.${method.rpc.name}"), reinterpret_cast<void*>(userCB));
}
6 changes: 3 additions & 3 deletions languages/c/templates/methods/polymorphic-pull.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* ${method.json.name} - ${method.description} */
/* ${method.rpc.name} - ${method.description} */
uint32_t ${info.Title}_Push${method.Name}( ${method.signature.params} )
{
uint32_t status = FireboltSDKErrorUnavailable;
Expand All @@ -9,9 +9,9 @@ uint32_t ${info.Title}_Push${method.Name}( ${method.signature.params} )
${method.params.serialization.with.indent}

WPEFramework::Core::JSON::Boolean jsonResult;
status = transport->Invoke(_T("${info.title}.${method.json.name}"), jsonParameters, jsonResult);
status = transport->Invoke(_T("${info.title.lowercase}.${method.rpc.name}"), jsonParameters, jsonResult);
if (status == FireboltSDKErrorNone) {
FIREBOLT_LOG_INFO(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module<FireboltSDK::Accessor>(), "${info.Title}.${method.json.name} is successfully pushed with status as %d", jsonResult.Value());
FIREBOLT_LOG_INFO(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module<FireboltSDK::Accessor>(), "${info.Title}.${method.rpc.name} is successfully pushed with status as %d", jsonResult.Value());
status = (jsonResult.Value() == true) ? FireboltSDKErrorNone : FireboltSDKErrorNotSupported;
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions languages/c/templates/methods/property.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* ${method.json.name} - ${method.description} */
/* ${method.rpc.name} - ${method.description} */
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.json.name}");
const string method = _T("${info.title.lowercase}.${method.rpc.name}");
${if.params}${method.params.serialization}${end.if.params}
${method.result.json} jsonResult;
${if.params}uint32_t status = FireboltSDK::Properties::Get(method, jsonParameters, jsonResult);${end.if.params}
Expand Down
55 changes: 26 additions & 29 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].toLowerCase()
title = ref.split('/')[2]
}
}
title && !refSchemas.includes(title) ? refSchemas.push(title) : null
Expand Down Expand Up @@ -416,6 +416,21 @@ const generateMacros = (obj, templates, languages, options = {}) => {
if (config.extractSubSchemas) {
obj = promoteAndNameSubSchemas(obj)
}
if (options.createPolymorphicMethods) {
let methods = []
obj.methods && obj.methods.forEach(method => {
let polymorphicMethods = createPolymorphicMethods(method, obj)
if (polymorphicMethods.length > 1) {
polymorphicMethods.forEach(polymorphicMethod => {
methods.push(polymorphicMethod)
})
}
else {
methods.push(method)
}
})
obj.methods = methods
}

// grab the options so we don't have to pass them from method to method
Object.assign(state, options)
Expand All @@ -426,7 +441,7 @@ const generateMacros = (obj, templates, languages, options = {}) => {
const eventsEnum = generateEvents(obj, templates)
const examples = generateExamples(obj, templates, languages)

const allMethodsArray = generateMethods(obj, examples, templates, { polymorphic : options.createPolymorphicMethods })
const allMethodsArray = generateMethods(obj, examples, templates)
const methodsArray = allMethodsArray.filter(m => !m.event && (!options.hideExcluded || !m.excluded))
const eventsArray = allMethodsArray.filter(m => m.event && (!options.hideExcluded || !m.excluded))
const declarationsArray = allMethodsArray.filter(m => m.declaration)
Expand Down Expand Up @@ -951,30 +966,12 @@ function generateExamples(json = {}, mainTemplates = {}, languages = {}) {
return examples
}

function generateMethods(json = {}, examples = {}, templates = {}, options = { polymorphic: false }) {
function generateMethods(json = {}, examples = {}, templates = {}) {
const methods = compose(
option([]),
getMethods
)(json)

let methodList = []
if (options.polymorphic) {
methods.forEach(method => {
let polymorphicMethods = createPolymorphicMethods(method, json)
if (polymorphicMethods.length > 1) {
polymorphicMethods.forEach(polymorphicMethod => {
methodList.push(polymorphicMethod)
})
}
else {
methodList.push(method)
}
})
}
else {
methodList = methods
}

// Code to generate methods
const results = reduce((acc, methodObj, i, arr) => {
const result = {
Expand All @@ -988,21 +985,21 @@ function generateMethods(json = {}, examples = {}, templates = {}, options = { p
let template = getTemplateForMethod(methodObj, templates);

if (template && template.length) {
let javascript = insertMethodMacros(template, methodObj, json, templates, options, examples)
let javascript = insertMethodMacros(template, methodObj, json, templates, examples)
result.body = javascript
}

template = getTemplateForDeclaration(methodObj, templates)

if (template && template.length) {
let javascript = insertMethodMacros(template, methodObj, json, templates, options, examples)
let javascript = insertMethodMacros(template, methodObj, json, templates, examples)
result.declaration = javascript
}

acc.push(result)

return acc
}, [], methodList)
}, [], methods)

// TODO: might be useful to pass in local macro for an array with all capability & provider interface names
if (json.methods && json.methods.find(isProviderInterfaceMethod)) {
Expand Down Expand Up @@ -1040,7 +1037,7 @@ function generateMethods(json = {}, examples = {}, templates = {}, options = { p
}

// TODO: this is called too many places... let's reduce that to just generateMethods
function insertMethodMacros(template, methodObj, json, templates, options = { polymorphic: false }, examples={}) {
function insertMethodMacros(template, methodObj, json, templates, examples={}) {
const moduleName = getModuleName(json)

const info = {
Expand Down Expand Up @@ -1104,11 +1101,11 @@ function insertMethodMacros(template, methodObj, json, templates, options = { po
// grab some related methdos in case they are output together in a single template file
const puller = json.methods.find(method => method.tags.find(tag => tag['x-pulls-for'] === methodObj.name))
const pullsFor = methodObj.tags.find(t => t['x-pulls-for']) && json.methods.find(method => method.name === methodObj.tags.find(t => t['x-pulls-for'])['x-pulls-for'])
const pullerTemplate = (puller ? insertMethodMacros(getTemplate('/codeblocks/puller', templates), puller, json, templates, options, examples) : '')
const pullerTemplate = (puller ? insertMethodMacros(getTemplate('/codeblocks/puller', templates), puller, json, templates, examples) : '')
const setter = getSetterFor(methodObj.name, json)
const setterTemplate = (setter ? insertMethodMacros(getTemplate('/codeblocks/setter', templates), setter, json, templates, options, examples) : '')
const setterTemplate = (setter ? insertMethodMacros(getTemplate('/codeblocks/setter', templates), setter, json, templates, examples) : '')
const subscriber = json.methods.find(method => method.tags.find(tag => tag['x-alternative'] === methodObj.name))
const subscriberTemplate = (subscriber ? insertMethodMacros(getTemplate('/codeblocks/subscriber', templates), subscriber, json, templates, options, examples) : '')
const subscriberTemplate = (subscriber ? insertMethodMacros(getTemplate('/codeblocks/subscriber', templates), subscriber, json, templates, examples) : '')
const setterFor = methodObj.tags.find(t => t.name === 'setter') && methodObj.tags.find(t => t.name === 'setter')['x-setter-for'] || ''
const pullsResult = (puller || pullsFor) ? localizeDependencies(pullsFor || methodObj, json).params[1].schema : null
const pullsParams = (puller || pullsFor) ? localizeDependencies(getPayloadFromEvent(puller || methodObj), json, null, { mergeAllOfs: true }).properties.parameters : null
Expand Down Expand Up @@ -1149,7 +1146,7 @@ function insertMethodMacros(template, methodObj, json, templates, options = { po
template = insertExampleMacros(template, examples[methodObj.name] || [], methodObj, json, templates)

template = template.replace(/\$\{method\.name\}/g, method.name)
.replace(/\$\{method\.json\.name\}/g, options.polymorphic ? methodObj.actualName || method.name : method.name)
.replace(/\$\{method\.rpc\.name\}/g, methodObj.title || methodObj.name)
.replace(/\$\{method\.summary\}/g, methodObj.summary)
.replace(/\$\{method\.description\}/g, methodObj.description
|| methodObj.summary)
Expand Down
Loading

0 comments on commit 15c17a9

Please sign in to comment.