Skip to content

Commit

Permalink
CPPSDK: calls metrics support added (#158)
Browse files Browse the repository at this point in the history
* CPPSDK: calls metrics support added
  • Loading branch information
HaseenaSainul committed Dec 15, 2023
1 parent 80d95ee commit 8901760
Show file tree
Hide file tree
Showing 23 changed files with 92 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
string ${property}Str;
response.${Property.dependency}${Property}.ToString(${property}Str);
response${Property.dependency}.${Property}.ToString(${property}Str);
${base.title}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = ${property}Str;
Original file line number Diff line number Diff line change
@@ -1 +1 @@
${base.title}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = response.${Property.dependency}${Property};
${base.title}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = response${Property.dependency}.${Property};
Original file line number Diff line number Diff line change
@@ -1 +1 @@
${base.title}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = response.${Property.dependency}${Property};
${base.title}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = response${Property.dependency}.${Property};
Original file line number Diff line number Diff line change
@@ -1 +1 @@
${shape} ${if.non.const}${if.non.anyOf}${if.non.array}${if.non.object}${base.title}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = response.${Property.dependency}${Property};${end.if.non.object}${end.if.non.array}${end.if.non.anyOf}${end.if.non.const}
${shape} ${if.non.const}${if.non.anyOf}${if.non.array}${if.non.object}${base.title}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = response${Property.dependency}.${Property};${end.if.non.object}${end.if.non.array}${end.if.non.anyOf}${end.if.non.const}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
${if.optional}if (proxyResponse->${Property.dependency}${Property}.IsSet()) {
${if.optional}if ((*proxyResponse)${Property.dependency}.${Property}.IsSet()) {
string ${property}Str;
proxyResponse->${Property.dependency}${Property}.ToString(${property}Str);
(*proxyResponse)${Property.dependency}.${Property}.ToString(${property}Str);
${base.title}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = ${property}Str;
}${end.if.optional}${if.non.optional}string ${property}Str;
proxyResponse->${Property.dependency}${Property}.ToString(${property}Str);
(*proxyResponse)${Property.dependency}.${Property}.ToString(${property}Str);
${base.title}${property.dependency}.${property} = ${property}Str;${end.if.non.optional}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
${if.optional}if (proxyResponse->${Property.dependency}${Property}.IsSet()) {
${base.title}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = proxyResponse->${Property.dependency}${Property};
}${end.if.optional}${if.non.optional}${base.title}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = proxyResponse->${Property.dependency}${Property};${end.if.non.optional}
${if.optional}if ((*proxyResponse)${Property.dependency}.${Property}.IsSet()) {
${base.title}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = (*proxyResponse)${Property.dependency}.${Property};
}${end.if.optional}${if.non.optional}${base.title}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = (*proxyResponse)${Property.dependency}.${Property};${end.if.non.optional}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
${if.optional} if (proxyResponse->${Property.dependency}IsSet()) {
${if.optional} if ((*proxyResponse)${Property.dependency}.IsSet()) {
${base.title}${property.dependency} = std::make_optional<${type}>();
${properties}
}${end.if.optional}${if.non.optional} {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
${shape} ${if.non.const}${if.non.anyOf}${if.non.array}${if.non.object}${if.optional}if (proxyResponse->${Property.dependency}${Property}.IsSet()) {
${base.title}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = proxyResponse->${Property.dependency}${Property};
}${end.if.optional}${if.non.optional}${base.title}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = proxyResponse->${Property.dependency}${Property};${end.if.non.optional}${end.if.non.object}${end.if.non.array}${end.if.non.anyOf}${end.if.non.const}
${shape} ${if.non.const}${if.non.anyOf}${if.non.array}${if.non.object}${if.optional}if ((*proxyResponse)${Property.dependency}.${Property}.IsSet()) {
${base.title}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = (*proxyResponse)${Property.dependency}.${Property};
}${end.if.optional}${if.non.optional}${base.title}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = (*proxyResponse)${Property.dependency}.${Property};${end.if.non.optional}${end.if.non.object}${end.if.non.array}${end.if.non.anyOf}${end.if.non.const}
5 changes: 5 additions & 0 deletions languages/cpp/templates/declarations-override/calls-metrics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*
${method.name}
${method.description}
*/
${method.signature.result} ${method.name}( ${method.signature.params}${if.params}, ${end.if.params}Firebolt::Error *err = nullptr )${if.result.nonvoid}${if.params.empty} const${end.if.params.empty}${end.if.result.nonvoid} override;
6 changes: 6 additions & 0 deletions languages/cpp/templates/declarations/calls-metrics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
${method.name}
${method.description}
${method.params.annotations}${if.deprecated} * @deprecated ${method.deprecation}${end.if.deprecated}
*/
virtual ${method.signature.result} ${method.name}( ${method.signature.params}${if.params}, ${end.if.params}Firebolt::Error *err = nullptr )${if.result.nonvoid}${if.params.empty} const${end.if.params.empty}${end.if.result.nonvoid} = 0;
1 change: 1 addition & 0 deletions languages/cpp/templates/imports/calls-metrics.impl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "metrics_impl.h"
29 changes: 29 additions & 0 deletions languages/cpp/templates/methods/calls-metrics.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* ${method.rpc.name} - ${method.description} */
static void ${method.name}Dispatcher(const void* result) {
Metrics::MetricsImpl::${method.name}(${if.result.nonboolean}${if.result.nonvoid}(static_cast<${method.result.json.type}>(const_cast<void*>(result)))${end.if.result.nonvoid}${end.if.result.nonboolean});
}
/* ${method.rpc.name} - ${method.description} */
${method.signature.result} ${info.Title}Impl::${method.name}( ${method.signature.params}${if.params}, ${end.if.params}Firebolt::Error *err ) ${if.result.nonvoid}${if.params.empty} const${end.if.params.empty}${end.if.result.nonvoid}
{
Firebolt::Error status = Firebolt::Error::NotConnected;
${if.result.nonvoid}${method.result.initialization}${end.if.result.nonvoid}
FireboltSDK::Transport<WPEFramework::Core::JSON::IElement>* transport = FireboltSDK::Accessor::Instance().GetTransport();
if (transport != nullptr) {

JsonObject jsonParameters;
${method.params.serialization.with.indent}
${method.result.json.type} jsonResult;
status = transport->Invoke("${info.title.lowercase}.${method.rpc.name}", jsonParameters, jsonResult);
if (status == Firebolt::Error::None) {
FIREBOLT_LOG_INFO(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module<FireboltSDK::Accessor>(), "${info.Title}.${method.rpc.name} is successfully invoked");
${if.result.nonvoid}${method.result.instantiation.with.indent}${end.if.result.nonvoid}
WPEFramework::Core::ProxyType<WPEFramework::Core::IDispatch> job = WPEFramework::Core::ProxyType<WPEFramework::Core::IDispatch>(WPEFramework::Core::ProxyType<FireboltSDK::Worker>::Create(${method.name}Dispatcher, nullptr));
WPEFramework::Core::IWorkerPool::Instance().Submit(job);
}

} else {
FIREBOLT_LOG_ERROR(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module<FireboltSDK::Accessor>(), "Error in getting Transport err = %d", status);
}

return${if.result.nonvoid} ${method.result.name}${end.if.result.nonvoid};
}
2 changes: 2 additions & 0 deletions languages/cpp/templates/modules/src/module_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include "FireboltSDK.h"
#include "IModule.h"
/* ${IMPORTS} */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
${if.optional}if (element${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property}.has_value()) {
${base.title}Container.${Property.dependency}${Property} = element${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property}.value();
}${end.if.optional}${if.non.optional}${base.title}Container.${Property.dependency}${Property} = element${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property};${end.if.non.optional}
${base.title}Container${Property.dependency}.${Property} = element${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property}.value();
}${end.if.optional}${if.non.optional}${base.title}Container${Property.dependency}.${Property} = element${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property};${end.if.non.optional}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
for (auto& element : ${property}) {
${if.object}${items.with.indent}${end.if.object}${if.non.object} ${property}Array.Add() = element;${end.if.non.object}
}
${base.title}Container.${Property.dependency}Add(_T("${property}"), &${property}Array);
${base.title}Container${Property.dependency}.${Property} = ${property}Array;
}${end.if.impl.array.optional}${if.impl.array.non.optional}WPEFramework::Core::JSON::ArrayType<${json.type}> ${property}Array;
${type} ${property} = element${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property};
for (auto& element : ${property}) {
${if.object}${items.with.indent}${end.if.object}${if.non.object} ${property}Array.Add() = element;${end.if.non.object}
}
${base.title}Container.${Property.dependency}Add(_T("${property}"), &${property}Array);${end.if.impl.array.non.optional}
${base.title}Container${Property.dependency}.${Property} = ${property}Array;${end.if.impl.array.non.optional}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
${if.optional}if (element${property.dependency}.${property}.has_value()) {
${base.title}Container.${Property.dependency}${Property} = element${property.dependency}.${property}.value();
}${end.if.optional}${if.non.optional}${base.title}Container.${Property.dependency}${Property} = element${property.dependency}.${property};${end.if.non.optional}
${base.title}Container${Property.dependency}.${Property} = element${property.dependency}.${property}.value();
}${end.if.optional}${if.non.optional}${base.title}Container${Property.dependency}.${Property} = element${property.dependency}.${property};${end.if.non.optional}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
${shape} ${if.non.const}${if.non.anyOf}${if.non.array}${if.non.object}${if.optional}if (element${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property}.has_value()) {
${base.title}Container.${Property.dependency}${Property} = element${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property}.value();
}${end.if.optional}${if.non.optional}${base.title}Container.${Property.dependency}${Property} = element${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property};${end.if.non.optional}${end.if.non.object}${end.if.non.array}${end.if.non.anyOf}${end.if.non.const}
${base.title}Container${Property.dependency}.${Property} = element${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property}.value();
}${end.if.optional}${if.non.optional}${base.title}Container${Property.dependency}.${Property} = element${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property};${end.if.non.optional}${end.if.non.object}${end.if.non.array}${end.if.non.anyOf}${end.if.non.const}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
${if.optional}if (jsonResult.${Property.dependency}${Property}.IsSet()) {
${if.optional}if (jsonResult${Property.dependency}.${Property}.IsSet()) {
string ${property}Str;
jsonResult.${Property.dependency}${Property}.ToString(${property}Str);
jsonResult${Property.dependency}.${Property}.ToString(${property}Str);
${base.title}Result${level}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = ${property}Str;
}${end.if.optional}${if.non.optional}string ${property}Str;
jsonResult.${Property.dependency}${Property}.ToString(${property}Str);
jsonResult${Property.dependency}.${Property}.ToString(${property}Str);
${base.title}Result${level}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = ${property}Str;${end.if.non.optional}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
${if.optional} if (jsonResult.${Property.dependency}IsSet()) {
${if.optional} if (jsonResult${Property.dependency}.IsSet()) {
${base.title}Result${level}${property.dependency} = std::make_optional<${type}>();
${properties}
}${end.if.optional}${if.non.optional} {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
${shape} ${if.non.anyOf}${if.non.array}${if.non.object}${if.optional}if (jsonResult.${Property.dependency}${Property}.IsSet()) {
${base.title}Result${level}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = jsonResult.${Property.dependency}${Property};
}${end.if.optional}${if.non.optional}${base.title}Result${level}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = jsonResult.${Property.dependency}${Property};${end.if.non.optional}${end.if.non.object}${end.if.non.array}${end.if.non.anyOf}
${shape} ${if.non.anyOf}${if.non.array}${if.non.object}${if.optional}if (jsonResult${Property.dependency}.${Property}.IsSet()) {
${base.title}Result${level}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = jsonResult${Property.dependency}.${Property};
}${end.if.optional}${if.non.optional}${base.title}Result${level}${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}.${property} = jsonResult${Property.dependency}.${Property};${end.if.non.optional}${end.if.non.object}${end.if.non.array}${end.if.non.anyOf}
30 changes: 18 additions & 12 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,20 @@ function getRelatedSchemaLinks(schema = {}, json = {}, templates = {}, options =
return links
}

function getTemplateFromDestination(destination, templateName, templates) {
const destinationArray = destination.split('/').pop().split(/[_.]+/)

let template = ''
destinationArray.filter(value => value).every((suffix) => {
template = getTemplate(templateName +`.${suffix}`, templates)
return template ? false: true
})
if (!template) {
template = getTemplate(templateName, templates)
}
return template
}

const generateImports = (json, templates, options = { destination: '' }) => {
let imports = ''

Expand Down Expand Up @@ -1011,21 +1025,11 @@ const generateImports = (json, templates, options = { destination: '' }) => {
imports += getTemplate('/imports/x-method', templates)
}

const suffix = options.destination.split('.').pop()
if (callsMetrics(json).length) {
imports += getTemplate(suffix ? `/imports/calls-metrics.${suffix}` : '/imports/calls-metrics', templates)
}

const destinationArray = options.destination.split('/').pop().split(/[_.]+/)
let template = ''
destinationArray.filter(value => value).every((suffix) => {
template = getTemplate(`/imports/default.${suffix}`, templates)
return template ? false: true
})
if (!template) {
template = getTemplate('/imports/default', templates)
imports += getTemplateFromDestination(options.destination, '/imports/calls-metrics', templates)
}

let template = getTemplateFromDestination(options.destination, '/imports/default', templates)
if (json['x-schemas'] && Object.keys(json['x-schemas']).length > 0 && !json.info['x-uri-titles']) {
imports += Object.keys(json['x-schemas']).map(shared => template.replace(/\$\{info.title.lowercase\}/g, shared.toLowerCase())).join('')
}
Expand Down Expand Up @@ -1312,6 +1316,7 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {})
// hmm... how is this different from callbackSerializedList? i guess they get merged?
const callbackResponseInst = event ? (event.params && event.params.length ? (event.params.map(param => types.getSchemaShape(param.schema, json, { templateDir: 'callback-response-instantiation', property: param.name, required: param.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true })).join(', ')) + ', ' : '' ) + (types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-response-instantiation', property: result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true })) : ''
const resultType = result.schema ? types.getSchemaType(result.schema, json, { templateDir: state.typeTemplateDir }) : ''
const resultSchemaType = result.schema.type
const resultJsonType = result.schema ? types.getSchemaType(result.schema, json, { templateDir: 'json-types' }) : ''
const resultParams = generateResultParams(result.schema, json, templates, { name: result.name})

Expand Down Expand Up @@ -1357,6 +1362,7 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {})
.replace(/\$\{if\.params\}(.*?)\$\{end\.if\.params\}/gms, method.params.length ? '$1' : '')
.replace(/\$\{if\.result\}(.*?)\$\{end\.if\.result\}/gms, resultType ? '$1' : '')
.replace(/\$\{if\.result.nonvoid\}(.*?)\$\{end\.if\.result.nonvoid\}/gms, resultType && resultType !== 'void' ? '$1' : '')
.replace(/\$\{if\.result.nonboolean\}(.*?)\$\{end\.if\.result.nonboolean\}/gms, resultSchemaType && resultSchemaType !== 'boolean' ? '$1' : '')
.replace(/\$\{if\.result\.properties\}(.*?)\$\{end\.if\.result\.properties\}/gms, resultParams ? '$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' : '')
Expand Down
2 changes: 1 addition & 1 deletion src/macrofier/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ const macrofy = async (

const staticCodeList = staticContent ? await readDir(staticContent, { recursive: true }) : []
const staticModules = staticModuleNames.map(name => ( { info: { title: name } } ))

let modules
if (hidePrivate) {
modules = moduleList.map(name => getModule(name, openrpc, copySchemasIntoModules, extractSubSchemas)).filter(hasPublicAPIs)
Expand Down
Loading

0 comments on commit 8901760

Please sign in to comment.