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

fix: Sort the macrofied schemas based on schema refs #90

Merged
merged 3 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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])
Copy link
Contributor

Choose a reason for hiding this comment

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

You'll need to check for the presence of uri (third array item).

If it's present, then the path to the schema is /x-schemas/${module.info.title} and we didn't grab the title in the loop above, so we don't have it down here.

I recommend removing the else above, to keep this code simpler.

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