Skip to content

Commit

Permalink
fix: Sort the macrofied schemas based on schema refs (#90)
Browse files Browse the repository at this point in the history
* fix: Sort the macrofied schemas based on schema refs

* fix: Sort the schemas before macrofying

* fix: Handle the schemas part
  • Loading branch information
sramani-metro authored and HaseenaSainul committed Jun 6, 2023
1 parent 7fc7c89 commit 1363db3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
15 changes: 12 additions & 3 deletions languages/c/templates/modules/src/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,25 @@

/* ${IMPORTS} */

namespace FireboltSDK {
namespace ${info.title} {

/* ${TYPES} */

}
}


#ifdef __cplusplus
extern "C" {
#endif

/* ${TYPES} */
/* ${ACCESSORS} */
/* ${METHODS} */


#ifdef __cplusplus
}
#endif

/* ${ACCESSORS} */
/* ${METHODS} */

29 changes: 18 additions & 11 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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 = []

Expand Down Expand Up @@ -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,
Expand All @@ -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
}

Expand Down

0 comments on commit 1363db3

Please sign in to comment.