Skip to content

Commit

Permalink
Types and Accessors generation added for objects inside methods resul…
Browse files Browse the repository at this point in the history
…t/params
  • Loading branch information
HaseenaSainul committed May 25, 2023
1 parent da0284b commit 7e9872c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
1 change: 1 addition & 0 deletions languages/c/templates/sections/methods_accessors.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${schema.list}
1 change: 1 addition & 0 deletions languages/c/templates/sections/methods_types.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${schema.list}
31 changes: 25 additions & 6 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const state = {
}

const capitalize = str => str[0].toUpperCase() + str.substr(1)
const hasMethodsSchema = (json, options) => json.methods && json.methods.length

const setTyper = (t) => {
types = t
Expand Down Expand Up @@ -333,6 +334,11 @@ const generateMacros = (obj, templates, languages, options = {}) => {
const declarations = declarationsArray.length ? getTemplate('/sections/declarations', templates).replace(/\$\{declaration\.list\}/g, declarationsArray.map(m => m.declaration).join('\n')) : ''
const methods = methodsArray.length ? getTemplate('/sections/methods', templates).replace(/\$\{method.list\}/g, methodsArray.map(m => m.body).join('\n')) : ''
const methodList = methodsArray.filter(m => m.body).map(m => m.name)
const methodTypesArray = generateSchemas(obj, templates, { baseUrl: '', section: 'methods_schemas' }).filter(s => (options.copySchemasIntoModules || !s.uri))
const methodTypes = methodTypesArray.length ? getTemplate('/sections/methods_types', templates).replace(/\$\{schema.list\}/g, methodTypesArray.map(s => s.body).filter(body => body).join('\n')) : ''
const methodAccessorsArray = generateSchemas(obj, templates, { baseUrl: '', section: 'methods_accessors' }).filter(s => (options.copySchemasIntoModules || !s.uri))
const methodAccessors = methodAccessorsArray.length ? getTemplate('/sections/methods_accessors', templates).replace(/\$\{schema.list\}/g, methodAccessorsArray.map(s => s.body).filter(body => body).join('\n')) : ''

const providerInterfaces = generateProviderInterfaces(obj, templates)
const events = eventsArray.length ? getTemplate('/sections/events', templates).replace(/\$\{event.list\}/g, eventsArray.map(m => m.body).join('\n')) : ''
const eventList = eventsArray.map(m => makeEventName(m))
Expand All @@ -341,8 +347,9 @@ const generateMacros = (obj, templates, languages, options = {}) => {
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 types = typesArray.length ? getTemplate('/sections/types', templates).replace(/\$\{schema.list\}/g, typesArray.map(s => s.body).filter(body => body).join('\n')) : ''
const accessors = accessorsArray.length ? getTemplate('/sections/accessors', templates).replace(/\$\{schema.list\}/g, accessorsArray.map(s => s.body).filter(body => body).join('\n')) : ''
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
const module = getTemplate('/codeblocks/module', templates)

const macros = {
Expand Down Expand Up @@ -623,9 +630,9 @@ const isEnum = x => x.type && x.type === 'string' && Array.isArray(x.enum) && x.
function generateSchemas(json, templates, options) {
let results = []

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

const generate = (name, schema, uri) => {
const generate = (name, schema, uri, { prefix = '' } = {}) => {
// 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
Expand All @@ -646,7 +653,7 @@ function generateSchemas(json, templates, options) {
else {
content = content.replace(/\$\{if\.description\}(.*?)\{end\.if\.description\}/gms, '$1')
}
const schemaShape = types.getSchemaShape(schema, json, { name, destination: state.destination, section: options.section })
const schemaShape = types.getSchemaShape(schema, json, { name, prefix, destination: state.destination, section: options.section })

content = content
.replace(/\$\{schema.title\}/, (schema.title || name))
Expand Down Expand Up @@ -689,6 +696,18 @@ function generateSchemas(json, templates, options) {
if (isSchema(schema)) {
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}])
}
}
}
})

list.sort((a, b) => {
Expand Down Expand Up @@ -1224,7 +1243,7 @@ function generateResult(result, json, templates, { name = '' } = {}) {
else {
const sch = localizeDependencies(result, json)
return getTemplate('/types/default', templates)
.replace(/\$\{type\}/, types.getSchemaShape(sch, json, { name: result.$ref.split("/").pop()}))
.replace(/\$\{type\}/, types.getSchemaShape(sch, json, { name: result.$ref.split("/").pop() }))
}
}
else {
Expand Down
3 changes: 1 addition & 2 deletions src/macrofier/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ const macrofy = async (
}
})

console.log()
}

// Grab all schema groups w/ a URI string. These came from some external json-schema that was bundled into the OpenRPC
Expand Down Expand Up @@ -271,4 +270,4 @@ const macrofy = async (
})
}

export default macrofy
export default macrofy

0 comments on commit 7e9872c

Please sign in to comment.