diff --git a/languages/cpp/templates/additional-types/boolean.cpp b/languages/cpp/templates/additional-types/boolean.cpp new file mode 100644 index 00000000..feef819e --- /dev/null +++ b/languages/cpp/templates/additional-types/boolean.cpp @@ -0,0 +1 @@ +Boolean() \ No newline at end of file diff --git a/languages/cpp/templates/additional-types/integer.cpp b/languages/cpp/templates/additional-types/integer.cpp new file mode 100644 index 00000000..15c659a4 --- /dev/null +++ b/languages/cpp/templates/additional-types/integer.cpp @@ -0,0 +1 @@ +Number() \ No newline at end of file diff --git a/languages/cpp/templates/additional-types/number.cpp b/languages/cpp/templates/additional-types/number.cpp new file mode 100644 index 00000000..a5d368a4 --- /dev/null +++ b/languages/cpp/templates/additional-types/number.cpp @@ -0,0 +1 @@ +Float() \ No newline at end of file diff --git a/languages/cpp/templates/additional-types/string.cpp b/languages/cpp/templates/additional-types/string.cpp new file mode 100644 index 00000000..d2a52c84 --- /dev/null +++ b/languages/cpp/templates/additional-types/string.cpp @@ -0,0 +1 @@ +String() \ No newline at end of file diff --git a/languages/cpp/templates/json-types/default.cpp b/languages/cpp/templates/json-types/default.h similarity index 100% rename from languages/cpp/templates/json-types/default.cpp rename to languages/cpp/templates/json-types/default.h diff --git a/languages/cpp/templates/json-types/null.h b/languages/cpp/templates/json-types/null.h new file mode 100644 index 00000000..fc121f63 --- /dev/null +++ b/languages/cpp/templates/json-types/null.h @@ -0,0 +1 @@ +WPEFramework::Core::JSON::VariantContainer \ No newline at end of file diff --git a/languages/cpp/templates/json-types/void.cpp b/languages/cpp/templates/json-types/void.cpp new file mode 100644 index 00000000..fc121f63 --- /dev/null +++ b/languages/cpp/templates/json-types/void.cpp @@ -0,0 +1 @@ +WPEFramework::Core::JSON::VariantContainer \ No newline at end of file diff --git a/languages/cpp/templates/parameter-serialization/array.cpp b/languages/cpp/templates/parameter-serialization/array.cpp index 43b6b532..d45916ab 100644 --- a/languages/cpp/templates/parameter-serialization/array.cpp +++ b/languages/cpp/templates/parameter-serialization/array.cpp @@ -1,6 +1,6 @@ WPEFramework::Core::JSON::ArrayType ${Property}; - for (auto& element: ${property}) { - ${Property}.Add() = element; + for (auto& element : ${property}) { +${if.object}${items.with.indent}${end.if.object}${if.non.object}${Property}.Add() = element;${end.if.non.object} } WPEFramework::Core::JSON::Variant ${Property}Variant; ${Property}Variant.Array(${Property}); diff --git a/languages/cpp/templates/parameter-serialization/boolean.cpp b/languages/cpp/templates/parameter-serialization/boolean.cpp deleted file mode 100644 index f924ac94..00000000 --- a/languages/cpp/templates/parameter-serialization/boolean.cpp +++ /dev/null @@ -1,2 +0,0 @@ - WPEFramework::Core::JSON::Variant ${Property} = ${property}; - jsonParameters.Set(_T("${property}"), ${Property}); diff --git a/languages/cpp/templates/parameter-serialization/default.cpp b/languages/cpp/templates/parameter-serialization/default.cpp deleted file mode 100644 index f924ac94..00000000 --- a/languages/cpp/templates/parameter-serialization/default.cpp +++ /dev/null @@ -1,2 +0,0 @@ - WPEFramework::Core::JSON::Variant ${Property} = ${property}; - jsonParameters.Set(_T("${property}"), ${Property}); diff --git a/languages/cpp/templates/parameter-serialization/enum.cpp b/languages/cpp/templates/parameter-serialization/enum.cpp index cb20df92..2367c118 100644 --- a/languages/cpp/templates/parameter-serialization/enum.cpp +++ b/languages/cpp/templates/parameter-serialization/enum.cpp @@ -1,3 +1,3 @@ - ${if.namespace.notsame}Firebolt::${info.Title}::${end.if.namespace.notsame}JsonData_${title} jsonValue = ${property}; + ${if.namespace.notsame}Firebolt::${info.Title}::${end.if.namespace.notsame}JsonData_${title} jsonValue = ${property}${if.optional}.value()${end.if.optional}; WPEFramework::Core::JSON::Variant ${Property}(jsonValue.Data()); jsonParameters.Set(_T("${property}"), ${Property}); \ No newline at end of file diff --git a/languages/cpp/templates/parameter-serialization/generic.cpp b/languages/cpp/templates/parameter-serialization/generic.cpp index 5bf50bc5..566c7354 100644 --- a/languages/cpp/templates/parameter-serialization/generic.cpp +++ b/languages/cpp/templates/parameter-serialization/generic.cpp @@ -1,2 +1,2 @@ - WPEFramework::Core::JSON::Variant ${Property}(${property}); - jsonParameters.Set(_T("${property}"), ${Property}); \ No newline at end of file + WPEFramework::Core::JSON::Variant ${Property}(${property}${if.optional}.value()${end.if.optional}); + jsonParameters.Set(_T("${property}"), ${Property}); diff --git a/languages/cpp/templates/parameter-serialization/object-array.cpp b/languages/cpp/templates/parameter-serialization/object-array.cpp new file mode 100644 index 00000000..0965afae --- /dev/null +++ b/languages/cpp/templates/parameter-serialization/object-array.cpp @@ -0,0 +1,7 @@ + ${if.namespace.notsame}Firebolt::${info.Title}::${end.if.namespace.notsame}JsonData_${title} ${Property}Container; +${properties} + string ${Property}Str; + ${Property}Container.ToString(${Property}Str); + WPEFramework::Core::JSON::VariantContainer ${Property}VariantContainer(${Property}Str); + WPEFramework::Core::JSON::Variant ${Property}Variant = ${Property}VariantContainer; + ${Property}.Add() = ${Property}Variant; diff --git a/languages/cpp/templates/parameter-serialization/object.cpp b/languages/cpp/templates/parameter-serialization/object.cpp index 20340a12..42226921 100644 --- a/languages/cpp/templates/parameter-serialization/object.cpp +++ b/languages/cpp/templates/parameter-serialization/object.cpp @@ -1,3 +1,4 @@ + auto element = ${property}; ${if.namespace.notsame}Firebolt::${info.Title}::${end.if.namespace.notsame}JsonData_${title} ${Property}Container; ${properties} string ${Property}Str; diff --git a/languages/cpp/templates/parameter-serialization/property.cpp b/languages/cpp/templates/parameter-serialization/property.cpp index f019a685..7036ea07 100644 --- a/languages/cpp/templates/parameter-serialization/property.cpp +++ b/languages/cpp/templates/parameter-serialization/property.cpp @@ -1 +1 @@ -${shape} ${if.non.object}${title}.${Property} = ${base.title}.${property};${end.if.non.object} +${shape} ${if.non.object}${base.Title}Container.${Property} = element${if.base.optional}.value()${end.if.base.optional}.${property}${if.optional}.value()${end.if.optional};${end.if.non.object} \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/default.cpp b/languages/cpp/templates/parameter-serialization/ref.h similarity index 100% rename from languages/cpp/templates/result-instantiation/default.cpp rename to languages/cpp/templates/parameter-serialization/ref.h diff --git a/languages/cpp/templates/parameter-serialization/string.cpp b/languages/cpp/templates/parameter-serialization/string.cpp deleted file mode 100644 index f924ac94..00000000 --- a/languages/cpp/templates/parameter-serialization/string.cpp +++ /dev/null @@ -1,2 +0,0 @@ - WPEFramework::Core::JSON::Variant ${Property} = ${property}; - jsonParameters.Set(_T("${property}"), ${Property}); diff --git a/languages/cpp/templates/parameter-serialization/sub-property/property.cpp b/languages/cpp/templates/parameter-serialization/sub-property/property.cpp index c19bd088..58aa5943 100644 --- a/languages/cpp/templates/parameter-serialization/sub-property/property.cpp +++ b/languages/cpp/templates/parameter-serialization/sub-property/property.cpp @@ -1 +1 @@ -${shape} ${if.non.object}${title}.${Property.dependency}${Property} = ${base.title}.${property.dependency}${property};${end.if.non.object} +${shape} ${if.non.object}${title}.${Property.dependency}${Property} = element${if.base.optional}.value()${end.if.base.optional}.${property.dependency}${property}${if.optional}.value()${end.if.optional};${end.if.non.object} \ No newline at end of file diff --git a/languages/cpp/templates/result-initialization/additionalProperties.cpp b/languages/cpp/templates/result-initialization/additionalProperties.cpp new file mode 100644 index 00000000..c33c2ea0 --- /dev/null +++ b/languages/cpp/templates/result-initialization/additionalProperties.cpp @@ -0,0 +1 @@ + ${Title} ${property}; \ No newline at end of file diff --git a/languages/cpp/templates/result-initialization/array.cpp b/languages/cpp/templates/result-initialization/array.cpp new file mode 100644 index 00000000..2f0629ac --- /dev/null +++ b/languages/cpp/templates/result-initialization/array.cpp @@ -0,0 +1 @@ + ${type} ${property}; diff --git a/languages/cpp/templates/result-instantiation/additionalProperties.cpp b/languages/cpp/templates/result-instantiation/additionalProperties.cpp index af41c4a6..e5d7b116 100644 --- a/languages/cpp/templates/result-instantiation/additionalProperties.cpp +++ b/languages/cpp/templates/result-instantiation/additionalProperties.cpp @@ -1,3 +1,6 @@ while (jsonResult.Variants().Next()) { - ${property}.insert(elements.Label(), elements.Current().${additional.type}.Value()); + std::string label = jsonResult.Variants().Label(); + ${property}.emplace(std::piecewise_construct, + std::forward_as_tuple(label), + std::forward_as_tuple(jsonResult.Variants().Current().${additional.type})); } \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/array.cpp b/languages/cpp/templates/result-instantiation/array.cpp index 464dea4a..2fab5f07 100644 --- a/languages/cpp/templates/result-instantiation/array.cpp +++ b/languages/cpp/templates/result-instantiation/array.cpp @@ -1,4 +1,4 @@ auto index(jsonResult.Elements()); while (index.Next() == true) { - ${property}.push_back(index.Current().Value()); - } +${if.object}${items.with.indent}${end.if.object}${if.non.object} ${property}.push_back(index.Current().Value());${end.if.non.object} + } \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/boolean.cpp b/languages/cpp/templates/result-instantiation/boolean.cpp deleted file mode 100644 index 036b4a7d..00000000 --- a/languages/cpp/templates/result-instantiation/boolean.cpp +++ /dev/null @@ -1,2 +0,0 @@ - *${property} = jsonResult.Value(); - \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/object-array.cpp b/languages/cpp/templates/result-instantiation/object-array.cpp new file mode 100644 index 00000000..0c396b43 --- /dev/null +++ b/languages/cpp/templates/result-instantiation/object-array.cpp @@ -0,0 +1,4 @@ + ${type} ${property}Result${level}; + ${if.namespace.notsame}Firebolt::${info.Title}::${end.if.namespace.notsame}JsonData_${title} jsonResult = index.Current(); +${properties} + ${property}.push_back(${property}Result${level}); \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/object.cpp b/languages/cpp/templates/result-instantiation/object.cpp index c4569c3e..5119c58b 100644 --- a/languages/cpp/templates/result-instantiation/object.cpp +++ b/languages/cpp/templates/result-instantiation/object.cpp @@ -1 +1,3 @@ -${properties} \ No newline at end of file + ${type} ${property}Result${level}; +${properties} + ${property} = ${property}Result${level}; \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/property.cpp b/languages/cpp/templates/result-instantiation/property.cpp index 3b54922c..fe19dab1 100644 --- a/languages/cpp/templates/result-instantiation/property.cpp +++ b/languages/cpp/templates/result-instantiation/property.cpp @@ -1 +1 @@ -${shape} ${if.non.object}${base.title}.${property} = jsonResult.${Property}.Value();${end.if.non.object} \ No newline at end of file +${shape} ${if.non.array}${if.non.object}${base.title}Result${level}.${property} = jsonResult.${Property}.Value();${end.if.non.object}${end.if.non.array} \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/ref.h b/languages/cpp/templates/result-instantiation/ref.h new file mode 100644 index 00000000..4da5b5d9 --- /dev/null +++ b/languages/cpp/templates/result-instantiation/ref.h @@ -0,0 +1 @@ +${shape} \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/sub-property/array.cpp b/languages/cpp/templates/result-instantiation/sub-property/array.cpp new file mode 100644 index 00000000..efa1adeb --- /dev/null +++ b/languages/cpp/templates/result-instantiation/sub-property/array.cpp @@ -0,0 +1,4 @@ + auto index(jsonResult.${Property}.Elements()); + while (index.Next() == true) { +${if.object}${items.with.indent}${end.if.object}${if.non.object} ${property}.push_back(index.Current().Value());${end.if.non.object} + } \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/sub-property/object-array.cpp b/languages/cpp/templates/result-instantiation/sub-property/object-array.cpp new file mode 100644 index 00000000..06a4d04d --- /dev/null +++ b/languages/cpp/templates/result-instantiation/sub-property/object-array.cpp @@ -0,0 +1,4 @@ + ${type} ${property}Result${level}; + ${if.namespace.notsame}Firebolt::${info.Title}::${end.if.namespace.notsame}JsonData_${title} jsonResult = index.Current(); +${properties} + ${property}Result.${property}${if.impl.optional}.value()${end.if.impl.optional}.push_back(${property}Result${level}); \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/sub-property/object.cpp b/languages/cpp/templates/result-instantiation/sub-property/object.cpp index c4569c3e..c04d0a50 100644 --- a/languages/cpp/templates/result-instantiation/sub-property/object.cpp +++ b/languages/cpp/templates/result-instantiation/sub-property/object.cpp @@ -1 +1 @@ -${properties} \ No newline at end of file +${properties} diff --git a/languages/cpp/templates/result-instantiation/sub-property/property.cpp b/languages/cpp/templates/result-instantiation/sub-property/property.cpp index 668f442c..97e0dc0f 100644 --- a/languages/cpp/templates/result-instantiation/sub-property/property.cpp +++ b/languages/cpp/templates/result-instantiation/sub-property/property.cpp @@ -1 +1 @@ -${shape} ${if.non.object}${base.title}.${property.dependency}${property} = jsonResult.${Property.dependency}${Property};${end.if.non.object} +${shape} ${if.non.object}${base.title}Result${level}.${property.dependency}${if.impl.optional}value().${end.if.impl.optional}${property} = jsonResult.${Property.dependency}${Property};${end.if.non.object} \ No newline at end of file diff --git a/languages/cpp/templates/result-instantiation/sub-property/title.cpp b/languages/cpp/templates/result-instantiation/sub-property/title.cpp index e2e80c7c..2a420b4b 100644 --- a/languages/cpp/templates/result-instantiation/sub-property/title.cpp +++ b/languages/cpp/templates/result-instantiation/sub-property/title.cpp @@ -1 +1 @@ -${Title} +${Title} \ No newline at end of file diff --git a/src/macrofier/engine.mjs b/src/macrofier/engine.mjs index 94fdca2c..cdf4132b 100644 --- a/src/macrofier/engine.mjs +++ b/src/macrofier/engine.mjs @@ -1191,10 +1191,10 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {}) const pullsResultType = pullsResult && types.getSchemaShape(pullsResult, json, { destination: state.destination, templateDir: state.typeTemplateDir, section: state.section }) const pullsForType = pullsResult && types.getSchemaType(pullsResult, json, { destination: state.destination, templateDir: state.typeTemplateDir, section: state.section }) const pullsParamsType = pullsParams ? types.getSchemaShape(pullsParams, json, { destination: state.destination, templateDir: state.typeTemplateDir, section: state.section }) : '' - const serializedParams = flattenedMethod.params.map(param => types.getSchemaShape(param.schema, json, { templateDir: 'parameter-serialization', property: param.name, destination: state.destination, section: state.section, level: 1, skipTitleOnce: true })).join('\n') + const serializedParams = flattenedMethod.params.map(param => types.getSchemaShape(param.schema, json, { templateDir: 'parameter-serialization', property: param.name, required: param.required || false, destination: state.destination, section: state.section, level: 1, skipTitleOnce: true })).join('\n') const resultInst = types.getSchemaShape(flattenedMethod.result.schema, json, { templateDir: 'result-instantiation', property: flattenedMethod.result.name, destination: state.destination, section: state.section, level: 1, skipTitleOnce: true }) // w/out level: 1, getSchemaShape skips anonymous types, like primitives const resultInit = types.getSchemaShape(flattenedMethod.result.schema, json, { templateDir: 'result-initialization', property: flattenedMethod.result.name, destination: state.destination, section: state.section, level: 1, skipTitleOnce: true }) // w/out level: 1, getSchemaShape skips anonymous types, like primitives - const serializedEventParams = event ? flattenedMethod.params.filter(p => p.name !== 'listen').map(param => types.getSchemaShape(param.schema, json, {templateDir: 'parameter-serialization', property: param.name, destination: state.destination, section: state.section, level: 1, skipTitleOnce: true })).join('\n') : '' + const serializedEventParams = event ? flattenedMethod.params.filter(p => p.name !== 'listen').map(param => types.getSchemaShape(param.schema, json, {templateDir: 'parameter-serialization', property: param.name, required: param.required || false, destination: state.destination, section: state.section, level: 1, skipTitleOnce: true })).join('\n') : '' // this was wrong... check when we merge if it was fixed const callbackSerializedParams = event ? types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-parameter-serialization', property: result.name, destination: state.destination, section: state.section, level: 1, skipTitleOnce: true }) : '' diff --git a/src/macrofier/types.mjs b/src/macrofier/types.mjs index 9a77757a..45c2ac61 100644 --- a/src/macrofier/types.mjs +++ b/src/macrofier/types.mjs @@ -51,6 +51,21 @@ function setAllocatedPrimitiveProxies(m) { } const capitalize = str => str ? str[0].toUpperCase() + str.substr(1) : str +const indent = (str, padding) => { + let first = true + if (str) { + return str.split('\n').map(line => { + if (first) { + first = false + return line + } + else { + return padding + line + } + }).join('\n') + } +} + const safeName = value => value.split(':').pop().replace(/[\.\-]/g, '_').replace(/\+/g, '_plus').replace(/([a-z])([A-Z0-9])/g, '$1_$2').toUpperCase() // TODO: This is what's left of getMethodSignatureParams. We need to figure out / handle C's `FireboltTypes_StringHandle` @@ -78,7 +93,7 @@ function getMethodSignatureParams(method, module, { destination, callback }) { }).join(', ') } -function getMethodSignatureResult(method, module, { destination, callback, overrideRule = false }) { +function getMethodSignatureResult(method, module, { destination, callback }) { let type = getSchemaType(method.result.schema, module, { destination, namespace : true }) let result = '' @@ -124,7 +139,7 @@ const getXSchemaGroup = (schema, module) => { return group } -function insertSchemaMacros(content, schema, module, name, parent, property, recursive = true, overrideRule = false, templateDir = 'types') { +function insertSchemaMacros(content, schema, module, { name = '', parent = '', property = '', required = true, recursive = true, templateDir = 'types'}) { const title = name || schema.title || '' let moduleTitle = getXSchemaGroup(schema, module) @@ -137,6 +152,8 @@ function insertSchemaMacros(content, schema, module, name, parent, property, rec .replace(/\$\{if\.namespace\.notsame}(.*?)\$\{end\.if\.namespace\.notsame\}/g, (module.info.title !== (parent || moduleTitle)) ? '$1' : '') .replace(/\$\{parent\.title\}/g, parent) .replace(/\$\{parent\.Title\}/g, capitalize(parent)) + .replace(/\$\{if\.optional\}(.*?)\$\{end\.if\.optional\}/gms, required ? '' : '$1') + .replace(/\$\{if\.impl.optional\}(.*?)\$\{end\.if\.impl.optional\}/gms, required ? '' : '$1') .replace(/\$\{description\}/g, schema.description ? schema.description : '') .replace(/\$\{summary\}/g, schema.description ? schema.description.split('\n')[0] : '') .replace(/\$\{name\}/g, title) @@ -205,7 +222,9 @@ const insertObjectAdditionalPropertiesMacros = (content, schema, module, title, const insertObjectMacros = (content, schema, module, title, property, options) => { const options2 = options ? JSON.parse(JSON.stringify(options)) : {} options2.parent = title + options2.parentLevel = options.parentLevel options2.level = options.level + 1 + options2.templateDir = options.templateDir ;(['properties', 'properties.register', 'properties.assign']).forEach(macro => { const indent = (content.split('\n').find(line => line.includes("${" + macro + "}")) || '').match(/^\s+/) || [''][0] @@ -239,15 +258,28 @@ const insertObjectMacros = (content, schema, module, title, property, options) = .replace(/\$\{summary\}/g, prop.description ? prop.description.split('\n')[0] : '') .replace(/\$\{delimiter\}(.*?)\$\{end.delimiter\}/gms, i === schema.properties.length - 1 ? '' : '$1') .replace(/\$\{if\.optional\}(.*?)\$\{end\.if\.optional\}/gms, schema.required && schema.required.includes(name) ? '' : '$1') + .replace(/\$\{if\.base\.optional\}(.*?)\$\{end\.if\.base\.optional\}/gms, options.required ? '' : '$1') .replace(/\$\{if\.non\.object\}(.*?)\$\{end\.if\.non\.object\}/gms, (localizedProp.type === 'object') ? '' : '$1') + .replace(/\$\{if\.non\.array\}(.*?)\$\{end\.if\.non\.array\}/gms, (localizedProp.type === 'array') ? '' : '$1') + let baseTitle = options.property if (localizedProp.type === 'object') { - let baseTitle = title || schema.title replacedTemplate = replacedTemplate - - .replace(/\$\{property.dependency\}/g, ((options.level > 1) ? '${property.dependency}' : '') + name + objSeparator) - .replace(/\$\{Property.dependency\}/g, ((options.level > 1) ? '${Property.dependency}' : '') + capitalize(name) + objSeparator) - .replace(/\$\{base.title\}/g, ((options.level <= 1) ? (baseTitle ? (baseTitle)[0].toLowerCase() + (baseTitle).substr(1) : '') : '')).trimEnd() + .replace(/\$\{property.dependency\}/g, ((options.level > 1) ? '${property.dependency}${if.impl.optional}.value()${end.if.impl.optional}' : '') + name + objSeparator) + .replace(/\$\{Property.dependency\}/g, ((options.level > 1) ? '${Property.dependency}' : '') + capitalize(name) + (objSeparator)) } + else { + if (options2.level <= 1) { + replacedTemplate = replacedTemplate + .replace(/\$\{property.dependency\}/g, '') + .replace(/\$\{Property.dependency\}/g, '') + .replace(/\$\{if\.impl.optional\}(.*?)\$\{end\.if\.impl.optional\}/gms, '') + } + } + replacedTemplate = replacedTemplate + .replace(/\$\{obj\.separator}/g, objSeparator) + .replace(/\$\{base.title\}/g, (baseTitle ? (baseTitle)[0].toLowerCase() + (baseTitle).substr(1) : '')).trimEnd() + .replace(/\$\{base.Title\}/g, (baseTitle ? (baseTitle)[0].toUpperCase() + (baseTitle).substr(1) : '')).trimEnd() + .replace(/\$\{if\.impl.optional\}(.*?)\$\{end\.if\.impl.optional\}/gms, prop.required ? '' : '$1') properties.push((i !== 0 ? indent : '') + replacedTemplate) } }) @@ -298,13 +330,18 @@ const insertObjectMacros = (content, schema, module, title, property, options) = const regex = new RegExp("\\$\\{" + macro + "\\}", "g") content = content.replace(regex, properties.join('\n')) + .replace(/\$\{level}/g, options.parentLevel > 0 ? options.parentLevel : '') }) return content } -const insertArrayMacros = (content, schema, module, name) => { - return content.replace(/\$\{json\.type\}/g, getSchemaType(schema.items, module, { templateDir: 'json-types', destination: state.destination, section: state.section, code: false, namespace: true })) +const insertArrayMacros = (content, schema, module, level = 0, items) => { + content = content + .replace(/\$\{json\.type\}/g, getSchemaType(schema.items, module, { templateDir: 'json-types', destination: state.destination, section: state.section, code: false, namespace: true })) + .replace(/\$\{items\}/g, items) + .replace(/\$\{items\.with\.indent\}/g, indent(items, ' ')) + return content } const insertTupleMacros = (content, schema, module, title, options) => { @@ -330,6 +367,7 @@ const insertTupleMacros = (content, schema, module, title, options) => { .replace(/\$\{summary\}/g, prop.description ? prop.description.split('\n')[0] : '') .replace(/\$\{delimiter\}(.*?)\$\{end.delimiter\}/g, i === schema.items.length - 1 ? '' : '$1') .replace(/\$\{if\.optional\}(.*?)\$\{end\.if\.optional\}/gms, '') + .replace(/\$\{if\.impl.optional\}(.*?)\$\{end\.if\.impl.optional\}/gms, '') } content = content.replace(/\$\{properties\}/g, schema.items.map((prop, i) => doMacroWork(propTemplate, prop, i, propIndent)).join(tupleDelimiter)) @@ -378,7 +416,7 @@ const sanitize = (schema) => { return result } -function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', name = '', parent = '', property = '', level = 0, summary, descriptions = true, destination, section, enums = true, skipTitleOnce = false } = {}) { +function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', parent = '', property = '', required = true, parentLevel = 0, level = 0, summary, descriptions = true, destination, section, enums = true, skipTitleOnce = false } = {}) { schema = sanitize(schema) state.destination = destination @@ -389,36 +427,36 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', name } const suffix = destination && ('.' + destination.split('.').pop()) || '' - const theTitle = insertSchemaMacros(getTemplate(path.join(templateDir, 'title' + suffix)), schema, module, schema.title || name, parent, property, false, templateDir) + const theTitle = insertSchemaMacros(getTemplate(path.join(templateDir, 'title' + suffix)), schema, module, { name: schema.title, parent, property, required, recursive: false}) let result = level === 0 ? getTemplate(path.join(templateDir, 'default' + suffix)) : '${shape}' let genericTemplate = getTemplate(path.join(templateDir, 'generic' + suffix)) if (enums && level === 0 && schema.type === "string" && Array.isArray(schema.enum)) { result = getTemplate(path.join(templateDir, 'enum' + suffix)) - return insertSchemaMacros(insertEnumMacros(result, schema, module, theTitle), schema, module, theTitle, parent, property) + return insertSchemaMacros(insertEnumMacros(result, schema, module, theTitle), schema, module, { name: theTitle, parent, property, required }) } if (schema['$ref']) { const someJson = getPath(schema['$ref'], module) if (someJson) { - return getSchemaShape(someJson, module, { templateDir, name, parent, property, level, summary, descriptions, destination, enums: false }) + return getSchemaShape(someJson, module, { templateDir, parent, property, required, parentLevel, level, summary, descriptions, destination, enums: false, array }) } throw "Unresolvable $ref: " + schema['ref'] + ", in " + module.info.title } else if (schema.hasOwnProperty('const')) { const shape = insertConstMacros(getTemplate(path.join(templateDir, 'const' + suffix)), schema, module, theTitle) - result = insertSchemaMacros(result.replace(/\$\{shape\}/g, shape), schema, module, theTitle, parent, property) + result = insertSchemaMacros(result.replace(/\$\{shape\}/g, shape), schema, module, { name: theTitle, parent, property, required }) return result } else if (!skipTitleOnce && (level > 0) && schema.title) { // TODO: allow the 'ref' template to actually insert the shape using getSchemaShape - const innerShape = getSchemaShape(schema, module, { skipTitleOnce: true, templateDir, name, parent, property, level, summary, descriptions, destination, enums: false }) + const innerShape = getSchemaShape(schema, module, { skipTitleOnce: true, templateDir, parent, property, required, parentLevel, level, summary, descriptions, destination, enums: false, array }) const shape = getTemplate(path.join(templateDir, 'ref' + suffix)) .replace(/\$\{shape\}/g, innerShape) result = result.replace(/\$\{shape\}/g, shape) - return insertSchemaMacros(result, schema, module, theTitle, parent, property) + return insertSchemaMacros(result, schema, module, { name: theTitle, parent, property, required }) } else if (schema.type === 'object') { let shape @@ -430,13 +468,13 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', name 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) + return insertSchemaMacros(result, schema, module, { name: theTitle, parent, property, required, templateDir }) } else if (schema.anyOf || schema.oneOf) { const template = getTemplate(path.join(templateDir, 'anyOfSchemaShape' + suffix)) let shape if (template) { - shape = insertAnyOfMacros(template, schema, module, theTitle) + shape = insertAnyOfMacros(template, schema, module, theTitle) } else { // borrow anyOf logic, note that schema is a copy, so we're not breaking it. @@ -447,7 +485,7 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', name } if (shape) { result = result.replace(/\$\{shape\}/g, shape) - return insertSchemaMacros(result, schema, module, theTitle, parent, property) + return insertSchemaMacros(result, schema, module, { name: theTitle, parent, property, required }) } else { return '' @@ -475,26 +513,26 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', name } delete union['$ref'] - return getSchemaShape(union, module, { templateDir, name, parent, property, level, summary, descriptions, destination, enums: false }) + return getSchemaShape(union, module, { templateDir, parent, property, required, parentLevel, level, summary, descriptions, destination, enums: false, array }) } else if (schema.type === "array" && schema.items && isSupportedTuple(schema)) { // tuple const shape = insertTupleMacros(getTemplate(path.join(templateDir, 'tuple' + suffix)) || genericTemplate, schema, module, theTitle, { level, templateDir, descriptions, destination, section, enums }) result = result.replace(/\$\{shape\}/g, shape) - return insertSchemaMacros(result, schema, module, theTitle, parent, property, true, templateDir) + return insertSchemaMacros(result, schema, module, { name: theTitle, parent, property, required, templateDir }) } else if (schema.type === "array" && schema.items && !Array.isArray(schema.items)) { // array - const items = getSchemaShape(schema.items, module, { templateDir, name, parent, property, level, summary, descriptions, destination, enums: false }) + const items = getSchemaShape(schema.items, module, { templateDir, parent, property, required, parentLevel: parentLevel + 1, level, summary, descriptions, destination, enums: false , array: true}) const shape = insertArrayMacros(getTemplate(path.join(templateDir, 'array' + suffix)) || genericTemplate, schema, module, items) result = result.replace(/\$\{shape\}/g, shape) - return insertSchemaMacros(result, schema, module, items, parent, property, true, templateDir) + return insertSchemaMacros(result, schema, module, { name: items, parent, property, required, templateDir }) } else if (schema.type) { const shape = insertPrimitiveMacros(getTemplate(path.join(templateDir, 'primitive' + suffix) || genericTemplate), schema, module, theTitle, templateDir) result = result.replace(/\$\{shape\}/g, shape) if (level > 0) { - return insertSchemaMacros(result, schema, module, theTitle, parent, property) + return insertSchemaMacros(result, schema, module, { name: theTitle, parent, property, required, templateDir}) } } @@ -556,7 +594,7 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin const suffix = destination && ('.' + destination.split('.').pop()) || '' const namespaceStr = namespace ? getTemplate(path.join(templateDir, 'namespace' + suffix)) : '' - const theTitle = insertSchemaMacros(namespaceStr + getTemplate(path.join(templateDir, 'title' + suffix)), schema, module, schema.title, getXSchemaGroup(schema, module), '', false, templateDir) + const theTitle = insertSchemaMacros(namespaceStr + getTemplate(path.join(templateDir, 'title' + suffix)), schema, module, { name: schema.title, parent: getXSchemaGroup(schema, module), recursive: false}) const allocatedProxy = event || result const title = schema.type === "object" || schema.enum ? true : false @@ -606,7 +644,7 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin const params = getMethodSignatureParams(target, module, { destination }) const template = getTemplate(path.join(templateDir, 'x-method')) - return insertSchemaMacros(template.replace(/\$\{params\}/g, params), target.result.schema, module, theTitle, '', '', false) + return insertSchemaMacros(template.replace(/\$\{params\}/g, params), target.result.schema, module, { name: theTitle, recursive: false }) } else if (schema.type === 'string' && schema.enum) { let type = expandEnums ? schema.enum.map(e => wrap(e, '\'')).join(' | ') : schema.type @@ -653,17 +691,17 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin // Tuple -> Array if (convertTuplesToArraysOrObjects && isTuple(schema) && isHomogenous(schema)) { template = insertArrayMacros(getTemplate(path.join(templateDir, 'array')), schema, module) - template = insertSchemaMacros(template, firstItem, module, getSchemaType(firstItem, module, {destination, templateDir, link, title, code, asPath, event, result, expandEnums, baseUrl, namespace }), '', '', false) + template = insertSchemaMacros(template, firstItem, module, { name: getSchemaType(firstItem, module, {destination, templateDir, link, title, code, asPath, event, result, expandEnums, baseUrl, namespace }), recursive: false }) } // Normal Array else if (!isTuple(schema)) { const baseDir = (templateDir !== 'json-types' ? 'types': templateDir) template = insertArrayMacros(getTemplate(path.join(baseDir, 'array')), schema, module) - template = insertSchemaMacros(template, schema.items, module, getSchemaType(schema.items, module, {destination, templateDir, link, title, code, asPath, event, result, expandEnums, baseUrl, namespace }), '', '', true) + template = insertSchemaMacros(template, schema.items, module, { name: getSchemaType(schema.items, module, {destination, templateDir, link, title, code, asPath, event, result, expandEnums, baseUrl, namespace })) } else { template = insertTupleMacros(getTemplate(path.join(templateDir, 'tuple')), schema, module, '', { templateDir }) - template = insertSchemaMacros(template, firstItem, module, '', '', '', false) + template = insertSchemaMacros(template, firstItem, module, { recursive: false }) } if (code) { @@ -677,7 +715,7 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin if (schema.title) { union.title = schema.title } - return getSchemaType(union, module, { destination, link, title, code, asPath, baseUrl, namespace }) + return getSchemaType(union, module, { templateDir, destination, link, title, code, asPath, baseUrl, namespace }) } else if (schema.oneOf || schema.anyOf) { if (!schema.anyOf) { @@ -685,7 +723,7 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin } // todo... we probably shouldn't allow untitled anyOfs, at least not w/out a feature flag const shape = insertAnyOfMacros(getTemplate(path.join(templateDir, 'anyOf' + suffix)), schema, module, theTitle) - return insertSchemaMacros(shape, schema, module, theTitle, '', '', false) + return insertSchemaMacros(shape, schema, module, { name: theTitle, recursive: false }) // if (event) { @@ -700,16 +738,23 @@ 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 baseDir = (templateDir !== 'json-types' ? 'types': templateDir) - const primitive = getPrimitiveType(schemaType, baseDir) - 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, { name: theTitle, recursive: 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 baseDir = (templateDir !== 'json-types' ? 'types': templateDir) + const primitive = getPrimitiveType(schemaType, baseDir) + const type = allocatedProxy ? allocatedPrimitiveProxies[schemaType] || primitive : primitive + return wrap(type, code ? '`' : '') + } } else { + const template = getTemplate(path.join(templateDir, 'void')) || 'void' // TODO this is TypeScript specific - return wrap('void', code ? '`' : '') + return wrap(template, code ? '`' : '') } }