From 425168b5388979d82709bee9c74acd69725337a7 Mon Sep 17 00:00:00 2001 From: Santhosh Ramani Date: Thu, 4 May 2023 19:23:20 +0530 Subject: [PATCH 1/3] fix: Sort the macrofied schemas based on schema refs --- languages/c/templates/modules/src/Module.cpp | 15 ++++++++++++--- src/macrofier/engine.mjs | 10 ++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/languages/c/templates/modules/src/Module.cpp b/languages/c/templates/modules/src/Module.cpp index 0783b416..b5522fd6 100644 --- a/languages/c/templates/modules/src/Module.cpp +++ b/languages/c/templates/modules/src/Module.cpp @@ -22,16 +22,25 @@ /* ${IMPORTS} */ +namespace FireboltSDK { + namespace ${info.title} { + + /* ${TYPES} */ + + } +} + #ifdef __cplusplus extern "C" { #endif -/* ${TYPES} */ +/* ${ACCESSORS} */ +/* ${METHODS} */ + #ifdef __cplusplus } #endif -/* ${ACCESSORS} */ -/* ${METHODS} */ + diff --git a/src/macrofier/engine.mjs b/src/macrofier/engine.mjs index db0304e4..dba71778 100644 --- a/src/macrofier/engine.mjs +++ b/src/macrofier/engine.mjs @@ -339,6 +339,16 @@ const generateMacros = (obj, templates, languages, options = {}) => { const eventList = eventsArray.map(m => makeEventName(m)) const defaults = generateDefaults(obj, templates) const schemasArray = generateSchemas(obj, templates, { baseUrl: '', section: 'schemas' }).filter(s => (options.copySchemasIntoModules || !s.uri)) + schemasArray.sort((a, b) => { + const aInB = b.body.toLowerCase().indexOf(a.name.toLowerCase()) !== -1; + const bInA = a.body.toLowerCase().indexOf(b.name.toLowerCase()) !== -1; + if(a.enum || (aInB && !bInA)) { + return -1 + } else if(b.enum || (!aInB && bInA)) { + return 1 + } + return 0; + }) 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).join('\n')) : '' const typesArray = schemasArray.filter(x => !x.enum) From ed93e61f5856f35e4c20ff3f23fcdf6393a87e40 Mon Sep 17 00:00:00 2001 From: Santhosh Ramani Date: Thu, 4 May 2023 20:25:34 +0530 Subject: [PATCH 2/3] fix: Sort the schemas before macrofying --- src/macrofier/engine.mjs | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/macrofier/engine.mjs b/src/macrofier/engine.mjs index dba71778..9289291e 100644 --- a/src/macrofier/engine.mjs +++ b/src/macrofier/engine.mjs @@ -31,7 +31,7 @@ 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 isEmpty from 'crocks/core/isEmpty.js' -import { getLinkedSchemaPaths, getSchemaConstraints, isSchema, localizeDependencies } from '../shared/json-schema.mjs' +import { getLinkedSchemaPaths, getSchemaConstraints, isSchema, localizeDependencies, isDefinitionReferencedBySchema } from '../shared/json-schema.mjs' // util for visually debugging crocks ADTs const _inspector = obj => { @@ -339,16 +339,6 @@ const generateMacros = (obj, templates, languages, options = {}) => { const eventList = eventsArray.map(m => makeEventName(m)) const defaults = generateDefaults(obj, templates) const schemasArray = generateSchemas(obj, templates, { baseUrl: '', section: 'schemas' }).filter(s => (options.copySchemasIntoModules || !s.uri)) - schemasArray.sort((a, b) => { - const aInB = b.body.toLowerCase().indexOf(a.name.toLowerCase()) !== -1; - const bInA = a.body.toLowerCase().indexOf(b.name.toLowerCase()) !== -1; - if(a.enum || (aInB && !bInA)) { - return -1 - } else if(b.enum || (!aInB && bInA)) { - return 1 - } - return 0; - }) 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).join('\n')) : '' const typesArray = schemasArray.filter(x => !x.enum) @@ -574,6 +564,8 @@ function generateDefaults(json = {}, templates) { return reducer(json) } +const isEnum = x => x.type && x.type === 'string' && Array.isArray(x.enum) && x.title + function generateSchemas(json, templates, options) { let results = [] @@ -619,8 +611,6 @@ function generateSchemas(json, templates, options) { content = content.replace(/.*\$\{schema.seeAlso\}/, '') } - const isEnum = x => x.type === 'string' && Array.isArray(x.enum) && x.title - const result = uri ? { uri: uri, name: schema.title || name, @@ -635,21 +625,36 @@ function generateSchemas(json, templates, options) { results.push(result) } + const list = [] + // schemas may be 1 or 2 levels deeps Object.entries(schemas).forEach( ([name, schema]) => { if (isSchema(schema)) { - generate(name, schema) + list.push([name, schema]) } else if (typeof schema === 'object') { const uri = schema.uri Object.entries(schema).forEach( ([name, schema]) => { if (name !== 'uri') { - generate(name, schema, uri) + list.push([name, schema, uri]) } }) } }) + list.sort((a, b) => { + const aInB = isDefinitionReferencedBySchema('#/components/schemas/' + a[0], b[1]) + const bInA = isDefinitionReferencedBySchema('#/components/schemas/' + b[0], a[1]) + if(isEnum(a[1]) || (aInB && !bInA)) { + return -1 + } else if(isEnum(b[1]) || (!aInB && bInA)) { + return 1 + } + return 0; + }) + + list.forEach(item => generate(...item)) + return results } From c29a6e53e1b25f46b9e7a3034d52bf5569fa32cf Mon Sep 17 00:00:00 2001 From: Santhosh Ramani Date: Thu, 11 May 2023 12:53:54 +0530 Subject: [PATCH 3/3] fix: Handle the schemas part --- src/macrofier/engine.mjs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/macrofier/engine.mjs b/src/macrofier/engine.mjs index 9289291e..e98bc090 100644 --- a/src/macrofier/engine.mjs +++ b/src/macrofier/engine.mjs @@ -632,14 +632,6 @@ function generateSchemas(json, templates, options) { if (isSchema(schema)) { list.push([name, schema]) } - else if (typeof schema === 'object') { - const uri = schema.uri - Object.entries(schema).forEach( ([name, schema]) => { - if (name !== 'uri') { - list.push([name, schema, uri]) - } - }) - } }) list.sort((a, b) => {