Skip to content

Commit

Permalink
Default template implementation added
Browse files Browse the repository at this point in the history
  • Loading branch information
HaseenaSainul committed Jun 22, 2023
1 parent 229cae6 commit 1cacbd1
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 42 deletions.
18 changes: 10 additions & 8 deletions languages/c/Types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ function getSchemaType(schema, module, { name, prefix = '', destination, resultS
return info.type
}

function getSchemaTypeInfo(module = {}, json = {}, name = '', schemas = {}, prefix = '', options = {level: 0, descriptions: true, title: false, resultSchema: false, event: false}) {
function getSchemaTypeInfo(module = {}, json = {}, name = '', schemas = {}, prefix = '', options = { level: 0, descriptions: true, title: false, resultSchema: false, event: false}) {

if (json.schema) {
json = json.schema
Expand Down Expand Up @@ -314,7 +314,7 @@ function getSchemaTypeInfo(module = {}, json = {}, name = '', schemas = {}, pref
return getSchemaTypeInfo(module, union, '', schemas, '', options)
}
else if (json.oneOf) {
structure.type = 'char*'
structure.type = stringAsHandle ? getFireboltStringType() : 'char*'
structure.json.type = 'string'
return structure
}
Expand All @@ -331,7 +331,7 @@ function getSchemaTypeInfo(module = {}, json = {}, name = '', schemas = {}, pref
structure.namespace = (json.namespace ? json.namespace : getModuleName(module))
}
else {
structure.type = 'char*'
structure.type = stringAsHandle ? getFireboltStringType() : 'char*'
}
if (name) {
structure.name = capitalize(name)
Expand Down Expand Up @@ -371,13 +371,12 @@ function getSchemaShapeInfo(json, module, schemas = {}, { name = '', prefix = ''
if (json['$ref'][0] === '#') {
//Ref points to local schema
//Get Path to ref in this module and getSchemaType
const schema = getPath(json['$ref'], module, schemas)
const tname = schema.title || json['$ref'].split('/').pop()
let schema = getPath(json['$ref'], module, schemas)
const tName = schema.title || json['$ref'].split('/').pop()
if (json['$ref'].includes('x-schemas')) {
schema = (getRefModule(json['$ref'].split('/')[2]))
}

shape = getSchemaShapeInfo(schema, module, schemas, { name, prefix, merged, level, title, summary, descriptions, destination, section, enums })
shape = getSchemaShapeInfo(schema, module, schemas, { name: tName, prefix, merged, level, title, summary, descriptions, destination, section, enums })
}
}
//If the schema is a const,
Expand Down Expand Up @@ -533,7 +532,7 @@ function getSchemaShapeInfo(json, module, schemas = {}, { name = '', prefix = ''

if (info.type && info.type.length > 0) {
let type = getArrayElementSchema(json, module, schemas, info.name)
let arrayName = capitalize(name) + capitalize(type.type)
let arrayName = capitalize(info.name) + capitalize(type.type)
let objName = getTypeName(info.namespace, arrayName, prefix)
let tName = objName + 'Array'
let moduleName = info.namespace
Expand Down Expand Up @@ -706,6 +705,9 @@ function getJsonTypeInfo(module = {}, json = {}, name = '', schemas, prefix = ''
structure.type = getJsonNativeType(json)
return structure
}
else {
structure.type = 'JsonObject'
}
return structure
}

Expand Down
2 changes: 1 addition & 1 deletion languages/c/src/types/ImplHelpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ function getResultInstantiation (name, nativeType, container, indentLevel = 3) {
impl += `${' '.repeat(indentLevel)}*resultPtr = WPEFramework::Core::ProxyType<${container}>::Create();\n`
impl += `${' '.repeat(indentLevel)}*(*resultPtr) = jsonResult;\n`
impl += `${' '.repeat(indentLevel)}*${name} = static_cast<${nativeType}>(resultPtr);`
} else {
} else if (nativeType) {
impl += `${' '.repeat(indentLevel)}*${name} = jsonResult.Value();`
}

Expand Down
4 changes: 1 addition & 3 deletions languages/c/templates/codeblocks/setter.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
uint32_t ${info.title}_${method.Name}( ${method.signature.params} )
{
const string method = _T("${info.title}.${method.name}");
${if.params}
${method.params.serialization}
${end.if.params}
${if.params}${method.params.serialization}${end.if.params}
return FireboltSDK::Properties::Set(method, jsonParameters);
}
3 changes: 3 additions & 0 deletions languages/c/templates/declarations/default.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* ${method.name} - ${method.description}
${method.params.annotations}${if.deprecated} * @deprecated ${method.deprecation}${end.if.deprecated} */
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} );
33 changes: 15 additions & 18 deletions languages/c/templates/methods/default.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
/* ${method.name} - ${method.description} */
uint32_t ${info.title}_${method.Name}(${method.params.list}${if.params}, ${end.if.params}${method.result.type}* ${method.result.name}) {
uint32_t status = FireboltSDKErrorUnavailable;
FireboltSDK::Transport<WPEFramework::Core::JSON::IElement>* transport = FireboltSDK::Accessor::Instance().GetTransport();
if (transport != nullptr) {

JsonObject jsonParameters;
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} ) {

${if.params}
${method.params.json}
${end.if.params}
uint32_t status = FireboltSDKErrorUnavailable;
FireboltSDK::Transport<WPEFramework::Core::JSON::IElement>* transport = FireboltSDK::Accessor::Instance().GetTransport();
if (transport != nullptr) {

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

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

return status;
return status;
}
58 changes: 46 additions & 12 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,43 @@ const getMethods = compose(
getPath(['methods'])
)

const getSchemas = compose(
const getComponentSchemas = compose(
map(Object.entries), // Maybe Array<Array<key, value>>
chain(safe(isObject)), // Maybe Object
getPath(['components', 'schemas']) // Maybe any
)

const getSchemas = (schema) => {
let methods = getMethods(schema)
let components = getComponentSchemas(schema)
let allSchema = []

methods.map((schemas) => {
Object.entries(schemas).forEach( ([name, schema]) => {
if (schema.tags) {
schema.params.forEach(param => {
if (param.schema) {
let sch = Object.assign({}, param.schema)
sch.title = param.name
allSchema.push([param.name, sch])
}
})
if (schema.result.schema) {
let sch = Object.assign({}, schema.result.schema)
sch.title = schema.result.name
allSchema.push([schema.result.name, sch])
}
}
})
})
components.map((schemas) => {
Object.entries(schemas).forEach( ([name, schema]) => {
allSchema.push(schema)
})
})
return [allSchema]
}

// TODO: import from shared/modules.mjs
const isDeprecatedMethod = compose(
option(false),
Expand Down Expand Up @@ -592,7 +623,6 @@ const enumFinder = compose(
const generateEnums = (json, templates, options = { destination: '' }) => {
const suffix = options.destination.split('.').pop()
return compose(
option(''),
map(val => {
let template = getTemplate(`/sections/enum.${suffix}`, templates)
return template ? template.replace(/\$\{schema.list\}/g, val.trimEnd()) : val
Expand Down Expand Up @@ -725,15 +755,15 @@ function generateSchemas(json, templates, options) {
list.push([name, schema])
}
else if (schema.tags) {
if (!isDeprecatedMethod(schema)) {
schema.params.forEach(param => {
if (param.schema && (param.schema.type === 'object')) {
list.push([param.name, param.schema, '', { prefix : schema.name }])
}
})
if (schema.result.schema && (schema.result.schema.type === 'object')) {
list.push([schema.result.name, schema.result.schema, '', { prefix : schema.name }])
schema.params.forEach(param => {
if (param.schema && ((param.schema.type === 'object') || (param.schema.type === 'array'))) {
let found = !!list.find(entry => JSON.stringify(param.schema) === JSON.stringify(entry[1]))
found === false ? list.push([param.name, param.schema, '']) : ''
}
})
if (schema.result.schema && ((schema.result.schema.type === 'object') || (schema.result.schema.type === 'array'))) {
let found = !!list.find(entry => JSON.stringify(schema.result.schema) === JSON.stringify(entry[1]))
found === false ? list.push([schema.result.name, schema.result.schema, '']) : ''
}
}
})
Expand Down Expand Up @@ -1041,7 +1071,7 @@ function insertMethodMacros(template, methodObj, json, templates, examples={}) {
result.schema = JSON.parse(JSON.stringify(getPayloadFromEvent(methodObj)))
event.result.schema = getPayloadFromEvent(event)
event.params = event.params.filter(p => p.name !== 'listen')
}
}

const eventParams = event.params && event.params.length ? getTemplate('/sections/parameters', templates) + event.params.map(p => insertParameterMacros(getTemplate('/parameters/default', templates), p, event, json)).join('') : ''
const eventParamsRows = event.params && event.params.length ? event.params.map(p => insertParameterMacros(getTemplate('/parameters/default', templates), p, event, json)).join('') : ''
Expand Down Expand Up @@ -1102,9 +1132,12 @@ function insertMethodMacros(template, methodObj, json, templates, examples={}) {
.replace(/\$\{method\.params\.array\}/g, JSON.stringify(methodObj.params.map(p => p.name)))
.replace(/\$\{method\.params\.count}/g, methodObj.params ? methodObj.params.length : 0)
.replace(/\$\{if\.params\}(.*?)\$\{end\.if\.params\}/gms, method.params.length ? '$1' : '')
.replace(/\$\{if\.params.empty\}(.*?)\$\{end\.if\.params.empty\}/gms, method.params.length === 0 ? '$1' : '')
.replace(/\$\{if\.result\}(.*?)\$\{end\.if\.result\}/gms, resultType ? '$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' : '')
.replace(/\$\{if\.context\}(.*?)\$\{end\.if\.context\}/gms, event && event.params.length ? '$1' : '')
.replace(/\$\{method\.params\.serialization\}/g, serializedParams)
.replace(/\$\{method\.params\.serialization\.with\.indent\}/g, indent(serializedParams, ' '))
// Typed signature stuff
.replace(/\$\{method\.signature\}/g, types.getMethodSignature(methodObj, json, { isInterface: false, destination: state.destination, section: state.section }))
.replace(/\$\{method\.signature\.params\}/g, types.getMethodSignatureParams(methodObj, json, { destination: state.destination, section: state.section }))
Expand Down Expand Up @@ -1142,6 +1175,7 @@ function insertMethodMacros(template, methodObj, json, templates, examples={}) {
.replace(/\$\{event\.result\.type\}/, isEventMethod(methodObj) ? types.getSchemaType(result.schema, json, { name: result.name, destination: state.destination, event: true, description: methodObj.result.summary, asPath: false }): '')
.replace(/\$\{event\.result\.json\.type\}/g, resultJsonType)
.replace(/\$\{method\.result\}/g, generateResult(result.schema, json, templates, { name : result.name }))
.replace(/\$\{method\.result\.json\.type\}/g, resultJsonType)
.replace(/\$\{method\.result\.instantiation\}/g, resultInst)
.replace(/\$\{method\.example\.value\}/g, JSON.stringify(methodObj.examples[0].result.value))
.replace(/\$\{method\.alternative\}/g, method.alternative)
Expand Down

0 comments on commit 1cacbd1

Please sign in to comment.