Skip to content

Commit

Permalink
Enum fixes (#93)
Browse files Browse the repository at this point in the history
Enum fixes :
1. missing description added
2. alignment fixes
3. implementation added for enum inside properties
4. Enum to String value Conversion logic added for C
  • Loading branch information
HaseenaSainul authored Jun 1, 2023
1 parent 7eab2ad commit 6de4490
Show file tree
Hide file tree
Showing 16 changed files with 125 additions and 89 deletions.
31 changes: 1 addition & 30 deletions languages/c/src/types/JSONHelpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,6 @@ function getJsonContainerDefinition (name, props) {
return c
}

/*
ENUM_CONVERSION_BEGIN(Advertising_SkipRestriction)
{ ADVERTISING_SKIPRESTRICTION_NONE, _T("none") },
{ ADVERTISING_SKIPRESTRICTION_ADS_UNWATCHED, _T("adsUnwatched") },
{ ADVERTISING_SKIPRESTRICTION_ADS_ALL, _T("adsAll") },
{ ADVERTISING_SKIPRESTRICTION_ALL, _T("all") },
ENUM_CONVERSION_END(Advertising_SkipRestriction)
*/

// TODO - this should be a global function in the main /src/ directory... not part of a language pack
const keyName = val => val.split(':').pop().replace(/[\.\-]/g, '_').replace(/\+/g, '_plus').replace(/([a-z])([A-Z0-9])/g, '$1_$2').toUpperCase()

function getJsonEnumConversion(schema, module, { name }) {
name = capitalize(schema.title || name)
let e = `ENUM_CONVERSION_BEGIN(${module.info.title}_${name})\n`

schema.enum.forEach(value => {
e += `{ ${module.info.title.toUpperCase()}_${name.toUpperCase()}_${keyName(value)}, _T("${value}") },`
})

e += `ENUM_CONVERSION_END(${module.info.title}_${name})`

return e
}

export {
getJsonContainerDefinition,
getJsonEnumConversion
getJsonContainerDefinition
}
9 changes: 5 additions & 4 deletions languages/c/templates/declarations/default.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/**
* ${method.summary}
${method.params} */
${method.signature};
/*
* ${method.summary}
* ${method.params}
*/
${method.signature};
11 changes: 6 additions & 5 deletions languages/c/templates/declarations/polymorphic-reducer.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* ${method.summary}
${method.params} */
${method.signature}
/*
* ${method.summary}
* ${method.params}
*/
${method.signature}

// TODO: generate reducer signature
// TODO: generate reducer signature
10 changes: 5 additions & 5 deletions languages/c/templates/modules/include/Common/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@
extern "C" {
#endif

// Enums
// Enums

/* ${ENUMS}} */
/* ${ENUMS} */

// Schemas
// Schemas

/* ${SCHEMAS} */
/* ${SCHEMAS} */

#ifdef __cplusplus
}
#endif

#endif // Header Include Guard
#endif // Header Include Guard
15 changes: 11 additions & 4 deletions languages/c/templates/modules/include/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,20 @@
extern "C" {
#endif

/* ${SCHEMAS} */
// Enums

/* ${ACCESSORS} */
/* ${DECLARATIONS} */
/* ${ENUMS} */

// Types

/* ${TYPES} */

/* ${ACCESSORS} */

/* ${DECLARATIONS} */

#ifdef __cplusplus
}
#endif

#endif // Header Include Guard
#endif // Header Include Guard
6 changes: 3 additions & 3 deletions languages/c/templates/modules/src/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@

namespace FireboltSDK {
namespace ${info.title} {
// Types

/* ${TYPES} */

/* ${TYPES} */
}
}

/* ${ENUMS} */

#ifdef __cplusplus
extern "C" {
Expand All @@ -38,7 +39,6 @@ extern "C" {
/* ${ACCESSORS} */
/* ${METHODS} */


#ifdef __cplusplus
}
#endif
Expand Down
7 changes: 3 additions & 4 deletions languages/c/templates/schemas/default.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/**
* ${schema.description}
*/

/*
* ${schema.description}
*/
${schema.shape}
10 changes: 4 additions & 6 deletions languages/c/templates/schemas/include/Common/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@
extern "C" {
#endif

// Enums
// Enums

/* ${ENUMS} */
/* ${ENUMS} */

// Types

/* ${TYPES} */
/* ${TYPES} */

#ifdef __cplusplus
}
#endif

#endif // Header Include Guard
#endif // Header Include Guard
6 changes: 4 additions & 2 deletions languages/c/templates/schemas/src/JsonData_Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
*/

namespace FireboltSDK {
namespace ${info.title} {
// Types

/* ${DECLARATIONS} */

/* ${SCHEMAS}*/
}
}
3 changes: 2 additions & 1 deletion languages/c/templates/schemas/src/Module_Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
#include "Common/${info.title}.h"
#include "JsonData_${info.title}.h"

/* ${ENUMS} */

#ifdef __cplusplus
extern "C" {
#endif

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

Expand Down
5 changes: 5 additions & 0 deletions languages/c/templates/sections/enum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace WPEFramework {

${schema.list}

}
2 changes: 0 additions & 2 deletions languages/c/templates/sections/schemas.c
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
// Types & Enums

${schema.list}
2 changes: 0 additions & 2 deletions languages/c/templates/sections/types.c
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
// Types

${schema.list}
4 changes: 4 additions & 0 deletions languages/c/templates/types/enum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* ${title} ${description} */
ENUM_CONVERSION_BEGIN(${info.title}_${name})
{ ${info.TITLE}_${NAME}_${key}, _T("${value}") },
ENUM_CONVERSION_END(${info.title}_${name})
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* ${title} ${description} */
typedef enum {
${info.TITLE}_${NAME}_${key},
} ${info.title}_${name};
92 changes: 71 additions & 21 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ const state = {
section: undefined
}

const capitalize = str => str[0].toUpperCase() + str.substr(1)

const setTyper = (t) => {
types = t
}
Expand All @@ -73,7 +75,7 @@ const setConfig = (c) => {
}

const getTemplate = (name, templates) => {
return templates[Object.keys(templates).find(k => k.startsWith(name + '.'))] || ''
return templates[Object.keys(templates).find(k => k === name)] || templates[Object.keys(templates).find(k => k.startsWith(name + '.'))] || ''
}

const getTemplateTypeForMethod = (method, type, templates) => {
Expand Down Expand Up @@ -322,7 +324,7 @@ const generateMacros = (obj, templates, languages, options = {}) => {

const imports = generateImports(obj, templates)
const initialization = generateInitialization(obj, templates)
const enums = generateEnums(obj, templates)
const enums = generateEnums(obj, templates, { destination : (options.destination ? options.destination : '') })
const eventsEnum = generateEvents(obj, templates)
const examples = generateExamples(obj, templates, languages)

Expand Down Expand Up @@ -486,29 +488,80 @@ function insertTableofContents(content) {
return content
}

const isEnumType = x => x.type !== 'undefined' && x.type === 'string' && Array.isArray(x.enum)

const getProperties = x => {
return Array.isArray(x.properties) ? x.properties[0] : x.properties
}

const isEnumProperties = schema => compose(
getProperties,
filter(enm => enm),
map(filter(enm => enm)),
map(props => props.map(([k, v]) => ((v.type === 'object') ? isEnumProperties(v) : ((v.type === 'array') ? isEnumType(v.items[0] ? v.items[0] : v.items): isEnumType(v))))),
map(Object.entries),
filter(schema => isObject(schema))
)(schema)

const getEnumProperties = schema => compose(
getProperties,
filter(enm => enm),
map(filter(isEnumType)),
map(props => props.map(([k, v]) => {
let enm = v
if (isEnumType(v) == true) {
enm = Object.assign({}, v)
enm.title = k
} else if (v.type === 'object') {
enm = getEnumProperties(v)
} else if (v.type === 'array') {
enm = Object.assign({}, (v.items[0] ? v.items[0] : v.items))
enm.title = k
}
return enm
})),
map(Object.entries),
filter(schema => isObject(schema))
)(schema)

const convertEnumTemplate = (sch, templateName, templates) => {
const template = getTemplate(templateName, templates).split('\n')
let schema = isEnumType(sch) ? sch : getEnumProperties(sch)
for (var i = 0; i < template.length; i++) {
if (template[i].indexOf('${key}') >= 0) {
template[i] = schema.enum.map(value => {
const safeName = value.split(':').pop().replace(/[\.\-]/g, '_').replace(/\+/g, '_plus').replace(/([a-z])([A-Z0-9])/g, '$1_$2').toUpperCase()
return template[i].replace(/\$\{key\}/g, safeName)
.replace(/\$\{value\}/g, value)
}).join('\n')
if (!templateName.includes(".cpp")) {
template[i] = template[i].replace(/,*$/, '');
}
}
}
return template.join('\n')
.replace(/\$\{title\}/g, capitalize(schema.title))
.replace(/\$\{description\}/g, schema.description ? ('- ' + schema.description) : '')
.replace(/\$\{name\}/g, schema.title)
.replace(/\$\{NAME\}/g, schema.title.toUpperCase())
}

const enumFinder = compose(
filter(x => x.type === 'string' && Array.isArray(x.enum) && x.title),
filter(x => ((isEnumType(x) && x.title) || isEnumProperties(x))),
map(([_, val]) => val),
filter(([_key, val]) => isObject(val))
)

const generateEnums = (json, templates) => {
const generateEnums = (json, templates, options = { destination: '' }) => {
const suffix = options.destination.split('.').pop()
return compose(
option(''),
map(val => {
let template = getTemplate(`/sections/enum.${suffix}`, templates)
return template ? template.replace(/\$\{schema.list\}/g, val.trimEnd()) : val
}),
map(reduce((acc, val) => acc.concat(val).concat('\n'), '')),
map(map((schema) => {
const template = getTemplate('/types/enum', templates).split('\n')
for (var i = 0; i < template.length; i++) {
if (template[i].indexOf('${key}') >= 0) {
template[i] = schema.enum.map(value => {
const safeName = value.split(':').pop().replace(/[\.\-]/g, '_').replace(/\+/g, '_plus').replace(/([a-z])([A-Z0-9])/g, '$1_$2').toUpperCase()
return template[i].replace(/\$\{key\}/g, safeName)
.replace(/\$\{value\}/g, value)
}).join('\n')
}
}
return template.join('\n').replace(/\$\{name\}/g, schema.title).replace(/\$\{NAME\}/g, schema.title.toUpperCase())
})),
map(map((schema) => convertEnumTemplate(schema, suffix ? `/types/enum.${suffix}` : '/types/enum', templates))),
map(enumFinder),
getSchemas
)(json)
Expand Down Expand Up @@ -701,8 +754,6 @@ const generateImports = (json, templates) => {
imports += getTemplate('/imports/x-method', templates)
}



if (json['x-schemas'] && Object.keys(json['x-schemas']).length > 0) {
imports += Object.keys(json['x-schemas']).map(shared => getTemplate('/imports/default', templates).replace(/\$\{info.title\}/g, shared)).join('\n')
}
Expand Down Expand Up @@ -1242,7 +1293,6 @@ function generateProviderInterfaces(json, templates) {
function insertProviderInterfaceMacros(template, capability, moduleJson = {}, templates) {
const iface = getProviderInterface(capability, moduleJson, { destination: state.destination, section: state.section })//.map(method => { method.name = method.name.charAt(9).toLowerCase() + method.name.substr(10); return method } )

const capitalize = str => str[0].toUpperCase() + str.substr(1)
const uglyName = capability.split(":").slice(-2).map(capitalize).reverse().join('') + "Provider"
let name = iface.length === 1 ? iface[0].name.charAt(0).toUpperCase() + iface[0].name.substr(1) + "Provider" : uglyName

Expand Down Expand Up @@ -1396,4 +1446,4 @@ export default {
insertAggregateMacros,
setTyper,
setConfig
}
}

0 comments on commit 6de4490

Please sign in to comment.