Skip to content

Commit

Permalink
Add support to generate file inclusion for common schema also (#97)
Browse files Browse the repository at this point in the history
Add support to generate file inclusion for common schema also
  • Loading branch information
HaseenaSainul committed Aug 11, 2023
1 parent 3ab8bad commit 0e742f2
Show file tree
Hide file tree
Showing 14 changed files with 226 additions and 268 deletions.
336 changes: 172 additions & 164 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/default.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "JsonData_${info.title}.h"
File renamed without changes.
1 change: 1 addition & 0 deletions languages/c/templates/imports/default.jsondata
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "JsonData_${info.title}.h"
40 changes: 0 additions & 40 deletions languages/c/templates/modules/include/Common/Module.h

This file was deleted.

29 changes: 0 additions & 29 deletions languages/c/templates/modules/src/JsonData_Module.h

This file was deleted.

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
5 changes: 5 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,11 @@
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

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

namespace FireboltSDK {
namespace ${info.title} {
// Types
Expand Down
5 changes: 2 additions & 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,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} */
Expand Down
50 changes: 39 additions & 11 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 @@ -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)
Expand All @@ -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 @@ -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) {
Expand Down Expand Up @@ -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 }])
}
}
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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...
}
Expand Down
2 changes: 1 addition & 1 deletion 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 Down

0 comments on commit 0e742f2

Please sign in to comment.