From ef9c924f4f0c7850412170b540ae702b8c8b7d6a Mon Sep 17 00:00:00 2001 From: sramani-metro <71630728+sramani-metro@users.noreply.github.com> Date: Tue, 23 May 2023 12:33:26 +0530 Subject: [PATCH] fix: Sort the macrofied schemas based on schema refs (#90) * fix: Sort the macrofied schemas based on schema refs * fix: Sort the schemas before macrofying * fix: Handle the schemas part --- languages/c/templates/modules/src/Module.cpp | 15 ++++++++-- src/macrofier/engine.mjs | 29 ++++++++++++-------- 2 files changed, 30 insertions(+), 14 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 f0b5959c..958f0bea 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 => { @@ -564,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 = [] @@ -609,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, @@ -625,21 +625,28 @@ 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.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 }