Skip to content

Commit

Permalink
CPPSDK: polymorphic method schema creation changes
Browse files Browse the repository at this point in the history
  • Loading branch information
HaseenaSainul committed Dec 15, 2023
1 parent 8901760 commit 67e542e
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 19 deletions.
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.title}( ${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.title}( ${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/methods/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.title}(${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
2 changes: 1 addition & 1 deletion languages/cpp/templates/methods/polymorphic-pull-event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.title}(${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 Down
10 changes: 6 additions & 4 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 @@ -1218,6 +1218,7 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {})
}
const method = {
name: methodObj.name,
title: methodObj.title,
params: methodObj.params.map(p => p.name).join(', '),
transforms: null,
alternative: null,
Expand Down Expand Up @@ -1347,6 +1348,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\.title\}/g, method.title ? method.title : method.name)
.replace(/\$\{method\.rpc\.name\}/g, methodObj.title || methodObj.name)
.replace(/\$\{method\.summary\}/g, methodObj.summary)
.replace(/\$\{method\.description\}/g, methodObj.description
Expand Down
8 changes: 4 additions & 4 deletions src/macrofier/types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,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 @@ -316,8 +316,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
9 changes: 5 additions & 4 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 @@ -980,11 +981,11 @@ const createPolymorphicMethods = (method, 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.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 67e542e

Please sign in to comment.