Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to generate file inclusion for common schema also #97

Merged
merged 14 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
362 changes: 197 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"
HaseenaSainul marked this conversation as resolved.
Show resolved Hide resolved
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
56 changes: 41 additions & 15 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import isString from 'crocks/core/isString.js'
import predicates from 'crocks/predicates/index.js'
const { isObject, isArray, propEq, pathSatisfies, propSatisfies } = predicates

import { isRPCOnlyMethod, isProviderInterfaceMethod, getProviderInterface, getPayloadFromEvent, providerHasNoParameters, isTemporalSetMethod, hasMethodAttributes, getMethodAttributes, isEventMethodWithContext, getSemanticVersion, getSetterFor, getProvidedCapabilities, isPolymorphicPullMethod, hasPublicAPIs } from '../shared/modules.mjs'
import { isRPCOnlyMethod, isProviderInterfaceMethod, getProviderInterface, getPayloadFromEvent, providerHasNoParameters, isTemporalSetMethod, hasMethodAttributes, getMethodAttributes, isEventMethodWithContext, getSemanticVersion, getSetterFor, getProvidedCapabilities, isPolymorphicPullMethod, isPolymorphicReducer, hasPublicAPIs } from '../shared/modules.mjs'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's remove all polymorphic reducer stuff into it's own branch until we sort out the plan for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved to separate branch

import isEmpty from 'crocks/core/isEmpty.js'
import { getLinkedSchemaPaths, getSchemaConstraints, isSchema, localizeDependencies, isDefinitionReferencedBySchema } from '../shared/json-schema.mjs'

Expand All @@ -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) => {
jlacivita marked this conversation as resolved.
Show resolved Hide resolved
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) : ''
HaseenaSainul marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -627,12 +646,11 @@ function generateSchemas(json, templates, options) {

const schemas = (options.section.includes('methods') ? (hasMethodsSchema(json) ? json.methods : '') : (json.definitions || (json.components && json.components.schemas) || {}))

const generate = (name, schema, uri, { prefix = '' } = {}) => {
const generate = (name, schema, uri, { prefix = '', type = '' } = {}) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is part of the polymorphic-reduce work right? if so, same comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, so moved to separate branch

// these are internal schemas used by the firebolt-openrpc tooling, and not meant to be used in code/doc generation
if (['ListenResponse', 'ProviderRequest', 'ProviderResponse', 'FederatedResponse', 'FederatedRequest'].includes(name)) {
return
}

let content = getTemplate('/schemas/default', templates)

if (!schema.examples || schema.examples.length === 0) {
Expand All @@ -648,7 +666,7 @@ function generateSchemas(json, templates, options) {
else {
content = content.replace(/\$\{if\.description\}(.*?)\{end\.if\.description\}/gms, '$1')
}
const schemaShape = types.getSchemaShape(schema, json, { name, prefix, destination: state.destination, section: options.section })
const schemaShape = types.getSchemaShape(schema, json, { name, prefix, type, destination: state.destination, section: options.section })

content = content
.replace(/\$\{schema.title\}/, (schema.title || name))
Expand Down Expand Up @@ -693,13 +711,18 @@ function generateSchemas(json, templates, options) {
}
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 (isPolymorphicReducer(schema)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved to separate branch

list.push([schema.name, schema, '', { type : 'reducer' }])
}
else {
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 }])
}
})
if (schema.result.schema && (schema.result.schema.type === 'object')) {
list.push([schema.result.name, schema.result.schema, '', { prefix : schema.name}])
}
}
}
Expand Down Expand Up @@ -742,7 +765,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 +795,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']) {
jlacivita marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -1021,7 +1048,6 @@ 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
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