From 0e742f2f6e90fac9674bcfd5b32c360118d9f159 Mon Sep 17 00:00:00 2001 From: HaseenaSainul <41037131+HaseenaSainul@users.noreply.github.com> Date: Thu, 15 Jun 2023 09:09:06 +0530 Subject: [PATCH] Add support to generate file inclusion for common schema also (#97) Add support to generate file inclusion for common schema also --- languages/c/Types.mjs | 336 +++++++++--------- languages/c/src/types/JSONHelpers.mjs | 3 +- languages/c/src/types/NativeHelpers.mjs | 15 - languages/c/templates/imports/default.cpp | 1 + .../imports/{default.c => default.h} | 0 .../c/templates/imports/default.jsondata | 1 + .../templates/modules/include/Common/Module.h | 40 --- .../c/templates/modules/src/JsonData_Module.h | 29 -- languages/c/templates/modules/src/Module.cpp | 6 +- .../templates/schemas/include/Common/Module.h | 1 + .../c/templates/schemas/src/JsonData_Module.h | 5 + .../c/templates/schemas/src/Module_Common.cpp | 5 +- src/macrofier/engine.mjs | 50 ++- src/shared/typescript.mjs | 2 +- 14 files changed, 226 insertions(+), 268 deletions(-) create mode 100644 languages/c/templates/imports/default.cpp rename languages/c/templates/imports/{default.c => default.h} (100%) create mode 100644 languages/c/templates/imports/default.jsondata delete mode 100644 languages/c/templates/modules/include/Common/Module.h delete mode 100644 languages/c/templates/modules/src/JsonData_Module.h diff --git a/languages/c/Types.mjs b/languages/c/Types.mjs index 0bb6cc59..bbd74eec 100644 --- a/languages/c/Types.mjs +++ b/languages/c/Types.mjs @@ -318,193 +318,201 @@ function getSchemaShape(json, module, { name = '', prefix = '', level = 0, title return shape } function getSchemaShapeInfo(json, module, schemas = {}, { name = '', prefix = '', merged = false, level = 0, title, summary, descriptions = true, destination = '', section = '', enums = true } = {}) { - const isHeader = (destination.includes("JsonData_") !== true) && destination.endsWith(".h") - const isCPP = ((destination.endsWith(".cpp") || destination.includes("JsonData_")) && (section.includes('accessors') !== true)) - json = JSON.parse(JSON.stringify(json)) - - name = json.title || name let shape = '' - if (json['$ref']) { - 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() - 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 }) - } - } - //If the schema is a const, - else if (json.hasOwnProperty('const') && !isCPP) { - if (level > 0) { + if (destination && section) { + const isHeader = (destination.includes("JsonData_") !== true) && destination.endsWith(".h") + const isCPP = ((destination.endsWith(".cpp") || destination.includes("JsonData_")) && (section.includes('accessors') !== true)) + json = JSON.parse(JSON.stringify(json)) + + name = json.title || name + + if (json['$ref']) { + 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() + if (json['$ref'].includes('x-schemas')) { + schema = (getRefModule(json['$ref'].split('/')[2])) + } - let t = description(capitalize(name), json.description) - typeName = getTypeName(getModuleName(module), name, prefix) - t += (isHeader ? getPropertyAccessors(typeName, capitalize(name), typeof schema.const, { level: level, readonly: true, optional: false }) : getPropertyAccessorsImpl(typeName, getJsonType(schema, module, { level, name }), typeof schema.const, { level: level, readonly: true, optional: false })) - shape += '\n' + t + shape = getSchemaShapeInfo(schema, module, schemas, { name, prefix, merged, level, title, summary, descriptions, destination, section, enums }) + } } - } - else if (json.type === 'object') { - if (!name) { - console.log(`WARNING: unnamed schema in ${module.info.title}.`) - console.dir(json) - shape = '' + //If the schema is a const, + else if (json.hasOwnProperty('const') && !isCPP) { + if (level > 0) { + + let t = description(capitalize(name), json.description) + typeName = getTypeName(getModuleName(module), name, prefix) + t += (isHeader ? getPropertyAccessors(typeName, capitalize(name), typeof schema.const, { level: level, readonly: true, optional: false }) : getPropertyAccessorsImpl(typeName, getJsonType(schema, module, { level, name }), typeof schema.const, { level: level, readonly: true, optional: false })) + shape += '\n' + t + } } - else if (json.properties && (validJsonObjectProperties(json) === true)) { - let c_shape = description(capitalize(name), json.description) - let cpp_shape = '' - let tName = getTypeName(getModuleName(module), name, prefix) - c_shape += '\n' + (isHeader ? getObjectHandleManagement(tName) : getObjectHandleManagementImpl(tName, getJsonType(json, module, { name }))) - let props = [] - let containerName = ((prefix.length > 0) && (!name.startsWith(prefix))) ? (prefix + '_' + capitalize(name)) : capitalize(name) - Object.entries(json.properties).forEach(([pname, prop]) => { - let items - var desc = '\n' + description(capitalize(pname), prop.description) - if (prop.type === 'array') { - if (Array.isArray(prop.items)) { - //TODO - const IsHomogenous = arr => new Set(arr.map( item => item.type ? item.type : typeof item)).size === 1 - if (!IsHomogenous(prop.items)) { - throw 'Heterogenous Arrays not supported yet' + else if (json.type === 'object') { + if (!name) { + console.log(`WARNING: unnamed schema in ${module.info.title}.`) + console.dir(json) + shape = '' + } + else if (json.properties && (validJsonObjectProperties(json) === true)) { + let c_shape = description(capitalize(name), json.description) + let cpp_shape = '' + let tName = getTypeName(getModuleName(module), name, prefix) + c_shape += '\n' + (isHeader ? getObjectHandleManagement(tName) : getObjectHandleManagementImpl(tName, getJsonType(json, module, { name }))) + let props = [] + let containerName = ((prefix.length > 0) && (!name.startsWith(prefix))) ? (prefix + '_' + capitalize(name)) : capitalize(name) + Object.entries(json.properties).forEach(([pname, prop]) => { + let items + var desc = '\n' + description(capitalize(pname), prop.description) + if (prop.type === 'array') { + if (Array.isArray(prop.items)) { + //TODO + const IsHomogenous = arr => new Set(arr.map( item => item.type ? item.type : typeof item)).size === 1 + if (!IsHomogenous(prop.items)) { + throw 'Heterogenous Arrays not supported yet' + } + items = prop.items[0] + } + else { + // grab the type for the non-array schema + items = prop.items + } + + let info = getSchemaTypeInfo(module, items, items.name || pname, schemas, prefix, {level : level, descriptions: descriptions, title: true}) + if (info.type && info.type.length > 0) { + let objName = tName + '_' + capitalize(prop.title || pname) + let moduleName = info.namespace + info.json.namespace = info.namespace + let moduleProperty = getJsonTypeInfo(module, json, json.title || name, schemas, prefix) + let prefixName = ((prefix.length > 0) && items['$ref']) ? '' : prefix + let subModuleProperty = getJsonTypeInfo(module, info.json, info.name, schemas, prefix) + + let t = description(capitalize(info.name), json.description) + '\n' + t += '\n' + (isHeader ? getArrayAccessors(objName, tName, info.type) : getArrayAccessorsImpl(tName, moduleProperty.type, (tName + 'Handle'), subModuleProperty.type, capitalize(pname || prop.title), info.type, info.json)) + c_shape += '\n' + t + props.push({name: `${pname}`, type: `WPEFramework::Core::JSON::ArrayType<${subModuleProperty.type}>`}) + } + else { + console.log(`a. WARNING: Type undetermined for ${name}:${pname}`) + } + } else { + if (((merged === false) || ((merged === true) && (pname.includes(name)))) && (prop.type === 'object' || prop.anyOf || prop.allOf)) { + shape += getSchemaShapeInfo(prop, module, schemas, { name : pname, prefix, merged: false, level: 1, title, summary, descriptions, destination, section, enums }) + } + let info = getSchemaTypeInfo(module, prop, pname, module['x-schemas'], prefix, {descriptions: descriptions, level: level + 1, title: true}) + if (info.type && info.type.length > 0) { + let subPropertyName = ((pname.length !== 0) ? capitalize(pname) : info.name) + let moduleProperty = getJsonTypeInfo(module, json, name, schemas, prefix) + let subProperty = getJsonTypeInfo(module, prop, pname, schemas, prefix) + c_shape += '\n' + description(capitalize(pname), info.json.description) + c_shape += '\n' + (isHeader ? getPropertyAccessors(tName, capitalize(pname), info.type, { level: 0, readonly: false, optional: isOptional(pname, json) }) : getPropertyAccessorsImpl(tName, moduleProperty.type, subProperty.type, subPropertyName, info.type, info.json, {readonly:false, optional:isOptional(pname, json)})) + let property = getJsonType(prop, module, { name : pname, prefix }) + props.push({name: `${pname}`, type: `${property}`}) + } + else { + console.log(`b. WARNING: Type undetermined for ${name}:${pname}`) } - items = prop.items[0] - } - else { - // grab the type for the non-array schema - items = prop.items - } - let info = getSchemaTypeInfo(module, items, items.name || pname, schemas, prefix, {level : level, descriptions: descriptions, title: true}) - if (info.type && info.type.length > 0) { - let objName = tName + '_' + capitalize(prop.title || pname) - let moduleName = info.namespace - info.json.namespace = info.namespace - let moduleProperty = getJsonTypeInfo(module, json, json.title || name, schemas, prefix) - let prefixName = ((prefix.length > 0) && items['$ref']) ? '' : prefix - let subModuleProperty = getJsonTypeInfo(module, info.json, info.name, schemas, prefix) - - let t = description(capitalize(info.name), json.description) + '\n' - t += '\n' + (isHeader ? getArrayAccessors(objName, tName, info.type) : getArrayAccessorsImpl(tName, moduleProperty.type, (tName + 'Handle'), subModuleProperty.type, capitalize(pname || prop.title), info.type, info.json)) - c_shape += '\n' + t - props.push({name: `${pname}`, type: `WPEFramework::Core::JSON::ArrayType<${subModuleProperty.type}>`}) - } - else { - console.log(`a. WARNING: Type undetermined for ${name}:${pname}`) - } - } else { - if (((merged === false) || ((merged === true) && (pname.includes(name)))) && (prop.type === 'object' || prop.anyOf || prop.allOf)) { - shape += getSchemaShapeInfo(prop, module, schemas, { name : pname, prefix, merged: false, level: 1, title, summary, descriptions, destination, section, enums }) - } - let info = getSchemaTypeInfo(module, prop, pname, module['x-schemas'], prefix, {descriptions: descriptions, level: level + 1, title: true}) - if (info.type && info.type.length > 0) { - let subPropertyName = ((pname.length !== 0) ? capitalize(pname) : info.name) - let moduleProperty = getJsonTypeInfo(module, json, name, schemas, prefix) - let subProperty = getJsonTypeInfo(module, prop, pname, schemas, prefix) - c_shape += '\n' + description(capitalize(pname), info.json.description) - c_shape += '\n' + (isHeader ? getPropertyAccessors(tName, capitalize(pname), info.type, { level: 0, readonly: false, optional: isOptional(pname, json) }) : getPropertyAccessorsImpl(tName, moduleProperty.type, subProperty.type, subPropertyName, info.type, info.json, {readonly:false, optional:isOptional(pname, json)})) - let property = getJsonType(prop, module, { name : pname, prefix }) - props.push({name: `${pname}`, type: `${property}`}) - } - else { - console.log(`b. WARNING: Type undetermined for ${name}:${pname}`) } - } - }) + }) - cpp_shape += getJsonContainerDefinition(json, containerName, props) + cpp_shape += getJsonContainerDefinition(json, containerName, props) - if (isCPP) { - shape += '\n' + cpp_shape - } - else { - shape += '\n' + c_shape + if (isCPP) { + shape += '\n' + cpp_shape + } + else { + shape += '\n' + c_shape + } } - } - else if (json.propertyNames && json.propertyNames.enum) { - //propertyNames in object not handled yet - } - else if (json.additionalProperties && (typeof json.additionalProperties === 'object') && (validJsonObjectProperties(json) === true) && !isCPP) { - let info = getSchemaTypeInfo(module, json.additionalProperties, name, module['x-schemas'], prefix) - if (!info.type || (info.type.length === 0)) { - info.type = 'char*' - info.json = json.additionalProperties - info.json.type = 'string' + else if (json.propertyNames && json.propertyNames.enum) { + //propertyNames in object not handled yet } + else if (json.additionalProperties && (typeof json.additionalProperties === 'object') && (validJsonObjectProperties(json) === true) && !isCPP) { + let info = getSchemaTypeInfo(module, json.additionalProperties, name, module['x-schemas'], prefix) + if (!info.type || (info.type.length === 0)) { + info.type = 'char*' + info.json = json.additionalProperties + info.json.type = 'string' + } - let tName = getTypeName(getModuleName(module), name, prefix) - let t = description(capitalize(name), json.description) + '\n' - let containerType = 'WPEFramework::Core::JSON::VariantContainer' + let tName = getTypeName(getModuleName(module), name, prefix) + let t = description(capitalize(name), json.description) + '\n' + let containerType = 'WPEFramework::Core::JSON::VariantContainer' - let subModuleProperty = getJsonTypeInfo(module, info.json, info.name, module['x-schemas']) - if (isCPP && ((info.json.type === 'object' && info.json.properties) || info.json.type === 'array')) { - // Handle Container generation here - } + let subModuleProperty = getJsonTypeInfo(module, info.json, info.name, module['x-schemas']) + if (isCPP && ((info.json.type === 'object' && info.json.properties) || info.json.type === 'array')) { + // Handle Container generation here + } - t += '\n' + (isHeader ? getObjectHandleManagement(tName) : getObjectHandleManagementImpl(tName, containerType)) - t += (isHeader ? getMapAccessors(tName, info.type, { descriptions: descriptions, level: level }) : getMapAccessorsImpl(tName, containerType, subModuleProperty.type, info.type, info.json, { readonly: true, optional: false })) - shape += '\n' + t + t += '\n' + (isHeader ? getObjectHandleManagement(tName) : getObjectHandleManagementImpl(tName, containerType)) + t += (isHeader ? getMapAccessors(tName, info.type, { descriptions: descriptions, level: level }) : getMapAccessorsImpl(tName, containerType, subModuleProperty.type, info.type, info.json, { readonly: true, optional: false })) + shape += '\n' + t + } + else if (json.patternProperties) { + console.log(`WARNING: patternProperties are not supported by Firebolt(inside getModuleName(module):${name})`) + } } - else if (json.patternProperties) { - console.log(`WARNING: patternProperties are not supported by Firebolt(inside getModuleName(module):${name})`) + else if (json.anyOf) { + if (level > 0) { + let mergedSchema = getMergedSchema(module, json, name, schemas) + let prefixName = ((prefix.length > 0) && (!name.startsWith(prefix))) ? prefix : capitalize(name) + shape += getSchemaShapeInfo(mergedSchema, module, schemas, { name, prefix: prefixName, merged, level, title, summary, descriptions, destination, section, enums }) + } } - } - else if (json.anyOf) { - if (level > 0) { - let mergedSchema = getMergedSchema(module, json, name, schemas) - let prefixName = ((prefix.length > 0) && (!name.startsWith(prefix))) ? prefix : capitalize(name) - shape += getSchemaShapeInfo(mergedSchema, module, schemas, { name, prefix: prefixName, merged, level, title, summary, descriptions, destination, section, enums }) + else if (json.oneOf) { + //Just ignore schema shape, since this has to be treated as string } - } - else if (json.oneOf) { - //Just ignore schema shape, since this has to be treated as string - } - else if (json.allOf) { - let title = (json.title ? json.title : name) - let union = deepMergeAll(module, title, json, schemas) - union.title = title + else if (json.allOf) { + let title = (json.title ? json.title : name) + let union = deepMergeAll(module, title, json, schemas) + union.title = title - delete union['$ref'] + delete union['$ref'] - return getSchemaShapeInfo(union, module, schemas, { name, prefix, merged: true, level, title, summary, descriptions, destination, section, enums }) - } - else if (json.type === 'array' && !isCPP) { - let j - if (Array.isArray(json.items)) { - //TODO - const IsHomogenous = arr => new Set(arr.map( item => item.type ? item.type : typeof item)).size === 1 - if (!IsHomogenous(json.items)) { - throw 'Heterogenous Arrays not supported yet' + return getSchemaShapeInfo(union, module, schemas, { name, prefix, merged: true, level, title, summary, descriptions, destination, section, enums }) + } + else if (json.type === 'array') { + let j + if (Array.isArray(json.items)) { + //TODO + const IsHomogenous = arr => new Set(arr.map( item => item.type ? item.type : typeof item)).size === 1 + if (!IsHomogenous(json.items)) { + throw 'Heterogenous Arrays not supported yet' + } + j = json.items[0] + } + else { + j = json.items + } + shape += getSchemaShapeInfo(j, module, schemas, { name: j.title || name, prefix, merged, level, title, summary, descriptions, destination, section, enums }) + + if (!isCPP) { + let info = getSchemaTypeInfo(module, j, j.title || name, schemas, prefix, {level : level, descriptions: descriptions, title: true}) + + if (info.type && info.type.length > 0) { + let type = getArrayElementSchema(json, module, schemas, info.name) + let arrayName = capitalize(name) + capitalize(type.type) + let objName = getTypeName(info.namespace, arrayName, prefix) + let tName = objName + 'Array' + let moduleName = info.namespace + info.json.namespace = info.namespace + let moduleProperty = getJsonTypeInfo(module, json, json.title || name, schemas, prefix) + let subModuleProperty = getJsonTypeInfo(module, j, j.title, schemas, prefix) + let t = '' + if (level === 0) { + t += description(capitalize(info.name), json.description) + '\n' + t += '\n' + (isHeader ? getObjectHandleManagement(tName) : getObjectHandleManagementImpl(tName, moduleProperty.type)) + } + t += '\n' + (isHeader ? getArrayAccessors(objName, tName, info.type) : getArrayAccessorsImpl(objName, moduleProperty.type, (tName + 'Handle'), subModuleProperty.type, '', info.type, info.json)) + shape += '\n' + t + } } - j = json.items[0] } else { - j = json.items + shape += '\n' + getSchemaType(module, json, name, schemas, prefix, {level: level, descriptions: descriptions}) } - - let info = getSchemaTypeInfo(module, j, j.name || name, schemas, prefix, {level : level, descriptions: descriptions, title: true}) - - if (info.type && info.type.length > 0) { - let type = getArrayElementSchema(json, module, schemas, info.name) - let arrayName = capitalize(info.name) + capitalize(type.type) - let objName = getTypeName(info.namespace, arrayName, prefix) - let tName = objName + 'Array' - let moduleName = info.namespace - info.json.namespace = info.namespace - let moduleProperty = getJsonTypeInfo(module, json, json.title || name, schemas, prefix) - let t = '' - if (level === 0) { - t += description(capitalize(info.name), json.description) + '\n' - t += '\n' + (isHeader ? getObjectHandleManagement(tName) : getObjectHandleManagementImpl(tName, moduleProperty.type)) - } - t += '\n' + (isHeader ? getArrayAccessors(objName, tName, info.type) : getArrayAccessorsImpl(objName, moduleProperty.type, (tName + 'Handle'), getJsonNativeType(type), '', info.type, info.json)) - shape += '\n' + t - } - } - else { - shape += '\n' + getSchemaType(module, json, name, schemas, prefix, {level: level, descriptions: descriptions}) } return shape diff --git a/languages/c/src/types/JSONHelpers.mjs b/languages/c/src/types/JSONHelpers.mjs index 3437c98c..b2a260b5 100644 --- a/languages/c/src/types/JSONHelpers.mjs +++ b/languages/c/src/types/JSONHelpers.mjs @@ -51,5 +51,6 @@ function getJsonContainerDefinition (schema, name, props) { } export { - getJsonContainerDefinition + getJsonContainerDefinition, + getJsonDataStructName } diff --git a/languages/c/src/types/NativeHelpers.mjs b/languages/c/src/types/NativeHelpers.mjs index eac20c16..489b3f52 100644 --- a/languages/c/src/types/NativeHelpers.mjs +++ b/languages/c/src/types/NativeHelpers.mjs @@ -211,20 +211,6 @@ const generateEnum = (schema, prefix)=> { } } -const getIncludeDefinitions = (json = {}, jsonData = false) => { - return getExternalSchemaPaths(json) - .map(ref => { - const mod = ref.split('#')[0].split('/').pop() - let i = `#include "Common/${capitalize(mod)}.h"` - if(jsonData === true) { - i += '\n' + `#include "JsonData_${capitalize(mod)}.h"` - } - return i - }) - .filter((item, index, arr) => arr.indexOf(item) === index) - .concat([`#include "Firebolt/Types.h"`]) -} - function getPropertyGetterSignature(method, module, paramType) { let m = `${capitalize(getModuleName(module))}_Get${capitalize(method.name)}` return `${description(method.name, method.summary)}\nuint32 ${m}( ${paramType === 'char*' ? 'FireboltTypes_StringHandle' : paramType}* ${method.result.name || method.name} )` @@ -251,7 +237,6 @@ export { getIncludeGuardClose, getNativeType, getModuleName, - getIncludeDefinitions, getPropertyGetterSignature, getPropertySetterSignature, getPropertyEventCallbackSignature, diff --git a/languages/c/templates/imports/default.cpp b/languages/c/templates/imports/default.cpp new file mode 100644 index 00000000..69e241f9 --- /dev/null +++ b/languages/c/templates/imports/default.cpp @@ -0,0 +1 @@ +#include "JsonData_${info.title}.h" diff --git a/languages/c/templates/imports/default.c b/languages/c/templates/imports/default.h similarity index 100% rename from languages/c/templates/imports/default.c rename to languages/c/templates/imports/default.h diff --git a/languages/c/templates/imports/default.jsondata b/languages/c/templates/imports/default.jsondata new file mode 100644 index 00000000..69e241f9 --- /dev/null +++ b/languages/c/templates/imports/default.jsondata @@ -0,0 +1 @@ +#include "JsonData_${info.title}.h" diff --git a/languages/c/templates/modules/include/Common/Module.h b/languages/c/templates/modules/include/Common/Module.h deleted file mode 100644 index 809418a2..00000000 --- a/languages/c/templates/modules/include/Common/Module.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2023 Comcast Cable Communications Management, LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#ifndef _COMMON_${info.TITLE}_H -#define _COMMON_${info.TITLE}_H - -#include "Firebolt.h" - -#ifdef __cplusplus -extern "C" { -#endif - -// Enums - -/* ${ENUMS} */ - -// Schemas - -/* ${SCHEMAS} */ - -#ifdef __cplusplus -} -#endif - -#endif // Header Include Guard diff --git a/languages/c/templates/modules/src/JsonData_Module.h b/languages/c/templates/modules/src/JsonData_Module.h deleted file mode 100644 index 2c7ca8c8..00000000 --- a/languages/c/templates/modules/src/JsonData_Module.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2023 Comcast Cable Communications Management, LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#pragma once - -#include "Firebolt.h" - -namespace FireboltSDK { - namespace ${info.title} { - - /* ${SCHEMAS}} */ - - } -} diff --git a/languages/c/templates/modules/src/Module.cpp b/languages/c/templates/modules/src/Module.cpp index 47a356d6..d34c1da5 100644 --- a/languages/c/templates/modules/src/Module.cpp +++ b/languages/c/templates/modules/src/Module.cpp @@ -16,11 +16,9 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "Firebolt.h" -#include "${info.title}.h" -#include "JsonData_${info.title}.h" - +#include "FireboltSDK.h" /* ${IMPORTS} */ +#include "${info.title}.h" namespace FireboltSDK { namespace ${info.title} { diff --git a/languages/c/templates/schemas/include/Common/Module.h b/languages/c/templates/schemas/include/Common/Module.h index 0326c220..ff037fba 100644 --- a/languages/c/templates/schemas/include/Common/Module.h +++ b/languages/c/templates/schemas/include/Common/Module.h @@ -20,6 +20,7 @@ #define _COMMON_${info.TITLE}_H #include "Firebolt.h" +/* ${IMPORTS} */ #ifdef __cplusplus extern "C" { diff --git a/languages/c/templates/schemas/src/JsonData_Module.h b/languages/c/templates/schemas/src/JsonData_Module.h index 7fa2b6f4..d0ecbbac 100644 --- a/languages/c/templates/schemas/src/JsonData_Module.h +++ b/languages/c/templates/schemas/src/JsonData_Module.h @@ -16,6 +16,11 @@ * SPDX-License-Identifier: Apache-2.0 */ +#pragma once + +/* ${IMPORTS} */ +#include "Common/${info.title}.h" + namespace FireboltSDK { namespace ${info.title} { // Types diff --git a/languages/c/templates/schemas/src/Module_Common.cpp b/languages/c/templates/schemas/src/Module_Common.cpp index 9dc2ee0a..e2389d50 100644 --- a/languages/c/templates/schemas/src/Module_Common.cpp +++ b/languages/c/templates/schemas/src/Module_Common.cpp @@ -16,9 +16,8 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "Firebolt.h" -#include "${info.title}.h" -#include "Common/${info.title}.h" +#include "FireboltSDK.h" +/* ${IMPORTS} */ #include "JsonData_${info.title}.h" /* ${ENUMS} */ diff --git a/src/macrofier/engine.mjs b/src/macrofier/engine.mjs index 37cf09d5..03d6cf70 100644 --- a/src/macrofier/engine.mjs +++ b/src/macrofier/engine.mjs @@ -46,6 +46,7 @@ const _inspector = obj => { // getMethodSignatureParams(method, module, options = { destination: 'file.txt' }) // getSchemaType(schema, module, options = { destination: 'file.txt', title: true }) // getSchemaShape(schema, module, options = { name: 'Foo', destination: 'file.txt' }) +// getJsonType(schema, module, options = { name: 'Foo', prefix: '', descriptions: false, level: 0 }) let types = { getMethodSignature: ()=>null, @@ -133,6 +134,24 @@ const getLinkForSchema = (schema, json, { name = '' } = {}) => { return '#' } +const getComponentExternalSchema = (json) => { + let refSchemas = [] + if (json.components && json.components.schemas) { + Object.entries(json.components.schemas).forEach(([name, schema]) => { + let refs = getLinkedSchemaPaths(schema).map(path => getPathOr(null, path, schema)) + refs.map(ref => { + let title = '' + if (ref.includes('x-schemas')) { + if (ref.split('/')[2] !== json.info.title) { + title = ref.split('/')[2] + } + } + title && !refSchemas.includes(title) ? refSchemas.push(title) : null + }) + }) + } + return (refSchemas) +} // Maybe methods array of objects const getMethods = compose( @@ -319,7 +338,7 @@ const generateMacros = (obj, templates, languages, options = {}) => { // grab the options so we don't have to pass them from method to method Object.assign(state, options) - const imports = generateImports(obj, templates) + const imports = generateImports(obj, templates, { destination : (options.destination ? options.destination : '') }) const initialization = generateInitialization(obj, templates) const enums = generateEnums(obj, templates, { destination : (options.destination ? options.destination : '') }) const eventsEnum = generateEvents(obj, templates) @@ -345,7 +364,7 @@ const generateMacros = (obj, templates, languages, options = {}) => { const schemasArray = generateSchemas(obj, templates, { baseUrl: '', section: 'schemas' }).filter(s => (options.copySchemasIntoModules || !s.uri)) const accessorsArray = generateSchemas(obj, templates, { baseUrl: '', section: 'accessors' }).filter(s => (options.copySchemasIntoModules || !s.uri)) const schemas = schemasArray.length ? getTemplate('/sections/schemas', templates).replace(/\$\{schema.list\}/g, schemasArray.map(s => s.body).filter(body => body).join('\n')) : '' - const typesArray = schemasArray.filter(x => !x.enum) + const typesArray = schemasArray.length ? schemasArray.filter(x => !x.enum) : [] const types = (typesArray.length ? getTemplate('/sections/types', templates).replace(/\$\{schema.list\}/g, typesArray.map(s => s.body).filter(body => body).join('\n')) : '') + methodTypes const accessors = (accessorsArray.length ? getTemplate('/sections/accessors', templates).replace(/\$\{schema.list\}/g, accessorsArray.map(s => s.body).filter(body => body).join('\n')) : '') + methodAccessors @@ -634,7 +653,6 @@ function generateSchemas(json, templates, options) { if (['ListenResponse', 'ProviderRequest', 'ProviderResponse', 'FederatedResponse', 'FederatedRequest'].includes(name)) { return } - let content = getTemplate('/schemas/default', templates) if (!schema.examples || schema.examples.length === 0) { @@ -697,11 +715,11 @@ function generateSchemas(json, templates, options) { if (!isDeprecatedMethod(schema)) { schema.params.forEach(param => { if (param.schema && (param.schema.type === 'object')) { - list.push([param.name, param.schema, '', { prefix : schema.name}]) + 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}]) + list.push([schema.result.name, schema.result.schema, '', { prefix : schema.name }]) } } } @@ -743,8 +761,8 @@ function getRelatedSchemaLinks(schema = {}, json = {}, templates = {}, options = return links } -const generateImports = (json, templates) => { - let imports = getTemplate('/imports/default', templates) +const generateImports = (json, templates, options = { destination: '' }) => { + let imports = '' if (rpcMethodsOrEmptyArray(json).length) { imports += getTemplate('/imports/rpc', templates) @@ -773,11 +791,22 @@ const generateImports = (json, templates) => { if (methodsWithXMethodsInResult(json).length) { imports += getTemplate('/imports/x-method', templates) } + const suffix = options.destination.split('.').pop() + const prefix = options.destination.split('/').pop().split('_')[0].toLowerCase() + + let template = prefix ? getTemplate(`/imports/default.${prefix}`, templates) : '' + if (!template) { + template = getTemplate(suffix ? `/imports/default.${suffix}` : '/imports/default', templates) + } - if (json['x-schemas'] && Object.keys(json['x-schemas']).length > 0) { - imports += Object.keys(json['x-schemas']).map(shared => getTemplate('/imports/default', templates).replace(/\$\{info.title\}/g, shared)).join('\n') + 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\}/g, shared)).join('') } + let componentExternalSchema = getComponentExternalSchema(json) + if (componentExternalSchema.length && json.info['x-uri-titles']) { + imports += componentExternalSchema.map(shared => template.replace(/\$\{info.title\}/g, shared)).join('') + } return imports } @@ -1021,9 +1050,8 @@ function insertMethodMacros(template, methodObj, json, templates, examples={}) { const pullsResultType = pullsResult && types.getSchemaShape(pullsResult, json, { destination: state.destination, section: state.section }) const pullsForType = pullsResult && types.getSchemaType(pullsResult, json, { destination: state.destination, section: state.section }) const pullsParamsType = pullsParams ? types.getSchemaShape(pullsParams, json, { destination: state.destination, section: state.section }) : '' - + let seeAlso = '' - if (isPolymorphicPullMethod(methodObj) && pullsForType) { seeAlso = `See also: [${pullsForType}](#${pullsForType.toLowerCase()}-1)` // this assumes the schema will be after the method... } diff --git a/src/shared/typescript.mjs b/src/shared/typescript.mjs index e53747cd..79c3e022 100644 --- a/src/shared/typescript.mjs +++ b/src/shared/typescript.mjs @@ -327,7 +327,7 @@ function getSchemaShape(schema = {}, module = {}, { name = '', level = 0, title, function getJsonType(schema, module, { destination, link = false, title = false, code = false, asPath = false, event = false, expandEnums = true, baseUrl = '' } = {}) { return '' } - + function getTypeScriptType(jsonType) { if (jsonType === 'integer') { return 'number'