Skip to content

Commit

Permalink
Add support to generate file inclusion for common schema also
Browse files Browse the repository at this point in the history
  • Loading branch information
HaseenaSainul committed Jun 6, 2023
1 parent aa3ec7b commit 60f9762
Show file tree
Hide file tree
Showing 11 changed files with 213 additions and 196 deletions.
336 changes: 171 additions & 165 deletions languages/c/Types.mjs

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion languages/c/src/types/JSONHelpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ function getJsonContainerDefinition (schema, name, props) {
}

export {
getJsonContainerDefinition
getJsonContainerDefinition,
getJsonDataStructName
}
15 changes: 0 additions & 15 deletions languages/c/src/types/NativeHelpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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} )`
Expand All @@ -251,7 +237,6 @@ export {
getIncludeGuardClose,
getNativeType,
getModuleName,
getIncludeDefinitions,
getPropertyGetterSignature,
getPropertySetterSignature,
getPropertyEventCallbackSignature,
Expand Down
1 change: 1 addition & 0 deletions languages/c/templates/imports/common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "JsonData_${info.title}.h"
2 changes: 1 addition & 1 deletion languages/c/templates/imports/default.c
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#include "Common/${info.title}.h"
#include "JsonData_${info.title}.h"
6 changes: 2 additions & 4 deletions languages/c/templates/modules/src/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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} {
Expand Down
1 change: 1 addition & 0 deletions languages/c/templates/schemas/include/Common/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define _COMMON_${info.TITLE}_H

#include "Firebolt.h"
/* ${IMPORTS} */

#ifdef __cplusplus
extern "C" {
Expand Down
4 changes: 4 additions & 0 deletions languages/c/templates/schemas/src/JsonData_Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include "Common/${info.title}.h"

namespace FireboltSDK {
namespace ${info.title} {
// Types
Expand Down
4 changes: 1 addition & 3 deletions languages/c/templates/schemas/src/Module_Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include "Firebolt.h"
#include "${info.title}.h"
#include "Common/${info.title}.h"
#include "FireboltSDK.h"
#include "JsonData_${info.title}.h"

/* ${ENUMS} */
Expand Down
33 changes: 28 additions & 5 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -742,7 +761,7 @@ function getRelatedSchemaLinks(schema = {}, json = {}, templates = {}, options =
}

const generateImports = (json, templates) => {
let imports = getTemplate('/imports/default', templates)
let imports = ''

if (rpcMethodsOrEmptyArray(json).length) {
imports += getTemplate('/imports/rpc', templates)
Expand Down Expand Up @@ -772,10 +791,14 @@ const generateImports = (json, templates) => {
imports += getTemplate('/imports/x-method', 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 => getTemplate('/imports/default', templates).replace(/\$\{info.title\}/g, shared)).join('')
}

let componentExternalSchema = getComponentExternalSchema(json)
if (componentExternalSchema.length && json.info['x-uri-titles']) {
imports += componentExternalSchema.map(shared => getTemplate('/imports/common', templates).replace(/\$\{info.title\}/g, shared)).join('')
}
return imports
}

Expand Down Expand Up @@ -1023,7 +1046,7 @@ function insertMethodMacros(template, methodObj, json, templates, examples={}) {
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...
}
Expand Down
4 changes: 2 additions & 2 deletions src/shared/typescript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -352,4 +352,4 @@ function getSchemaShape(schema = {}, module = {}, { name = '', level = 0, title,
getSchemaShape,
getSchemaType,
getJsonType
}
}

0 comments on commit 60f9762

Please sign in to comment.