Skip to content

Commit

Permalink
CPPSDK: polymorphic-pull method/event generation support added
Browse files Browse the repository at this point in the history
  • Loading branch information
HaseenaSainul committed Nov 17, 2023
1 parent 77f2cfc commit d26e2c5
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
void subscribe( I${info.Title}::I${method.Name}Notification& notification, Firebolt::Error *err = nullptr ) override;
void unsubscribe( I${info.Title}::I${method.Name}Notification& notification, Firebolt::Error *err = nullptr ) override;
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/polymorphic-pull-event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* ${method.name} - ${method.description} */
struct I${method.Name}Notification {
virtual ${method.pulls.type} ${method.name}( ${method.pulls.param.type} ) = 0;
};
virtual void subscribe( I${method.Name}Notification& notification, Firebolt::Error *err = nullptr ) = 0;
virtual void unsubscribe( I${method.Name}Notification& notification, Firebolt::Error *err = nullptr ) = 0;
6 changes: 6 additions & 0 deletions languages/cpp/templates/declarations/polymorphic-pull.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;
60 changes: 60 additions & 0 deletions languages/cpp/templates/methods/polymorphic-pull-event.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* ${method.rpc.name} - ${method.description} */
static void ${method.name}InnerCallback( void* notification, const void* userData, void* jsonResponse )
{
${event.callback.serialization}
ASSERT(proxyResponse.IsValid() == true);

if (proxyResponse.IsValid() == true) {
${method.pulls.param.json.type} jsonResult = proxyResponse->Parameters;
${method.pulls.response.initialization}
${method.pulls.response.instantiation}

I${info.Title}::I${method.Name}Notification& notifier = *(reinterpret_cast<I${info.Title}::I${method.Name}Notification*>(notification));
${method.pulls.type} element = notifier.${method.name}(${method.pulls.param.title});
Firebolt::Error status = Firebolt::Error::NotConnected;
FireboltSDK::Transport<WPEFramework::Core::JSON::IElement>* transport = FireboltSDK::Accessor::Instance().GetTransport();
if (transport != nullptr) {
JsonObject jsonParameters;
WPEFramework::Core::JSON::Variant CorrelationId = proxyResponse->CorrelationId.Value();
jsonParameters.Set(_T("correlationId"), CorrelationId);
${method.pulls.json.type} ${method.pulls.result.title}Container;

${method.pulls.result.serialization.with.indent}
string resultStr;
${method.pulls.result.title}Container.ToString(resultStr);
WPEFramework::Core::JSON::VariantContainer resultContainer(resultStr);
WPEFramework::Core::JSON::Variant Result = resultContainer;
jsonParameters.Set(_T("result"), Result);
WPEFramework::Core::JSON::Boolean jsonResult;

status = transport->Invoke("${info.title.lowercase}.${method.pulls.for}", 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 pushed with status as %d", jsonResult.Value());
}

} else {
FIREBOLT_LOG_ERROR(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module<FireboltSDK::Accessor>(), "Error in getting Transport err = %d", status);
}
proxyResponse.Release();
}
}
void ${info.Title}Impl::subscribe( I${info.Title}::I${method.Name}Notification& notification, Firebolt::Error *err )
{
const string eventName = _T("${info.title.lowercase}.${method.rpc.name}");
Firebolt::Error status = Firebolt::Error::None;

JsonObject jsonParameters;
status = FireboltSDK::Event::Instance().Subscribe<${event.result.json.type}>(eventName, jsonParameters, ${method.name}InnerCallback, reinterpret_cast<void*>(&notification), nullptr);

if (err != nullptr) {
*err = status;
}
}
void ${info.Title}Impl::unsubscribe( I${info.Title}::I${method.Name}Notification& notification, Firebolt::Error *err )
{
Firebolt::Error status = FireboltSDK::Event::Instance().Unsubscribe(_T("${info.title.lowercase}.${method.rpc.name}"), reinterpret_cast<void*>(&notification));

if (err != nullptr) {
*err = status;
}
}
23 changes: 23 additions & 0 deletions languages/cpp/templates/methods/polymorphic-pull.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* ${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) {
string correlationId = "";
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}
}

} 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};
}
12 changes: 11 additions & 1 deletion src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,12 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {})
const pullsResultType = pullsResult && types.getSchemaShape(pullsResult, json, { destination: state.destination, templateDir: state.typeTemplateDir, section: state.section })
const pullsForType = pullsResult && types.getSchemaType(pullsResult, json, { destination: state.destination, templateDir: state.typeTemplateDir, section: state.section })
const pullsParamsType = pullsParams ? types.getSchemaShape(pullsParams, json, { destination: state.destination, templateDir: state.typeTemplateDir, section: state.section }) : ''
const pullsForParamTitle = pullsParams ? pullsParams.title.charAt(0).toLowerCase() + pullsParams.title.substring(1) : ''
const pullsForResultTitle = pullsResult ? pullsResult.title.charAt(0).toLowerCase() + pullsResult.title.substring(1) : ''
const pullsResponseInit = pullsParams ? types.getSchemaShape(pullsParams, json, { templateDir: 'result-initialization', property: pullsForParamTitle, required: pullsParams.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) : ''
const pullsResponseInst = pullsParams ? types.getSchemaShape(pullsParams, json, { templateDir: 'result-instantiation', property: pullsForParamTitle, required: pullsParams.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) : ''
const pullsResultSerialize = pullsResult ? types.getSchemaShape(pullsResult, json, { templateDir: 'parameter-serialization/sub-property', property: pullsForResultTitle, required: pullsResult.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) : ''

const serializedParams = flattenedMethod.params.map(param => types.getSchemaShape(param.schema, json, { templateDir: 'parameter-serialization', property: param.name, required: param.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true })).join('\n')
const resultInst = types.getSchemaShape(flattenedMethod.result.schema, json, { templateDir: 'result-instantiation', property: flattenedMethod.result.name, required: flattenedMethod.result.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) // w/out primitive: true, getSchemaShape skips anonymous types, like primitives
const resultInit = types.getSchemaShape(flattenedMethod.result.schema, json, { templateDir: 'result-initialization', property: flattenedMethod.result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) // w/out primitive: true, getSchemaShape skips anonymous types, like primitives
Expand All @@ -1274,7 +1280,6 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {})
}
}
// hmm... how is this different from callbackSerializedList? i guess they get merged?
//console.log("event --- ", event)
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 resultJsonType = result.schema ? types.getSchemaType(result.schema, json, { templateDir: 'json-types' }) : ''
Expand Down Expand Up @@ -1383,10 +1388,15 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {})
.replace(/\$\{method\.pulls\.type\}/g, pullsForType)
.replace(/\$\{method\.pulls\.json\.type\}/g, pullsForJsonType)
.replace(/\$\{method\.pulls\.result\}/g, pullsResultType)
.replace(/\$\{method\.pulls\.result\.title\}/g, pullsForResultTitle)
.replace(/\$\{method\.pulls\.params.type\}/g, pullsParams ? pullsParams.title : '')
.replace(/\$\{method\.pulls\.params\}/g, pullsParamsType)
.replace(/\$\{method\.pulls\.param\.type\}/g, pullsForParamType)
.replace(/\$\{method\.pulls\.param\.title\}/g, pullsForParamTitle)
.replace(/\$\{method\.pulls\.param\.json\.type\}/g, pullsForParamJsonType)
.replace(/\$\{method\.pulls\.response\.initialization\}/g, pullsResponseInit)
.replace(/\$\{method\.pulls\.response\.instantiation}/g, pullsResponseInst)
.replace(/\$\{method\.pulls\.result\.serialization\.with\.indent\}/g, indent(pullsResultSerialize, ' '))
.replace(/\$\{method\.setter\.for\}/g, setterFor)
.replace(/\$\{method\.puller\}/g, pullerTemplate) // must be last!!
.replace(/\$\{method\.setter\}/g, setterTemplate) // must be last!!
Expand Down
6 changes: 5 additions & 1 deletion src/macrofier/types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ const safeName = value => value.split(':').pop().replace(/[\.\-]/g, '_').replace
// TODO: This is what's left of getMethodSignatureParams. We need to figure out / handle C's `FireboltTypes_StringHandle`
function getMethodSignatureParams(method, module, { destination, callback }) {
const paramOptional = getTemplate('/parameters/optional')
let polymorphicPull = method.tags.find(t => t.name === 'polymorphic-pull')
return method.params.map(param => {
if (polymorphicPull && (param.name === 'correlationId')) {
return
}
let type = getSchemaType(param.schema, module, { destination, namespace : true })
if (callback && allocatedPrimitiveProxies[type]) {
type = allocatedPrimitiveProxies[type]
Expand All @@ -93,7 +97,7 @@ function getMethodSignatureParams(method, module, { destination, callback }) {
}

return (param.required ? paramRequired : paramOptional).replace(/\$\{method\.param\.name\}/g, param.name).replace(/\$\{method\.param\.type\}/g, type)
}).join(', ')
}).filter(param => param).join(', ')
}

function getMethodSignatureResult(method, module, { destination, callback }) {
Expand Down
7 changes: 7 additions & 0 deletions src/shared/modules.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,7 @@ const createPolymorphicMethods = (method, json) => {
}

const isSubSchema = (schema) => schema.type === 'object' || (schema.type === 'string' && schema.enum)
const isSubEnumOfArraySchema = (schema) => (schema.type === 'array' && schema.items.enum)

const addComponentSubSchemasNameForProperties = (key, schema) => {
if ((schema.type === "object") && schema.properties) {
Expand All @@ -998,6 +999,12 @@ const addComponentSubSchemasNameForProperties = (key, schema) => {
}
propSchema = addComponentSubSchemasNameForProperties(key, propSchema)
}
else if (isSubEnumOfArraySchema(propSchema)) {
key = key + name.charAt(0).toUpperCase() + name.substring(1)
if (!propSchema.items.title) {
propSchema.items.title = key
}
}
})
}

Expand Down

0 comments on commit d26e2c5

Please sign in to comment.