Skip to content

Commit

Permalink
Merge branch feature/c-language to development-types/feature/c-language
Browse files Browse the repository at this point in the history
  • Loading branch information
HaseenaSainul committed Jun 1, 2023
2 parents f8eaa49 + 6de4490 commit 5f9f803
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 62 deletions.
32 changes: 1 addition & 31 deletions languages/c/src/types/JSONHelpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,36 +50,6 @@ function getJsonContainerDefinition (schema, 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, { name }) {
name = (schema.title || name)
let e = schema.description ? ('\n/* ${info.title} - ' + `${schema.description} */`) : ''
e += '\nENUM_CONVERSION_BEGIN(${info.title}_' + `${name})\n`
schema.enum.forEach(value => {
e += ' { ${info.TITLE}_' + `${name.toUpperCase()}_${keyName(value)}, _T("${value}") },\n`
})

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

return e
}

export {
getJsonContainerDefinition,
getJsonEnumConversion,
getJsonDataStructName
getJsonContainerDefinition
}
2 changes: 1 addition & 1 deletion languages/c/templates/modules/src/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace FireboltSDK {
}
}

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

#ifdef __cplusplus
extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion languages/c/templates/schemas/src/Module_Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "Common/${info.title}.h"
#include "JsonData_${info.title}.h"

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

#ifdef __cplusplus
extern "C" {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 24 additions & 27 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,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 @@ -321,8 +321,7 @@ const generateMacros = (obj, templates, languages, options = {}) => {

const imports = generateImports(obj, templates)
const initialization = generateInitialization(obj, templates)
const enums = generateEnums(obj, templates)
const enumsConversion = generateEnumsConversion(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 @@ -494,7 +493,7 @@ function insertTableofContents(content) {
return content
}

const isEnumType = x => x.type === 'string' && Array.isArray(x.enum)
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
Expand All @@ -503,9 +502,8 @@ const getProperties = x => {
const isEnumProperties = schema => compose(
getProperties,
filter(enm => enm),
map(enm => (enm.length > 0)),
map(filter(isEnumType)),
map(props => props.map(([k, v]) => v)),
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)
Expand All @@ -515,19 +513,22 @@ const getEnumProperties = schema => compose(
filter(enm => enm),
map(filter(isEnumType)),
map(props => props.map(([k, v]) => {
let enm = v
if (isEnumType(v) == true) {
let type = Object.assign({}, v)
type.title = k
return type
} else {
return v
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)
Expand All @@ -538,7 +539,9 @@ const convertEnumTemplate = (sch, templateName, templates) => {
return template[i].replace(/\$\{key\}/g, safeName)
.replace(/\$\{value\}/g, value)
}).join('\n')
template[i] = template[i].replace(/,*$/, '');
if (!templateName.includes(".cpp")) {
template[i] = template[i].replace(/,*$/, '');
}
}
}
return template.join('\n')
Expand All @@ -554,22 +557,16 @@ const enumFinder = compose(
filter(([_key, val]) => isObject(val))
)

const generateEnums = (json, templates) => {
return compose(
option(''),
map(reduce((acc, val) => acc.concat(val).concat('\n'), '')),
map(map((schema) => convertEnumTemplate(schema, '/types/enum', templates))),
map(enumFinder),
getSchemas
)(json)
}

const generateEnumsConversion = (json, templates) => {
const generateEnums = (json, templates, options = { destination: '' }) => {
const suffix = options.destination.split('.').pop()
return compose(
option(''),
map(val => val ? getTemplate('/sections/enum_conversion', templates).replace(/\$\{schema.list\}/g, val.trimEnd()): ''),
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) => convertEnumTemplate(schema, '/types/enum_conversion', templates))),
map(map((schema) => convertEnumTemplate(schema, suffix ? `/types/enum.${suffix}` : '/types/enum', templates))),
map(enumFinder),
getSchemas
)(json)
Expand Down

0 comments on commit 5f9f803

Please sign in to comment.