From 5695efe54635faf910e719c46d2969afd4ae0976 Mon Sep 17 00:00:00 2001 From: HaseenaSainul <41037131+HaseenaSainul@users.noreply.github.com> Date: Wed, 18 Oct 2023 09:09:23 +0530 Subject: [PATCH 1/2] CPP SDK : changes to handle additional Properties (#134) CPP SDK : changes to handle additional Properties --- .../templates/types/additionalProperties.h | 2 +- .../templates/types/additionalPropertiesKey.h | 1 + src/macrofier/types.mjs | 101 ++++++++++++------ 3 files changed, 72 insertions(+), 32 deletions(-) create mode 100644 languages/cpp/templates/types/additionalPropertiesKey.h diff --git a/languages/cpp/templates/types/additionalProperties.h b/languages/cpp/templates/types/additionalProperties.h index baa50800..05581106 100644 --- a/languages/cpp/templates/types/additionalProperties.h +++ b/languages/cpp/templates/types/additionalProperties.h @@ -1 +1 @@ -${title} value; \ No newline at end of file +std::unordered_map<${key}, ${type}> ${title}; diff --git a/languages/cpp/templates/types/additionalPropertiesKey.h b/languages/cpp/templates/types/additionalPropertiesKey.h new file mode 100644 index 00000000..9c1a95b6 --- /dev/null +++ b/languages/cpp/templates/types/additionalPropertiesKey.h @@ -0,0 +1 @@ +std::string diff --git a/src/macrofier/types.mjs b/src/macrofier/types.mjs index d6e53788..7c627fce 100644 --- a/src/macrofier/types.mjs +++ b/src/macrofier/types.mjs @@ -30,6 +30,7 @@ const primitives = { "string": "string" } +const isPrimitiveType = type => primitives[type] ? true : false const allocatedPrimitiveProxies = {} function setTemplates(t) { @@ -109,7 +110,7 @@ function insertSchemaMacros(content, schema, module, name, parent, property, rec .replace(/\$\{info.TITLE\}/g, moduleTitle.toUpperCase()) if (recursive) { - content = content.replace(/\$\{type\}/g, getSchemaType(schema, module, { name: title, destination: state.destination, section: state.section, code: false, namespace: true })) + content = content.replace(/\$\{type\}/g, getSchemaType(schema, module, { destination: state.destination, section: state.section, code: false, namespace: true })) } return content } @@ -137,13 +138,40 @@ const insertEnumMacros = (content, schema, module, name) => { return template.join('\n') } -const insertObjectMacros = (content, schema, module, title, property, options) => { - options = options ? JSON.parse(JSON.stringify(options)) : {} +const insertObjectAdditionalPropertiesMacros = (content, schema, module, title, options) => { + const options2 = options ? JSON.parse(JSON.stringify(options)) : {} + options2.parent = title + options2.level = options.level + 1 + + const shape = getSchemaShape(schema.additionalProperties, module, options2) + let type = getSchemaType(schema.additionalProperties, module, options2).trimEnd() + const propertyNames = localizeDependencies(schema, module).propertyNames + + let jsonType = getJsonType(schema.additionalProperties, module) + if (!isPrimitiveType(jsonType)) { + jsonType = 'string' + } + + const additionalType = getPrimitiveType(jsonType, 'additional-types') + let key = (propertyNames && propertyNames.title) ? propertyNames.title : getTemplate(path.join(options.templateDir, 'additionalPropertiesKey')).trimEnd() + + content = content + .replace(/\$\{shape\}/g, shape) + .replace(/\$\{parent\.title\}/g, title) + .replace(/\$\{title\}/g, title) + .replace(/\$\{type\}/g, type) + .replace(/\$\{additional\.type\}/g, additionalType) + .replace(/\$\{key\}/g, key) + .replace(/\$\{delimiter\}(.*?)\$\{end.delimiter\}/g, '') + .replace(/\$\{if\.optional\}(.*?)\$\{end\.if\.optional\}/g, '') - const options2 = JSON.parse(JSON.stringify(options)) + return content +} + +const insertObjectMacros = (content, schema, module, title, property, options) => { + const options2 = options ? JSON.parse(JSON.stringify(options)) : {} options2.parent = title options2.level = options.level + 1 - options2.name = '' ;(['properties', 'properties.register', 'properties.assign']).forEach(macro => { const indent = (content.split('\n').find(line => line.includes("${" + macro + "}")) || '').match(/^\s+/) || [''][0] @@ -151,21 +179,7 @@ const insertObjectMacros = (content, schema, module, title, property, options) = const template = getTemplate(path.join(options.templateDir, 'property' + (templateType ? `-${templateType}` : ''))).replace(/\n/gms, indent + '\n') const properties = [] - - if (schema.additionalProperties && (typeof schema.additionalProperties === 'object')) { - const template = getTemplate(path.join(options.templateDir, 'additionalProperties')) - let type = getSchemaType(schema.additionalProperties, module, options2) - const shape = getSchemaShape(schema.additionalProperties, module, options2) - properties.push(template - .replace(/\$\{property\}/g, '') - .replace(/\$\{Property\}/g, '') - .replace(/\$\{shape\}/g, shape) - .replace(/\$\{parent\.title\}/g, title) - .replace(/\$\{title\}/g, type) - .replace(/\$\{delimiter\}(.*?)\$\{end.delimiter\}/g, '') - .replace(/\$\{if\.optional\}(.*?)\$\{end\.if\.optional\}/g, '')) - } - + if (schema.properties) { Object.entries(schema.properties).forEach(([name, prop], i) => { options2.property = name @@ -319,7 +333,7 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', name if (level === 0 && !schema.title) { return '' } - + const suffix = destination && ('.' + destination.split('.').pop()) || '' const theTitle = insertSchemaMacros(getTemplate(path.join(templateDir, 'title' + suffix)), schema, module, schema.title || name, parent, property, false) @@ -353,8 +367,14 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', name return insertSchemaMacros(result, schema, module, theTitle, parent, property) } else if (schema.type === 'object') { - const shape = insertObjectMacros(getTemplate(path.join(templateDir, 'object' + suffix)), schema, module, theTitle, property, { level, parent, property, templateDir, descriptions, destination, section, enums }) - + let shape + const additionalPropertiesTemplate = getTemplate(path.join(templateDir, 'additionalProperties')) + if (additionalPropertiesTemplate && schema.additionalProperties && (typeof schema.additionalProperties === 'object')) { + shape = insertObjectAdditionalPropertiesMacros(additionalPropertiesTemplate, schema, module, theTitle, { level, parent, templateDir, namespace: true }) + } + else { + shape = insertObjectMacros(getTemplate(path.join(templateDir, 'object' + suffix)), schema, module, theTitle, property, { level, parent, property, templateDir, descriptions, destination, section, enums, namespace: true }) + } result = result.replace(/\$\{shape\}/g, shape) return insertSchemaMacros(result, schema, module, theTitle, parent, property) } @@ -614,11 +634,17 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin // } } else if (schema.type) { - // TODO: this assumes that when type is an array of types, that it's one other primative & 'null', which isn't necessarily true. - const schemaType = !Array.isArray(schema.type) ? schema.type : schema.type.find(t => t !== 'null') - const primitive = getPrimitiveType(schemaType, templateDir) - const type = allocatedProxy ? allocatedPrimitiveProxies[schemaType] || primitive : primitive - return wrap(type, code ? '`' : '') + const template = getTemplate(path.join(templateDir, 'additionalProperties')) + if (schema.additionalProperties && template ) { + return insertSchemaMacros(getTemplate(path.join(templateDir, 'Title')), schema, module, theTitle, '', '', false) + } + else { + // TODO: this assumes that when type is an array of types, that it's one other primative & 'null', which isn't necessarily true. + const schemaType = !Array.isArray(schema.type) ? schema.type : schema.type.find(t => t !== 'null') + const primitive = getPrimitiveType(schemaType, templateDir) + const type = allocatedProxy ? allocatedPrimitiveProxies[schemaType] || primitive : primitive + return wrap(type, code ? '`' : '') + } } else { // TODO this is TypeScript specific @@ -627,7 +653,21 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin } function getJsonType(schema, module, { destination, link = false, title = false, code = false, asPath = false, event = false, expandEnums = true, baseUrl = '' } = {}) { - return '' + + schema = sanitize(schema) + let type + if (schema['$ref']) { + if (schema['$ref'][0] === '#') { + //Ref points to local schema + //Get Path to ref in this module and getSchemaType + let definition = getPath(schema['$ref'], module) + type = getJsonType(definition, schema, {destination}) + } + } + else { + type = !Array.isArray(schema.type) ? schema.type : schema.type.find(t => t !== 'null') + } + return type } function getSchemaInstantiation(schema, module, { instantiationType }) { @@ -642,6 +682,5 @@ export default { getMethodSignatureParams, getSchemaShape, getSchemaType, - getJsonType, getSchemaInstantiation -} \ No newline at end of file +} From 1f2854bea633b01ac554367bac22ad0ba6f92f31 Mon Sep 17 00:00:00 2001 From: HaseenaSainul <41037131+HaseenaSainul@users.noreply.github.com> Date: Wed, 18 Oct 2023 09:12:02 +0530 Subject: [PATCH 2/2] CPPSDK: parameter and result handling changes (#136) CPPSDK: 1. parameter and result handling changes 2. anyOf type handling 3. reference schema handling --- languages/cpp/templates/json-types/anyOf.c | 1 - languages/cpp/templates/json-types/anyOf.h | 1 + .../templates/json-types/anyOfSchemaShape.h | 1 + .../parameter-serialization/generic.cpp | 2 + .../parameters/{object.h => nonprimitive.h} | 0 .../cpp/templates/parameters/result-null.h | 0 languages/cpp/templates/parameters/result.h | 1 - .../cpp/templates/result-callback/default.h | 1 + .../templates/result-callback/nonprimitive.h | 1 + .../cpp/templates/result-callback/string.h | 1 + languages/cpp/templates/result/default.h | 1 + languages/cpp/templates/types/anyOf.c | 1 - languages/cpp/templates/types/anyOf.h | 1 + .../cpp/templates/types/anyOfSchemaShape.h | 1 + languages/cpp/templates/types/tuple.h | 2 +- .../templates/types/tuple-delimiter.mjs | 1 + src/macrofier/engine.mjs | 10 ++- src/macrofier/types.mjs | 88 +++++++++++++++---- 18 files changed, 87 insertions(+), 27 deletions(-) delete mode 100644 languages/cpp/templates/json-types/anyOf.c create mode 100644 languages/cpp/templates/json-types/anyOf.h create mode 100644 languages/cpp/templates/json-types/anyOfSchemaShape.h create mode 100644 languages/cpp/templates/parameter-serialization/generic.cpp rename languages/cpp/templates/parameters/{object.h => nonprimitive.h} (100%) delete mode 100644 languages/cpp/templates/parameters/result-null.h delete mode 100644 languages/cpp/templates/parameters/result.h create mode 100644 languages/cpp/templates/result-callback/default.h create mode 100644 languages/cpp/templates/result-callback/nonprimitive.h create mode 100644 languages/cpp/templates/result-callback/string.h create mode 100644 languages/cpp/templates/result/default.h delete mode 100644 languages/cpp/templates/types/anyOf.c create mode 100644 languages/cpp/templates/types/anyOf.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/anyOf.c b/languages/cpp/templates/json-types/anyOf.c deleted file mode 100644 index a2682179..00000000 --- a/languages/cpp/templates/json-types/anyOf.c +++ /dev/null @@ -1 +0,0 @@ -/* AnyOf is not supported in C: ${title} */ \ No newline at end of file diff --git a/languages/cpp/templates/json-types/anyOf.h b/languages/cpp/templates/json-types/anyOf.h new file mode 100644 index 00000000..fc121f63 --- /dev/null +++ b/languages/cpp/templates/json-types/anyOf.h @@ -0,0 +1 @@ +WPEFramework::Core::JSON::VariantContainer \ No newline at end of file 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/parameter-serialization/generic.cpp b/languages/cpp/templates/parameter-serialization/generic.cpp new file mode 100644 index 00000000..8ccb8295 --- /dev/null +++ b/languages/cpp/templates/parameter-serialization/generic.cpp @@ -0,0 +1,2 @@ + WPEFramework::Core::JSON::Variant ${Property} = ${property}; + jsonParameters.Set(_T("${property}"), ${Property}); \ No newline at end of file diff --git a/languages/cpp/templates/parameters/object.h b/languages/cpp/templates/parameters/nonprimitive.h similarity index 100% rename from languages/cpp/templates/parameters/object.h rename to languages/cpp/templates/parameters/nonprimitive.h diff --git a/languages/cpp/templates/parameters/result-null.h b/languages/cpp/templates/parameters/result-null.h deleted file mode 100644 index e69de29b..00000000 diff --git a/languages/cpp/templates/parameters/result.h b/languages/cpp/templates/parameters/result.h deleted file mode 100644 index 3c96807b..00000000 --- a/languages/cpp/templates/parameters/result.h +++ /dev/null @@ -1 +0,0 @@ -${method.param.type}& ${method.param.name} \ No newline at end of file diff --git a/languages/cpp/templates/result-callback/default.h b/languages/cpp/templates/result-callback/default.h new file mode 100644 index 00000000..d66fdb8a --- /dev/null +++ b/languages/cpp/templates/result-callback/default.h @@ -0,0 +1 @@ +const ${method.result.type} \ No newline at end of file diff --git a/languages/cpp/templates/result-callback/nonprimitive.h b/languages/cpp/templates/result-callback/nonprimitive.h new file mode 100644 index 00000000..84b86816 --- /dev/null +++ b/languages/cpp/templates/result-callback/nonprimitive.h @@ -0,0 +1 @@ +const ${method.result.type}& \ No newline at end of file diff --git a/languages/cpp/templates/result-callback/string.h b/languages/cpp/templates/result-callback/string.h new file mode 100644 index 00000000..84b86816 --- /dev/null +++ b/languages/cpp/templates/result-callback/string.h @@ -0,0 +1 @@ +const ${method.result.type}& \ No newline at end of file diff --git a/languages/cpp/templates/result/default.h b/languages/cpp/templates/result/default.h new file mode 100644 index 00000000..d66fdb8a --- /dev/null +++ b/languages/cpp/templates/result/default.h @@ -0,0 +1 @@ +const ${method.result.type} \ No newline at end of file diff --git a/languages/cpp/templates/types/anyOf.c b/languages/cpp/templates/types/anyOf.c deleted file mode 100644 index bd2ba3e5..00000000 --- a/languages/cpp/templates/types/anyOf.c +++ /dev/null @@ -1 +0,0 @@ -/* AnyOf is not supported in CPP: ${title} */ \ No newline at end of file diff --git a/languages/cpp/templates/types/anyOf.h b/languages/cpp/templates/types/anyOf.h new file mode 100644 index 00000000..c6d1c815 --- /dev/null +++ b/languages/cpp/templates/types/anyOf.h @@ -0,0 +1 @@ +std::string \ No newline at end of file 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/cpp/templates/types/tuple.h b/languages/cpp/templates/types/tuple.h index ce217a83..b00d2610 100644 --- a/languages/cpp/templates/types/tuple.h +++ b/languages/cpp/templates/types/tuple.h @@ -1,2 +1,2 @@ /* ${title} */ -std::pair<${type},${type}> ${title}; +std::pair<${items}> ${title}; 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 500bc3b1..b5527ac6 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]) { @@ -844,7 +844,7 @@ function generateSchemas(json, templates, options) { else { content = content.replace(/.*\$\{schema.seeAlso\}/, '') } -// content = content.trim().length ? content.trimEnd() : content.trim() + content = content.trim().length ? content : content.trim() const isEnum = x => x.type === 'string' && Array.isArray(x.enum) && x.title @@ -1207,7 +1207,7 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {}) // todo: what does prefix do in Types.mjs? need to account for it somehow const callbackResultJsonType = event && result.schema ? types.getSchemaType(result.schema, json, { name: result.name, prefix: method.alternative, templateDir: 'json-types' }) : '' - const pullsForParamType = pullsParams ? types.getSchemaType(pullsParams, json, { destination: state.destination, section: state.section }) : '' + const pullsForParamType = pullsParams ? types.getSchemaType(pullsParams, json, { destination: state.destination, section: state.section }) : '' const pullsForJsonType = pullsResult ? types.getSchemaType(pullsResult, json, { name: result.name, templateDir: 'json-types' }) : '' const pullsForParamJsonType = pullsParams ? types.getSchemaType(pullsParams, json, { name: pullsParams.title , templateDir: 'json-types' }) : '' @@ -1245,6 +1245,7 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {}) .replace(/\$\{method\.params\.count}/g, methodObj.params ? methodObj.params.length : 0) .replace(/\$\{if\.params\}(.*?)\$\{end\.if\.params\}/gms, method.params.length ? '$1' : '') .replace(/\$\{if\.result\}(.*?)\$\{end\.if\.result\}/gms, resultType ? '$1' : '') + .replace(/\$\{if\.result.nonvoid\}(.*?)\$\{end\.if\.result.nonvoid\}/gms, resultType && resultType !== 'void' ? '$1' : '') .replace(/\$\{if\.result\.properties\}(.*?)\$\{end\.if\.result\.properties\}/gms, resultParams ? '$1' : '') .replace(/\$\{if\.params\.empty\}(.*?)\$\{end\.if\.params\.empty\}/gms, method.params.length === 0 ? '$1' : '') .replace(/\$\{if\.signature\.empty\}(.*?)\$\{end\.if\.signature\.empty\}/gms, (method.params.length === 0 && resultType === '') ? '$1' : '') @@ -1253,6 +1254,7 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {}) .replace(/\$\{method\.params\.serialization\.with\.indent\}/g, indent(serializedParams, ' ')) // Typed signature stuff .replace(/\$\{method\.signature\.params\}/g, types.getMethodSignatureParams(methodObj, json, { destination: state.destination, section: state.section })) + .replace(/\$\{method\.signature\.result\}/g, types.getMethodSignatureResult(methodObj, json, { destination: state.destination, section: state.section })) .replace(/\$\{method\.context\}/g, method.context.join(', ')) .replace(/\$\{method\.context\.array\}/g, JSON.stringify(method.context)) .replace(/\$\{method\.context\.count}/g, method.context ? method.context.length : 0) @@ -1287,7 +1289,7 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {}) .replace(/\$\{method\.result\.type\}/g, types.getSchemaType(result.schema, json, { name: result.name, templateDir: state.typeTemplateDir, title: true, asPath: false, destination: state.destination, result: true })) //, baseUrl: options.baseUrl .replace(/\$\{method\.result\.json\}/, types.getSchemaType(result.schema, json, { name: result.name, templateDir: 'json-types', destination: state.destination, section: state.section, title: true, code: false, link: false, asPath: false, expandEnums: false, namespace: true })) // todo: what does prefix do? - .replace(/\$\{event\.result\.type\}/, isEventMethod(methodObj) ? types.getSchemaType(result.schema, json, { name: result.name, prefix: method.alternative, templateDir: state.typeTemplateDir, destination: state.destination, event: true, description: methodObj.result.summary, asPath: false }) : '') + .replace(/\$\{event\.result\.type\}/g, isEventMethod(methodObj) ? types.getMethodSignatureResult(event, json, { destination: state.destination, section: state.section, callback: true }) : '') .replace(/\$\{event\.result\.json\.type\}/g, resultJsonType) .replace(/\$\{event\.result\.json\.type\}/g, callbackResultJsonType) .replace(/\$\{event\.pulls\.param\.name\}/g, pullsEventParamName) diff --git a/src/macrofier/types.mjs b/src/macrofier/types.mjs index 7c627fce..6ee24f2a 100644 --- a/src/macrofier/types.mjs +++ b/src/macrofier/types.mjs @@ -30,6 +30,7 @@ const primitives = { "string": "string" } +const isVoid = type => (type === 'void') ? true : false const isPrimitiveType = type => primitives[type] ? true : false const allocatedPrimitiveProxies = {} @@ -54,17 +55,52 @@ const safeName = value => value.split(':').pop().replace(/[\.\-]/g, '_').replace // TODO: This is what's left of getMethodSignatureParams. We need to figure out / handle C's `FireboltTypes_StringHandle` function getMethodSignatureParams(method, module, { destination, callback }) { - const paramRequired = getTemplate('/parameters/default') const paramOptional = getTemplate('/parameters/optional') return method.params.map(param => { let type = getSchemaType(param.schema, module, { destination, namespace : true }) if (callback && allocatedPrimitiveProxies[type]) { type = allocatedPrimitiveProxies[type] } + + let paramRequired = '' + let jsonType = getJsonType(param.schema, module, { destination }) + if (!isPrimitiveType(jsonType) && getTemplate('/parameters/nonprimitive')) { + paramRequired = getTemplate('/parameters/nonprimitive') + } + else if ((jsonType === 'string') && getTemplate('/parameters/string')) { + paramRequired = getTemplate('/parameters/string') + } + else { + paramRequired = getTemplate('/parameters/default') + } + return (param.required ? paramRequired : paramOptional).replace(/\$\{method\.param\.name\}/g, param.name).replace(/\$\{method\.param\.type\}/g, type) }).join(', ') } +function getMethodSignatureResult(method, module, { destination, callback, overrideRule = false }) { + let type = getSchemaType(method.result.schema, module, { destination, namespace : true }) + let result = '' + + if (callback) { + let jsonType = getJsonType(method.result.schema, module, { destination }) + + if (!isVoid(type) && !isPrimitiveType(jsonType) && getTemplate('/result-callback/nonprimitive')) { + result = getTemplate('/result-callback/nonprimitive') + } + else if ((jsonType === 'string') && getTemplate('/result-callback/string')) { + result = getTemplate('/result-callback/string') + } + else { + result = getTemplate('/result-callback/default') + } + } + else { + result = getTemplate('/result/default') + } + return result.replace(/\$\{method\.result\.type\}/g, type) +} + const getTemplate = (name) => { if (name[0] !== '/') { name = '/' + name @@ -121,8 +157,6 @@ const insertConstMacros = (content, schema, module, name) => { return content } - - const insertEnumMacros = (content, schema, module, name) => { const template = content.split('\n') @@ -213,7 +247,7 @@ const insertObjectMacros = (content, schema, module, title, property, options) = } // TODO: add language config feature for 'unknown' type let type; // = { type: "null" } - + if (schema.additionalProperties && (typeof schema.additionalProperties === 'object')) { type = schema.additionalProperties } @@ -226,7 +260,7 @@ const insertObjectMacros = (content, schema, module, title, property, options) = } }) } - + if (type) { options2.property = prop const schemaShape = getSchemaShape(type, module, options2) @@ -265,6 +299,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 tupleDelimiter = getTemplate(path.join(options.templateDir, 'tuple-delimiter')) const doMacroWork = (str, prop, i, indent) => { const schemaShape = getSchemaShape(prop, module, options) @@ -281,8 +316,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('\n')) - content = content.replace(/\$\{items\}/g, schema.items.map((prop, i) => doMacroWork(itemsTemplate, prop, i, itemsIndent)).join('\n')) + 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 } @@ -301,10 +336,13 @@ const insertPrimitiveMacros = (content, schema, module, name, templateDir) => { const insertAnyOfMacros = (content, schema, module, name) => { const itemTemplate = content - content = schema.anyOf.map((item, i) => itemTemplate - .replace(/\$\{type\}/g, getSchemaType(item, module)) - .replace(/\$\{delimiter\}(.*?)\$\{end.delimiter\}/g, i === schema.anyOf.length - 1 ? '' : '$1') - ).join('') + if (content.split('\n').find(line => line.includes("${type}"))) { + content = schema.anyOf.map((item, i) => itemTemplate + .replace(/\$\{type\}/g, getSchemaType(item, module)) + .replace(/\$\{delimiter\}(.*?)\$\{end.delimiter\}/g, i === schema.anyOf.length - 1 ? '' : '$1') + ).join('') + } + return content } @@ -379,14 +417,25 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', name return insertSchemaMacros(result, schema, module, theTitle, parent, property) } else if (schema.anyOf || schema.oneOf) { - // borrow anyOf logic, note that schema is a copy, so we're not breaking it. - if (!schema.anyOf) { - schema.anyOf = schema.oneOf + const template = getTemplate(path.join(templateDir, 'anyOfSchemaShape' + suffix)) + let shape + if (template) { + shape = insertAnyOfMacros(template, schema, module, theTitle) + } + else { + // borrow anyOf logic, note that schema is a copy, so we're not breaking it. + if (!schema.anyOf) { + schema.anyOf = schema.oneOf + } + shape = insertAnyOfMacros(getTemplate(path.join(templateDir, 'anyOf' + suffix)), schema, module, theTitle) + } + if (shape) { + result = result.replace(/\$\{shape\}/g, shape) + return insertSchemaMacros(result, schema, module, theTitle, parent, property) + } + else { + return '' } - const shape = insertAnyOfMacros(getTemplate(path.join(templateDir, 'anyOf' + suffix)), schema, module, theTitle) - - result = result.replace(/\$\{shape\}/g, shape) - return insertSchemaMacros(result, schema, module, theTitle, parent, property) } else if (schema.allOf) { const merger = (key) => function (a, b) { @@ -500,7 +549,7 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin if (schema['$ref'][0] === '#') { const refSchema = getPath(schema['$ref'], module) const includeNamespace = (module.info.title !== getXSchemaGroup(refSchema, module)) - return getSchemaType(refSchema, module, {destination, templateDir, link, title, code, asPath, event, result, expandEnums, baseUrl, namespace:includeNamespace })// { 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? @@ -680,6 +729,7 @@ export default { setConvertTuples, setAllocatedPrimitiveProxies, getMethodSignatureParams, + getMethodSignatureResult, getSchemaShape, getSchemaType, getSchemaInstantiation