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 1 commit
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} */

10 changes: 10 additions & 0 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

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

This code will incorrectly assume that a class named "Foo" is used, when it's actually a class named "BarFoo" or "FooBar"

I might have been mistaken when I suggested putting the sorting here. At this point in the code, the language pack has already done it's work and we have no idea if b.name has been transformed in some way.

We might want to move this down to generateSchemas with something like this:

  const list = []
  // schemas may be 1 or 2 levels deeps
  Object.entries(schemas).forEach( ([name, schema]) => {
    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(...) // rewrite your sort method to use the real schemas, not string searching

  list.forEach(item => generate(...item)

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)
Expand Down