From 058c9713a8e398148d280859e42c4d8b1c47560c Mon Sep 17 00:00:00 2001 From: HaseenaSainul Date: Tue, 10 Oct 2023 00:46:57 -0400 Subject: [PATCH] CPPSDK: review comments updated --- .../cpp/templates/json-types/anyOfSchema.h | 1 - .../cpp/templates/json-types/anyOfSchemaShape.h | 1 + languages/cpp/templates/types/anyOfSchema.h | 1 - .../cpp/templates/types/anyOfSchemaShape.h | 1 + .../templates/types/tuple-delimiter.mjs | 1 + src/macrofier/engine.mjs | 2 +- src/macrofier/types.mjs | 17 +++++++---------- 7 files changed, 11 insertions(+), 13 deletions(-) delete mode 100644 languages/cpp/templates/json-types/anyOfSchema.h create mode 100644 languages/cpp/templates/json-types/anyOfSchemaShape.h delete mode 100644 languages/cpp/templates/types/anyOfSchema.h create mode 100644 languages/cpp/templates/types/anyOfSchemaShape.h create mode 100644 languages/javascript/templates/types/tuple-delimiter.mjs diff --git a/languages/cpp/templates/json-types/anyOfSchema.h b/languages/cpp/templates/json-types/anyOfSchema.h deleted file mode 100644 index cc2ecb63..00000000 --- a/languages/cpp/templates/json-types/anyOfSchema.h +++ /dev/null @@ -1 +0,0 @@ -SKIP diff --git a/languages/cpp/templates/json-types/anyOfSchemaShape.h b/languages/cpp/templates/json-types/anyOfSchemaShape.h new file mode 100644 index 00000000..9a53ff94 --- /dev/null +++ b/languages/cpp/templates/json-types/anyOfSchemaShape.h @@ -0,0 +1 @@ + /* anyOf schema shape is supported right now */ diff --git a/languages/cpp/templates/types/anyOfSchema.h b/languages/cpp/templates/types/anyOfSchema.h deleted file mode 100644 index cc2ecb63..00000000 --- a/languages/cpp/templates/types/anyOfSchema.h +++ /dev/null @@ -1 +0,0 @@ -SKIP diff --git a/languages/cpp/templates/types/anyOfSchemaShape.h b/languages/cpp/templates/types/anyOfSchemaShape.h new file mode 100644 index 00000000..9a53ff94 --- /dev/null +++ b/languages/cpp/templates/types/anyOfSchemaShape.h @@ -0,0 +1 @@ + /* anyOf schema shape is supported right now */ diff --git a/languages/javascript/templates/types/tuple-delimiter.mjs b/languages/javascript/templates/types/tuple-delimiter.mjs new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/languages/javascript/templates/types/tuple-delimiter.mjs @@ -0,0 +1 @@ + diff --git a/src/macrofier/engine.mjs b/src/macrofier/engine.mjs index 7c9e6ba9..80173a48 100644 --- a/src/macrofier/engine.mjs +++ b/src/macrofier/engine.mjs @@ -120,7 +120,7 @@ const getLinkForSchema = (schema, json, { name = '' } = {}) => { const dirs = config.createModuleDirectories const copySchemasIntoModules = config.copySchemasIntoModules - const type = types.getSchemaType(schema, json, { name: name, templateDir: state.typeTemplateDir, destination: state.destination, section: state.section }) + const type = types.getSchemaType(schema, json, { templateDir: state.typeTemplateDir, destination: state.destination, section: state.section }) // local - insert a bogus link, that we'll update later based on final table-of-contents if (json.components.schemas[type]) { diff --git a/src/macrofier/types.mjs b/src/macrofier/types.mjs index d12cc940..9035ddd0 100644 --- a/src/macrofier/types.mjs +++ b/src/macrofier/types.mjs @@ -142,7 +142,7 @@ function insertSchemaMacros(content, schema, module, name, parent, property, rec // .replace(/\$\{type.link\}/g, getLinkForSchema(schema, module, { name: title })) if (recursive) { - content = content.replace(/\$\{type\}/g, getSchemaType(schema, module, { name: title, destination: state.destination, section: state.section, code: false })) + content = content.replace(/\$\{type\}/g, getSchemaType(schema, module, { destination: state.destination, section: state.section, code: false })) } return content } @@ -282,7 +282,7 @@ const insertTupleMacros = (content, schema, module, title, options) => { const itemsTemplate = getTemplate(path.join(options.templateDir, 'items')) const propIndent = (content.split('\n').find(line => line.includes("${properties}")) || '').match(/^\s+/) || [''][0] const itemsIndent = (content.split('\n').find(line => line.includes("${items}")) || '').match(/^\s+/) || [''][0] - const delimiter = getTemplate(path.join(options.templateDir, 'delimiter')) + const tupleDelimiter = getTemplate(path.join(options.templateDir, 'tuple-delimiter')) const doMacroWork = (str, prop, i, indent) => { const schemaShape = getSchemaShape(prop, module, options) @@ -299,8 +299,8 @@ const insertTupleMacros = (content, schema, module, title, options) => { .replace(/\$\{if\.optional\}(.*?)\$\{end\.if\.optional\}/gms, '') } - content = content.replace(/\$\{properties\}/g, schema.items.map((prop, i) => doMacroWork(propTemplate, prop, i, propIndent)).join(delimiter)) - content = content.replace(/\$\{items\}/g, schema.items.map((prop, i) => doMacroWork(itemsTemplate, prop, i, itemsIndent)).join(delimiter)) + content = content.replace(/\$\{properties\}/g, schema.items.map((prop, i) => doMacroWork(propTemplate, prop, i, propIndent)).join(tupleDelimiter)) + content = content.replace(/\$\{items\}/g, schema.items.map((prop, i) => doMacroWork(itemsTemplate, prop, i, itemsIndent)).join(tupleDelimiter)) return content } @@ -394,12 +394,10 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', name return insertSchemaMacros(result, schema, module, theTitle, parent, property) } else if (schema.anyOf || schema.oneOf) { - const template = getTemplate(path.join(templateDir, 'anyOfSchema' + suffix)) + const template = getTemplate(path.join(templateDir, 'anyOfSchemaShape' + suffix)) let shape if (template) { - if (!template.includes('SKIP')) { shape = insertAnyOfMacros(template, schema, module, theTitle) - } } else { // borrow anyOf logic, note that schema is a copy, so we're not breaking it. @@ -512,7 +510,7 @@ const isSupportedTuple = schema => { } } -function getSchemaType(schema, module, { destination, templateDir = 'types', link = false, code = false, asPath = false, event = false, result = false, expandEnums = true, baseUrl = '', namespace = false, name = '' } = {}) { +function getSchemaType(schema, module, { destination, templateDir = 'types', link = false, code = false, asPath = false, event = false, result = false, expandEnums = true, baseUrl = '', namespace = false } = {}) { const wrap = (str, wrapper) => wrapper + str + wrapper schema = sanitize(schema) @@ -527,9 +525,8 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin if (schema['$ref']) { if (schema['$ref'][0] === '#') { const refSchema = getPath(schema['$ref'], module) - const refName = refSchema.title || schema['$ref'].split('/').pop() const includeNamespace = (module.info.title !== getXSchemaGroup(refSchema, module)) - return getSchemaType(refSchema, module, {destination, templateDir, link, title, code, asPath, event, result, expandEnums, baseUrl, namespace:includeNamespace, name:refName })// { link: link, code: code, destination }) + return getSchemaType(refSchema, module, {destination, templateDir, link, code, asPath, event, result, expandEnums, baseUrl, namespace:includeNamespace })// { link: link, code: code, destination }) } else { // TODO: This never happens... but might be worth keeping in case we link to an opaque external schema at some point?