Skip to content

Commit

Permalink
CPPSDK: polymorphic method schema creation changes (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
HaseenaSainul authored Dec 21, 2023
1 parent 4a63789 commit c200483
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 40 deletions.
4 changes: 2 additions & 2 deletions languages/cpp/templates/codeblocks/setter.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

/* ${method.rpc.name} - ${method.description} */
/* ${method.name} - ${method.description} */
void ${info.Title}Impl::${method.name}( ${method.signature.params}${if.params}, ${end.if.params}Firebolt::Error *err )
{
const string method = _T("${info.title.lowercase}.${method.rpc.name}");
const string method = _T("${info.title.lowercase}.${method.name}");

JsonObject jsonParameters;
${if.params}${method.params.serialization}${end.if.params}
Expand Down
2 changes: 1 addition & 1 deletion languages/cpp/templates/declarations-override/setter.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* ${method.rpc.name}
* ${method.name}
* ${method.description}
*/
void ${method.name}( ${method.signature.params}${if.params}, ${end.if.params}Firebolt::Error *err = nullptr ) override;
2 changes: 1 addition & 1 deletion languages/cpp/templates/declarations/event.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* ${method.name} - ${method.description} */
struct I${method.Name}Notification {
virtual void ${method.name}( ${event.signature.callback.params}${if.event.params}, ${end.if.event.params}${event.result.type} ) = 0;
virtual void ${method.rpc.name}( ${event.signature.callback.params}${if.event.params}, ${end.if.event.params}${event.result.type} ) = 0;
};
// signature callback params: ${event.signature.callback.params}
// method result properties : ${method.result.properties}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* ${method.name} - ${method.description} */
struct I${method.Name}Notification {
virtual ${method.pulls.type} ${method.name}( ${method.pulls.param.type} ) = 0;
virtual ${method.pulls.type} ${method.rpc.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;
2 changes: 1 addition & 1 deletion languages/cpp/templates/declarations/setter.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
${method.rpc.name}
${method.name}
${method.description}
*/
virtual void ${method.name}( ${method.signature.params}${if.params}, ${end.if.params}Firebolt::Error *err = nullptr ) = 0;
8 changes: 4 additions & 4 deletions languages/cpp/templates/methods/calls-metrics.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* ${method.rpc.name} - ${method.description} */
/* ${method.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.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;
Expand All @@ -13,9 +13,9 @@
JsonObject jsonParameters;
${method.params.serialization.with.indent}
${method.result.json.type} jsonResult;
status = transport->Invoke("${info.title.lowercase}.${method.rpc.name}", jsonParameters, jsonResult);
status = transport->Invoke("${info.title.lowercase}.${method.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");
FIREBOLT_LOG_INFO(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module<FireboltSDK::Accessor>(), "${info.Title}.${method.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);
Expand Down
6 changes: 3 additions & 3 deletions languages/cpp/templates/methods/default.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* ${method.rpc.name} - ${method.description} */
/* ${method.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;
Expand All @@ -9,9 +9,9 @@
JsonObject jsonParameters;
${method.params.serialization.with.indent}
${method.result.json.type} jsonResult;
status = transport->Invoke("${info.title.lowercase}.${method.rpc.name}", jsonParameters, jsonResult);
status = transport->Invoke("${info.title.lowercase}.${method.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");
FIREBOLT_LOG_INFO(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module<FireboltSDK::Accessor>(), "${info.Title}.${method.name} is successfully invoked");
${if.result.nonvoid}${method.result.instantiation.with.indent}${end.if.result.nonvoid}
}

Expand Down
4 changes: 2 additions & 2 deletions languages/cpp/templates/methods/event.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* ${method.rpc.name} - ${method.description} */
/* ${method.name} - ${method.description} */
static void ${method.name}InnerCallback( void* notification, const void* userData, void* jsonResponse )
{
${event.callback.serialization}
Expand All @@ -11,7 +11,7 @@
proxyResponse.Release();

I${info.Title}::I${method.Name}Notification& notifier = *(reinterpret_cast<I${info.Title}::I${method.Name}Notification*>(notification));
notifier.${method.name}(${event.callback.response.instantiation});
notifier.${method.rpc.name}(${event.callback.response.instantiation});
}
}
void ${info.Title}Impl::subscribe( ${event.signature.params}${if.event.params}, ${end.if.event.params}I${info.Title}::I${method.Name}Notification& notification, Firebolt::Error *err )
Expand Down
6 changes: 3 additions & 3 deletions languages/cpp/templates/methods/polymorphic-pull-event.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* ${method.rpc.name} - ${method.description} */
/* ${method.name} - ${method.description} */
static void ${method.name}InnerCallback( void* notification, const void* userData, void* jsonResponse )
{
${event.callback.serialization}
Expand All @@ -10,7 +10,7 @@
${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});
${method.pulls.type} element = notifier.${method.rpc.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) {
Expand All @@ -30,7 +30,7 @@

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());
FIREBOLT_LOG_INFO(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module<FireboltSDK::Accessor>(), "${info.Title}.${method.name} is successfully pushed with status as %d", jsonResult.Value());
}

} else {
Expand Down
6 changes: 3 additions & 3 deletions languages/cpp/templates/methods/polymorphic-pull.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* ${method.rpc.name} - ${method.description} */
/* ${method.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;
Expand All @@ -9,9 +9,9 @@
JsonObject jsonParameters;
${method.params.serialization.with.indent}
${method.result.json.type} jsonResult;
status = transport->Invoke("${info.title.lowercase}.${method.rpc.name}", jsonParameters, jsonResult);
status = transport->Invoke("${info.title.lowercase}.${method.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");
FIREBOLT_LOG_INFO(FireboltSDK::Logger::Category::OpenRPC, FireboltSDK::Logger::Module<FireboltSDK::Accessor>(), "${info.Title}.${method.name} is successfully invoked");
${if.result.nonvoid}${method.result.instantiation.with.indent}${end.if.result.nonvoid}
}

Expand Down
4 changes: 2 additions & 2 deletions languages/cpp/templates/methods/property.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* ${method.rpc.name} - ${method.description} */
/* ${method.name} - ${method.description} */
${method.signature.result} ${info.Title}Impl::${method.name}( ${method.signature.params}${if.params}, ${end.if.params}Firebolt::Error *err ) const
{
const string method = _T("${info.title.lowercase}.${method.rpc.name}");
const string method = _T("${info.title.lowercase}.${method.name}");
${if.params}${method.params.serialization}${end.if.params}
${method.result.json} jsonResult;
${method.result.initialization}
Expand Down
10 changes: 5 additions & 5 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,6 @@ return obj
}

const generateMacros = (obj, templates, languages, options = {}) => {
// for languages that don't support nested schemas, let's promote them to first-class schemas w/ titles
if (config.extractSubSchemas) {
obj = promoteAndNameSubSchemas(obj)
}
if (options.createPolymorphicMethods) {
let methods = []
obj.methods && obj.methods.forEach(method => {
Expand All @@ -522,6 +518,10 @@ const generateMacros = (obj, templates, languages, options = {}) => {
})
obj.methods = methods
}
// for languages that don't support nested schemas, let's promote them to first-class schemas w/ titles
if (config.extractSubSchemas) {
obj = promoteAndNameSubSchemas(obj)
}

// config.mergeAnyOfs = true
// if (config.mergeAnyOfs) {
Expand Down Expand Up @@ -1347,7 +1347,7 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {})
template = insertExampleMacros(template, examples[methodObj.name] || [], methodObj, json, templates)

template = template.replace(/\$\{method\.name\}/g, method.name)
.replace(/\$\{method\.rpc\.name\}/g, methodObj.title || methodObj.name)
.replace(/\$\{method\.rpc\.name\}/g, methodObj.rpc_name || methodObj.name)
.replace(/\$\{method\.summary\}/g, methodObj.summary)
.replace(/\$\{method\.description\}/g, methodObj.description
|| methodObj.summary)
Expand Down
8 changes: 4 additions & 4 deletions src/macrofier/types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ const insertObjectMacros = (content, schema, module, title, property, options) =
.replace(/\$\{description\}/g, prop.description || '')
.replace(/\$\{summary\}/g, prop.description ? prop.description.split('\n')[0] : '')
.replace(/\$\{delimiter\}(.*?)\$\{end.delimiter\}/gms, i === schema.properties.length - 1 ? '' : '$1')
.replace(/\$\{if\.optional\}(.*?)\$\{end\.if\.optional\}/gms, schema.required && schema.required.includes(name) ? '' : '$1')
.replace(/\$\{if\.non.optional\}(.*?)\$\{end\.if\.non.optional\}/gms, schema.required && schema.required.includes(name) ? '$1' : '')
.replace(/\$\{if\.optional\}(.*?)\$\{end\.if\.optional\}/gms, ((schema.required && schema.required.includes(name)) || (localizedProp.required && localizedProp.required === true)) ? '' : '$1')
.replace(/\$\{if\.non.optional\}(.*?)\$\{end\.if\.non.optional\}/gms, ((schema.required && schema.required.includes(name)) || (localizedProp.required && localizedProp.required === true)) ? '$1' : '')
.replace(/\$\{if\.base\.optional\}(.*?)\$\{end\.if\.base\.optional\}/gms, options.required ? '' : '$1')
.replace(/\$\{if\.non\.object\}(.*?)\$\{end\.if\.non\.object\}/gms, isObject(localizedProp) ? '' : '$1')
.replace(/\$\{if\.non\.array\}(.*?)\$\{end\.if\.non\.array\}/gms, (localizedProp.type === 'array') ? '' : '$1')
Expand All @@ -346,8 +346,8 @@ const insertObjectMacros = (content, schema, module, title, property, options) =
let baseTitle = options.property
if (isObject(localizedProp)) {
replacedTemplate = replacedTemplate
.replace(/\$\{if\.impl.optional\}(.*?)\$\{end\.if\.impl.optional\}/gms, schema.required && schema.required.includes(name) ? '' : '$1')
.replace(/\$\{if\.impl.non.optional\}(.*?)\$\{end\.if\.impl.non.optional\}/gms, schema.required && schema.required.includes(name) ? '$1' : '')
.replace(/\$\{if\.impl.optional\}(.*?)\$\{end\.if\.impl.optional\}/gms, ((schema.required && schema.required.includes(name)) || (localizedProp.required && localizedProp.required === true)) ? '' : '$1')
.replace(/\$\{if\.impl.non.optional\}(.*?)\$\{end\.if\.impl.non.optional\}/gms, ((schema.required && schema.required.includes(name)) || (localizedProp.required && localizedProp.required === true)) ? '$1' : '')
.replace(/\$\{property.dependency\}/g, ((options.level > 0) ? '${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}' : '') + objSeparator + name)
.replace(/\$\{Property.dependency\}/g, ((options.level > 0) ? '${Property.dependency}' : '') + (objSeparator) + capitalize(name))
}
Expand Down
4 changes: 1 addition & 3 deletions src/openrpc/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ const run = async ({
}




// add methods from this module
openrpc.methods.push(...json.methods)

Expand All @@ -113,4 +111,4 @@ const run = async ({
return Promise.resolve()
}

export default run
export default run
11 changes: 6 additions & 5 deletions src/shared/modules.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ const getAnyOfSchema = (inType, json) => {

const generateAnyOfSchema = (anyOf, name, summary) => {
let anyOfType = {}
anyOfType["name"] = name[0].toLowerCase() + name.substr(1)
anyOfType["name"] = name;
anyOfType["summary"] = summary
anyOfType["schema"] = anyOf
return anyOfType
Expand All @@ -896,7 +896,7 @@ const generateParamsAnyOfSchema = (methodParams, anyOf, anyOfTypes, title, summa
let params = []
methodParams.forEach(p => {
if (p.schema.anyOf === anyOfTypes) {
let anyOfType = generateAnyOfSchema(anyOf, title, summary)
let anyOfType = generateAnyOfSchema(anyOf, p.name, summary)
anyOfType.required = p.required
params.push(anyOfType)
}
Expand Down Expand Up @@ -937,6 +937,7 @@ const createPolymorphicMethods = (method, json) => {
let anyOfTypes
let methodParams = []
let methodResult = Object.assign({}, method.result)

method.params.forEach(p => {
if (p.schema) {
let param = getAnyOfSchema(p, json)
Expand Down Expand Up @@ -979,12 +980,12 @@ const createPolymorphicMethods = (method, json) => {
let localized = localizeDependencies(anyOf, json)
let title = localized.title || localized.name || ''
let summary = localized.summary || localized.description || ''
polymorphicMethodSchema.title = method.name
polymorphicMethodSchema.name = foundAnyOfParams ? `${method.name}With${title}` : `${method.name}${title}`
polymorphicMethodSchema.rpc_name = method.name
polymorphicMethodSchema.name = foundAnyOfResult && isEventMethod(method) ? `${method.name}${title}` : method.name
polymorphicMethodSchema.tags = method.tags
polymorphicMethodSchema.params = foundAnyOfParams ? generateParamsAnyOfSchema(methodParams, anyOf, anyOfTypes, title, summary) : methodParams
polymorphicMethodSchema.result = Object.assign({}, method.result)
polymorphicMethodSchema.result.schema = foundAnyOfResult ? generateResultAnyOfSchema(method, methodResult, anyOf, anyOfTypes, title, summary) : methodResult
polymorphicMethodSchema.result.schema = foundAnyOfResult ? generateResultAnyOfSchema(method, methodResult, anyOf, anyOfTypes, title, summary) : methodResult.schema
polymorphicMethodSchema.examples = method.examples
polymorphicMethodSchemas.push(Object.assign({}, polymorphicMethodSchema))
})
Expand Down

0 comments on commit c200483

Please sign in to comment.