From fd66f378246be7575d24d885ee7cdca40a289339 Mon Sep 17 00:00:00 2001 From: Santhosh Ramani Date: Wed, 6 Sep 2023 13:20:43 +0530 Subject: [PATCH] feat: Fix issues in generated headers --- languages/cpp/templates/declarations/default.c | 12 ++++++------ languages/cpp/templates/declarations/event.c | 18 ++++++++---------- .../declarations/polymorphic-pull-event.c | 14 +++++++------- .../templates/declarations/polymorphic-pull.c | 4 ++-- .../declarations/polymorphic-reducer.c | 10 +++++----- .../cpp/templates/declarations/property.h | 10 +++++----- languages/cpp/templates/declarations/setter.h | 10 +++++----- .../cpp/templates/modules/include/module.h | 12 ++++++------ .../parameters/{default.c => default.h} | 0 .../templates/parameters/{json.c => json.cpp} | 0 .../parameters/{object.c => object.h} | 0 .../parameters/{optional.c => optional.h} | 0 .../cpp/templates/parameters/result-null.h | 0 .../parameters/{result.c => result.h} | 0 .../parameters/{string.c => string.h} | 0 .../cpp/templates/types/additionalProperties.h | 2 +- languages/cpp/templates/types/array.h | 2 +- languages/cpp/templates/types/null.h | 1 - languages/cpp/templates/types/property.h | 2 +- languages/cpp/templates/types/tuple.h | 6 +----- src/macrofier/engine.mjs | 12 ++++++------ 21 files changed, 54 insertions(+), 61 deletions(-) rename languages/cpp/templates/parameters/{default.c => default.h} (100%) rename languages/cpp/templates/parameters/{json.c => json.cpp} (100%) rename languages/cpp/templates/parameters/{object.c => object.h} (100%) rename languages/cpp/templates/parameters/{optional.c => optional.h} (100%) create mode 100644 languages/cpp/templates/parameters/result-null.h rename languages/cpp/templates/parameters/{result.c => result.h} (100%) rename languages/cpp/templates/parameters/{string.c => string.h} (100%) diff --git a/languages/cpp/templates/declarations/default.c b/languages/cpp/templates/declarations/default.c index 4366becf..2c19a1ef 100644 --- a/languages/cpp/templates/declarations/default.c +++ b/languages/cpp/templates/declarations/default.c @@ -1,6 +1,6 @@ - /* - ${method.name} - ${method.description} - ${method.params.annotations}${if.deprecated} * @deprecated ${method.deprecation}${end.if.deprecated} - */ - virtual int32_t ${method.Name}( ${method.signature.params}${if.result}${if.params}, ${end.if.params}${method.result.type}& ${method.result.name}${end.if.result} ) = 0; \ No newline at end of file + /* + ${method.name} + ${method.description} + ${method.params.annotations}${if.deprecated} * @deprecated ${method.deprecation}${end.if.deprecated} + */ + virtual int32_t ${method.Name}( ${method.signature.params}${if.result}${if.params}, ${end.if.params}${method.result.type}& ${method.result.name}${end.if.result} ) = 0; \ No newline at end of file diff --git a/languages/cpp/templates/declarations/event.c b/languages/cpp/templates/declarations/event.c index d20578c9..d2df4f12 100644 --- a/languages/cpp/templates/declarations/event.c +++ b/languages/cpp/templates/declarations/event.c @@ -1,10 +1,8 @@ - /* ${method.name} - ${method.description} */ - struct I${method.Name}Notification { - virtual void ${method.Name} ( ${event.signature.callback.params}${if.event.params}, ${end.if.event.params}${method.result.properties} ) = 0; - }; - - virtual int32_t subscribe ( ${event.signature.params}${if.event.params}, ${end.if.event.params}I${method.Name}Notification& notification ) = 0; - virtual int32_t unsubscribe ( I${method.Name}Notification& notification ) = 0; - - // signature callback params: ${event.signature.callback.params} - // method result properties : ${method.result.properties} + /* ${method.name} - ${method.description} */ + struct I${method.Name}Notification { + virtual void ${method.Name} ( ${event.signature.callback.params}${if.event.params}, ${end.if.event.params}${method.result.properties} ) = 0; + }; + // signature callback params: ${event.signature.callback.params} + // method result properties : ${method.result.properties} + virtual int32_t subscribe ( ${event.signature.params}${if.event.params}, ${end.if.event.params}I${method.Name}Notification& notification ) = 0; + virtual int32_t unsubscribe ( I${method.Name}Notification& notification ) = 0; \ No newline at end of file diff --git a/languages/cpp/templates/declarations/polymorphic-pull-event.c b/languages/cpp/templates/declarations/polymorphic-pull-event.c index 755f7f54..178678ad 100644 --- a/languages/cpp/templates/declarations/polymorphic-pull-event.c +++ b/languages/cpp/templates/declarations/polymorphic-pull-event.c @@ -1,8 +1,8 @@ -/* ${method.name} - ${method.description} */ -struct I{method.Name}Notification { - virtual void ${method.Name} ( ${method.pulls.param.type}& value ) = 0; -}; - -virtual int32_t subscribe ( I{method.Name}Notification& notification ) = 0; -virtual int32_t unsubscribe ( I{method.Name}Notification& notification ) = 0; + /* ${method.name} - ${method.description} */ + struct I{method.Name}Notification { + virtual void ${method.Name} ( ${method.pulls.param.type}& value ) = 0; + }; + + virtual int32_t subscribe ( I{method.Name}Notification& notification ) = 0; + virtual int32_t unsubscribe ( I{method.Name}Notification& notification ) = 0; diff --git a/languages/cpp/templates/declarations/polymorphic-pull.c b/languages/cpp/templates/declarations/polymorphic-pull.c index 74f23cc4..6588a635 100644 --- a/languages/cpp/templates/declarations/polymorphic-pull.c +++ b/languages/cpp/templates/declarations/polymorphic-pull.c @@ -1,2 +1,2 @@ -/* ${method.name} - ${method.description} */ -virtual int32_t ${method.Name}( ${method.signature.params} ) = 0; + /* ${method.name} - ${method.description} */ + virtual int32_t ${method.Name}( ${method.signature.params} ) = 0; diff --git a/languages/cpp/templates/declarations/polymorphic-reducer.c b/languages/cpp/templates/declarations/polymorphic-reducer.c index 46f633b8..a6f50d1b 100644 --- a/languages/cpp/templates/declarations/polymorphic-reducer.c +++ b/languages/cpp/templates/declarations/polymorphic-reducer.c @@ -1,5 +1,5 @@ -/* - * ${method.summary} - * ${method.params} - */ -virtual int32_t ${method.Name}(${method.signature.params}${if.result.properties}${if.params}, ${end.if.params}${end.if.result.properties}${method.result.properties}) = 0; + /* + * ${method.summary} + * ${method.params} + */ + virtual int32_t ${method.Name}(${method.signature.params}${if.result.properties}${if.params}, ${end.if.params}${end.if.result.properties}${method.result.properties}) = 0; diff --git a/languages/cpp/templates/declarations/property.h b/languages/cpp/templates/declarations/property.h index 884fd2f0..09cc5d7d 100644 --- a/languages/cpp/templates/declarations/property.h +++ b/languages/cpp/templates/declarations/property.h @@ -1,5 +1,5 @@ - /* - * ${method.description} - * ${method.params} - */ - virtual int32_t ${method.Name} (${method.signature.params}${if.params}, ${end.if.params}${method.result.properties}) = 0; + /* + * ${method.description} + * ${method.params} + */ +virtual int32_t ${method.Name} (${method.signature.params}${if.params}, ${end.if.params}${method.result.properties}) = 0; diff --git a/languages/cpp/templates/declarations/setter.h b/languages/cpp/templates/declarations/setter.h index 75d3b5a6..64e4d19e 100644 --- a/languages/cpp/templates/declarations/setter.h +++ b/languages/cpp/templates/declarations/setter.h @@ -1,5 +1,5 @@ - /* - ${method.rpc.name} - ${method.description} - */ - virtual int32_t ${method.Name}( ${method.signature.params} ) = 0; + /* + ${method.rpc.name} + ${method.description} + */ + virtual int32_t ${method.Name}( ${method.signature.params} ) = 0; diff --git a/languages/cpp/templates/modules/include/module.h b/languages/cpp/templates/modules/include/module.h index f6d5128d..462c9a30 100644 --- a/languages/cpp/templates/modules/include/module.h +++ b/languages/cpp/templates/modules/include/module.h @@ -25,19 +25,19 @@ namespace ${info.Title} { // Enums -/* ${ENUMS} */ + /* ${ENUMS} */ // Types /* ${TYPES} */ - struct I${info.Title} { +struct I${info.Title} { - virtual ~I${info.Title} = default; + virtual ~I${info.Title} = default; - // Methods & Events - /* ${DECLARATIONS} */ +// Methods & Events +/* ${DECLARATIONS} */ - }; +}; } //namespace ${info.Title} }${end.if.declarations} diff --git a/languages/cpp/templates/parameters/default.c b/languages/cpp/templates/parameters/default.h similarity index 100% rename from languages/cpp/templates/parameters/default.c rename to languages/cpp/templates/parameters/default.h diff --git a/languages/cpp/templates/parameters/json.c b/languages/cpp/templates/parameters/json.cpp similarity index 100% rename from languages/cpp/templates/parameters/json.c rename to languages/cpp/templates/parameters/json.cpp diff --git a/languages/cpp/templates/parameters/object.c b/languages/cpp/templates/parameters/object.h similarity index 100% rename from languages/cpp/templates/parameters/object.c rename to languages/cpp/templates/parameters/object.h diff --git a/languages/cpp/templates/parameters/optional.c b/languages/cpp/templates/parameters/optional.h similarity index 100% rename from languages/cpp/templates/parameters/optional.c rename to languages/cpp/templates/parameters/optional.h diff --git a/languages/cpp/templates/parameters/result-null.h b/languages/cpp/templates/parameters/result-null.h new file mode 100644 index 00000000..e69de29b diff --git a/languages/cpp/templates/parameters/result.c b/languages/cpp/templates/parameters/result.h similarity index 100% rename from languages/cpp/templates/parameters/result.c rename to languages/cpp/templates/parameters/result.h diff --git a/languages/cpp/templates/parameters/string.c b/languages/cpp/templates/parameters/string.h similarity index 100% rename from languages/cpp/templates/parameters/string.c rename to languages/cpp/templates/parameters/string.h diff --git a/languages/cpp/templates/types/additionalProperties.h b/languages/cpp/templates/types/additionalProperties.h index 52882d43..baa50800 100644 --- a/languages/cpp/templates/types/additionalProperties.h +++ b/languages/cpp/templates/types/additionalProperties.h @@ -1 +1 @@ -std::unordered_map title; \ No newline at end of file +${title} value; \ No newline at end of file diff --git a/languages/cpp/templates/types/array.h b/languages/cpp/templates/types/array.h index 2c1e61d4..ed5a1d2b 100644 --- a/languages/cpp/templates/types/array.h +++ b/languages/cpp/templates/types/array.h @@ -1 +1 @@ -std::vector<${type}> ${title}; \ No newline at end of file +std::vector<${type}> \ No newline at end of file diff --git a/languages/cpp/templates/types/null.h b/languages/cpp/templates/types/null.h index 79c36400..e69de29b 100644 --- a/languages/cpp/templates/types/null.h +++ b/languages/cpp/templates/types/null.h @@ -1 +0,0 @@ -nullptr \ No newline at end of file diff --git a/languages/cpp/templates/types/property.h b/languages/cpp/templates/types/property.h index 39c3b6b8..eb813df6 100644 --- a/languages/cpp/templates/types/property.h +++ b/languages/cpp/templates/types/property.h @@ -1 +1 @@ -${if.optional}std::optional<${end.if.optional}${title}${if.optional}>${end.if.optional} ${property}; // ${summary} \ No newline at end of file +${if.optional}std::optional<${end.if.optional}${title}${if.optional}>${end.if.optional} ${property}; \ No newline at end of file diff --git a/languages/cpp/templates/types/tuple.h b/languages/cpp/templates/types/tuple.h index 0e042b9c..ce217a83 100644 --- a/languages/cpp/templates/types/tuple.h +++ b/languages/cpp/templates/types/tuple.h @@ -1,6 +1,2 @@ /* ${title} */ -struct ${title} { - - ${properties} - -}; +std::pair<${type},${type}> ${title}; diff --git a/src/macrofier/engine.mjs b/src/macrofier/engine.mjs index c5055b91..500bc3b1 100644 --- a/src/macrofier/engine.mjs +++ b/src/macrofier/engine.mjs @@ -376,7 +376,7 @@ const promoteSchema = (location, property, title, document, destinationPath) => } // only consider sub-objects and sub enums to be sub-schemas -const isSubSchema = (schema) => schema.type === 'object' || (schema.type === 'string' && schema.enum) || (schema.type === 'array' && schema.items) +const isSubSchema = (schema) => schema.type === 'object' || (schema.type === 'string' && schema.enum) // || (schema.type === 'array' && schema.items) const promoteAndNameSubSchemas = (obj) => { // make a copy so we don't polute our inputs @@ -506,7 +506,7 @@ const generateMacros = (obj, templates, languages, options = {}) => { macros.types[dir] = getTemplate('/sections/types', templates).replace(/\$\{schema.list\}/g, schemasArray.filter(x => !x.enum).map(s => s.body).filter(body => body).join('\n')) macros.enums[dir] = getTemplate('/sections/enums', templates).replace(/\$\{schema.list\}/g, schemasArray.filter(x => x.enum).map(s => s.body).filter(body => body).join('\n')) }) - + state.typeTemplateDir = 'types' const examples = generateExamples(obj, templates, languages) const allMethodsArray = generateMethods(obj, examples, templates) const methodsArray = allMethodsArray.filter(m => m.body && !m.event && (!options.hideExcluded || !m.excluded)) @@ -1483,7 +1483,7 @@ function generateResultParams(result, json, templates, {name = ''}={}) { const template = getTemplate('/parameters/result', templates) return Object.entries(result.properties).map( ([name, type]) => template .replace(/\$\{method\.param\.name\}/g, name) - .replace(/\$\{method\.param\.type\}/g, types.getSchemaType(type, json, {name: name, moduleTitle: moduleTitle, result: true})) + .replace(/\$\{method\.param\.type\}/g, types.getSchemaType(type, json, {name: name, moduleTitle: moduleTitle, result: true, namespace: true})) ).join(', ') // most languages separate params w/ a comma, so leaving this here for now } // tuples get unwrapped @@ -1492,14 +1492,14 @@ function generateResultParams(result, json, templates, {name = ''}={}) { const template = getTemplate('/parameters/result', templates) return result.items.map( (type) => template .replace(/\$\{method\.param\.name\}/g, type['x-property']) - .replace(/\$\{method\.param\.type\}/g, types.getSchemaType(type, json, {name: name, moduleTitle: moduleTitle, result: true})) + .replace(/\$\{method\.param\.type\}/g, types.getSchemaType(type, json, {name: name, moduleTitle: moduleTitle, result: true, namespace: true})) ).join(', ') } // everything else is just output as-is else { - const template = getTemplate('/parameters/result', templates) - const type = types.getSchemaType(result, json, {name: name, moduleTitle: moduleTitle, result: true}) + const template = getTemplate('/parameters/result', templates) + const type = types.getSchemaType(result, json, {name: name, moduleTitle: moduleTitle, result: true, namespace: true}) if (type === 'undefined') { console.log(`Warning: undefined type for ${name}`) }