diff --git a/src/macrofier/engine.mjs b/src/macrofier/engine.mjs index 14c80927..fa2e6922 100644 --- a/src/macrofier/engine.mjs +++ b/src/macrofier/engine.mjs @@ -43,11 +43,6 @@ const _inspector = obj => { } } -// getSchemaType(schema, module, options = { destination: 'file.txt', title: true }) -// getSchemaShape(schema, module, options = { name: 'Foo', destination: 'file.txt' }) -// getJsonType(schema, module, options = { name: 'Foo', prefix: '', descriptions: false, level: 0 }) -// getSchemaInstantiation(schema, module, options = {type: 'params' | 'result' | 'callback.params'| 'callback.result' | 'callback.response'}) - let types = { getSchemaShape: () => null, getSchemaType: () => null @@ -136,7 +131,7 @@ const getLinkForSchema = (schema, json) => { const copySchemasIntoModules = config.copySchemasIntoModules const definitions = json.definitions || json.components.schemas - const type = types.getSchemaType(schema, json, { templateDir: state.typeTemplateDir, destination: state.destination, section: state.section }) + const type = types.getSchemaType(schema, json, { templateDir: state.typeTemplateDir, destination: state.destination, section: state.section, namespace: !config.copySchemasIntoModules }) // local - insert a bogus link, that we'll update later based on final table-of-contents if (definitions && definitions[type]) { @@ -163,25 +158,6 @@ const getLinkForSchema = (schema, json) => { return '#' } -// const getComponentExternalSchema = (json) => { -// let refSchemas = [] -// if (json.components && json.components.schemas) { -// Object.entries(json.components.schemas).forEach(([name, schema]) => { -// let refs = getLinkedSchemaPaths(schema).map(path => getPathOr(null, path, schema)) -// refs.map(ref => { -// let title = '' -// if (ref.includes('x-schemas')) { -// if (ref.split('/')[2] !== json.info.title) { -// title = ref.split('/')[2] -// } -// } -// title && !refSchemas.includes(title) ? refSchemas.push(title) : null -// }) -// }) -// } -// return (refSchemas) -// } - // Maybe methods array of objects const getMethods = compose( map(filter(isObject)), @@ -950,7 +926,7 @@ function generateSchemas(json, templates, options) { else { content = content.replace(/\$\{if\.description\}(.*?)\{end\.if\.description\}/gms, '$1') } - const schemaShape = types.getSchemaShape(schema, json, { templateDir: state.typeTemplateDir, destination: state.destination, section: options.section, primitive: config.primitives ? Object.keys(config.primitives).length > 0 : false }) + const schemaShape = types.getSchemaShape(schema, json, { templateDir: state.typeTemplateDir, destination: state.destination, section: options.section, primitive: config.primitives ? Object.keys(config.primitives).length > 0 : false, namespace: !config.copySchemasIntoModules }) content = content .replace(/\$\{schema.title\}/, (schema.title || name)) @@ -1023,7 +999,7 @@ function getRelatedSchemaLinks(schema = {}, json = {}, templates = {}, options = .filter(path => seen.hasOwnProperty(path) ? false : (seen[path] = true)) .map(ref => getReferencedSchema(ref, json)) .filter(schema => schema.title) - .map(schema => '[' + types.getSchemaType(schema, json, { templateDir: state.typeTemplateDir, destination: state.destination, section: state.section }) + '](' + getLinkForSchema(schema, json) + ')') // need full module here, not just the schema + .map(schema => '[' + types.getSchemaType(schema, json, { templateDir: state.typeTemplateDir, destination: state.destination, section: state.section, namespace: !config.copySchemasIntoModules }) + '](' + getLinkForSchema(schema, json) + ')') // need full module here, not just the schema .filter(link => link) .join('\n') @@ -1338,53 +1314,53 @@ function insertMethodMacros(template, methodObj, json, templates, type = '', exa const pullsResult = (puller || pullsFor) ? localizeDependencies(pullsFor || methodObj, json).params[1].schema : null const pullsParams = (puller || pullsFor) ? localizeDependencies(getPayloadFromEvent(puller || methodObj), json, null, { mergeAllOfs: true }).properties.parameters : null - const pullsResultType = (pullsResult && (type === 'methods')) ? 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 && (type === 'methods')) ? types.getSchemaShape(pullsParams, json, { destination: state.destination, templateDir: state.typeTemplateDir, section: state.section }) : '' + const pullsResultType = (pullsResult && (type === 'methods')) ? types.getSchemaShape(pullsResult, json, { destination: state.destination, templateDir: state.typeTemplateDir, section: state.sectio, namespace: !config.copySchemasIntoModules }) : '' + const pullsForType = pullsResult && types.getSchemaType(pullsResult, json, { destination: state.destination, templateDir: state.typeTemplateDir, section: state.section, namespace: !config.copySchemasIntoModules }) + const pullsParamsType = (pullsParams && (type === 'methods')) ? types.getSchemaShape(pullsParams, json, { destination: state.destination, templateDir: state.typeTemplateDir, section: state.section, namespace: !config.copySchemasIntoModules }) : '' const pullsForParamTitle = pullsParams ? pullsParams.title.charAt(0).toLowerCase() + pullsParams.title.substring(1) : '' const pullsForResultTitle = (pullsResult && pullsResult.title) ? pullsResult.title.charAt(0).toLowerCase() + pullsResult.title.substring(1) : '' - const pullsResponseInit = (pullsParams && (type === 'methods')) ? types.getSchemaShape(pullsParams, json, { templateDir: 'result-initialization', property: pullsForParamTitle, required: pullsParams.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) : '' - const pullsResponseInst = (pullsParams && (type === 'methods')) ? types.getSchemaShape(pullsParams, json, { templateDir: 'result-instantiation', property: pullsForParamTitle, required: pullsParams.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) : '' - const pullsResultSerialize = (pullsResult && (type === 'methods')) ? types.getSchemaShape(pullsResult, json, { templateDir: 'parameter-serialization/sub-property', property: pullsForResultTitle, required: pullsResult.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) : '' - - const serializedParams = (type === 'methods') ? flattenedMethod.params.map(param => types.getSchemaShape(param.schema, json, { templateDir: 'parameter-serialization', property: param.name, required: param.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true })).join('\n') : '' - const resultInst = (type === 'methods') ? types.getSchemaShape(flattenedMethod.result.schema, json, { templateDir: 'result-instantiation', property: flattenedMethod.result.name, required: flattenedMethod.result.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) : '' // w/out primitive: true, getSchemaShape skips anonymous types, like primitives - const resultInit = (type === 'methods') ? types.getSchemaShape(flattenedMethod.result.schema, json, { templateDir: 'result-initialization', property: flattenedMethod.result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) : '' // w/out primitive: true, getSchemaShape skips anonymous types, like primitives - const serializedEventParams = event && (type === 'methods') ? flattenedMethod.params.filter(p => p.name !== 'listen').map(param => types.getSchemaShape(param.schema, json, {templateDir: 'parameter-serialization', property: param.name, required: param.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true })).join('\n') : '' + const pullsResponseInit = (pullsParams && (type === 'methods')) ? types.getSchemaShape(pullsParams, json, { templateDir: 'result-initialization', property: pullsForParamTitle, required: pullsParams.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true, namespace: !config.copySchemasIntoModules }) : '' + const pullsResponseInst = (pullsParams && (type === 'methods')) ? types.getSchemaShape(pullsParams, json, { templateDir: 'result-instantiation', property: pullsForParamTitle, required: pullsParams.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true, namespace: !config.copySchemasIntoModules }) : '' + const pullsResultSerialize = (pullsResult && (type === 'methods')) ? types.getSchemaShape(pullsResult, json, { templateDir: 'parameter-serialization/sub-property', property: pullsForResultTitle, required: pullsResult.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true, namespace: !config.copySchemasIntoModules }) : '' + + const serializedParams = (type === 'methods') ? flattenedMethod.params.map(param => types.getSchemaShape(param.schema, json, { templateDir: 'parameter-serialization', property: param.name, required: param.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true, namespace: !config.copySchemasIntoModules })).join('\n') : '' + const resultInst = (type === 'methods') ? types.getSchemaShape(flattenedMethod.result.schema, json, { templateDir: 'result-instantiation', property: flattenedMethod.result.name, required: flattenedMethod.result.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true, namespace: !config.copySchemasIntoModules }) : '' // w/out primitive: true, getSchemaShape skips anonymous types, like primitives + const resultInit = (type === 'methods') ? types.getSchemaShape(flattenedMethod.result.schema, json, { templateDir: 'result-initialization', property: flattenedMethod.result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true, namespace: !config.copySchemasIntoModules }) : '' // w/out primitive: true, getSchemaShape skips anonymous types, like primitives + const serializedEventParams = event && (type === 'methods') ? flattenedMethod.params.filter(p => p.name !== 'listen').map(param => types.getSchemaShape(param.schema, json, {templateDir: 'parameter-serialization', property: param.name, required: param.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true, namespace: !config.copySchemasIntoModules })).join('\n') : '' // this was wrong... check when we merge if it was fixed - const callbackSerializedList = event && (type === 'methods') ? types.getSchemaShape(event.result.schema, json, { templateDir: eventHasOptionalParam(event) && !event.tags.find(t => t.name === 'provider') ? 'callback-serialization' : 'callback-result-serialization', property: result.name, required: event.result.schema.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) : '' - const callbackInitialization = event && (type === 'methods') ? (eventHasOptionalParam(event) && !event.tags.find(t => t.name === 'provider') ? (event.params.map(param => isOptionalParam(param) ? types.getSchemaShape(param.schema, json, { templateDir: 'callback-initialization-optional', property: param.name, required: param.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) : '').filter(param => param).join('\n') + '\n') : '' ) + (types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-initialization', property: result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true })) : '' + const callbackSerializedList = event && (type === 'methods') ? types.getSchemaShape(event.result.schema, json, { templateDir: eventHasOptionalParam(event) && !event.tags.find(t => t.name === 'provider') ? 'callback-serialization' : 'callback-result-serialization', property: result.name, required: event.result.schema.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true, namespace: !config.copySchemasIntoModules }) : '' + const callbackInitialization = event && (type === 'methods') ? (eventHasOptionalParam(event) && !event.tags.find(t => t.name === 'provider') ? (event.params.map(param => isOptionalParam(param) ? types.getSchemaShape(param.schema, json, { templateDir: 'callback-initialization-optional', property: param.name, required: param.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) : '').filter(param => param).join('\n') + '\n') : '' ) + (types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-initialization', property: result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true, namespace: !config.copySchemasIntoModules })) : '' let callbackInstantiation = '' if (event) { if (eventHasOptionalParam(event) && !event.tags.find(t => t.name === 'provider')) { - callbackInstantiation = (type === 'methods') ? types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-instantiation', property: result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) : '' - let paramInstantiation = (type === 'methods') ? event.params.map(param => isOptionalParam(param) ? types.getSchemaShape(param.schema, json, { templateDir: 'callback-context-instantiation', property: param.name, required: param.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) : '').filter(param => param).join('\n') : '' - let resultInitialization = (type === 'methods') ? types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-value-initialization', property: result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) : '' - let resultInstantiation = (type === 'methods') ? types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-value-instantiation', property: result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) : '' + callbackInstantiation = (type === 'methods') ? types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-instantiation', property: result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true, namespace: !config.copySchemasIntoModules }) : '' + let paramInstantiation = (type === 'methods') ? event.params.map(param => isOptionalParam(param) ? types.getSchemaShape(param.schema, json, { templateDir: 'callback-context-instantiation', property: param.name, required: param.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true, namespace: !config.copySchemasIntoModules }) : '').filter(param => param).join('\n') : '' + let resultInitialization = (type === 'methods') ? types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-value-initialization', property: result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true, namespace: !config.copySchemasIntoModules }) : '' + let resultInstantiation = (type === 'methods') ? types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-value-instantiation', property: result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true, namespace: !config.copySchemasIntoModules }) : '' callbackInstantiation = callbackInstantiation .replace(/\$\{callback\.param\.instantiation\.with\.indent\}/g, indent(paramInstantiation, ' ', 3)) .replace(/\$\{callback\.result\.initialization\.with\.indent\}/g, indent(resultInitialization, ' ', 1)) .replace(/\$\{callback\.result\.instantiation\}/g, resultInstantiation) } else { - callbackInstantiation = (type === 'methods') ? types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-result-instantiation', property: result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) : '' + callbackInstantiation = (type === 'methods') ? types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-result-instantiation', property: result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true, namespace: !config.copySchemasIntoModules }) : '' } } // hmm... how is this different from callbackSerializedList? i guess they get merged? - const callbackResponseInst = event && (type === 'methods') ? (eventHasOptionalParam(event) ? (event.params.map(param => isOptionalParam(param) ? types.getSchemaShape(param.schema, json, { templateDir: 'callback-response-instantiation', property: param.name, required: param.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) : '').filter(param => param).join(', ') + ', ') : '' ) + (types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-response-instantiation', property: result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true })) : '' - const resultType = result.schema ? types.getSchemaType(result.schema, json, { templateDir: state.typeTemplateDir }) : '' + const callbackResponseInst = event && (type === 'methods') ? (eventHasOptionalParam(event) ? (event.params.map(param => isOptionalParam(param) ? types.getSchemaShape(param.schema, json, { templateDir: 'callback-response-instantiation', property: param.name, required: param.required, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true, namespace: !config.copySchemasIntoModules }) : '').filter(param => param).join(', ') + ', ') : '' ) + (types.getSchemaShape(event.result.schema, json, { templateDir: 'callback-response-instantiation', property: result.name, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true, namespace: !config.copySchemasIntoModules })) : '' + const resultType = result.schema ? types.getSchemaType(result.schema, json, { templateDir: state.typeTemplateDir, namespace: !config.copySchemasIntoModules }) : '' const resultSchemaType = result.schema.type - const resultJsonType = result.schema ? types.getSchemaType(result.schema, json, { templateDir: 'json-types' }) : '' + const resultJsonType = result.schema ? types.getSchemaType(result.schema, json, { templateDir: 'json-types', namespace: !config.copySchemasIntoModules }) : '' const resultParams = generateResultParams(result.schema, json, templates, { name: result.name}) // todo: what does prefix do in Types.mjs? need to account for it somehow - const callbackResultJsonType = event && result.schema ? types.getSchemaType(result.schema, json, { templateDir: 'json-types' }) : '' + const callbackResultJsonType = event && result.schema ? types.getSchemaType(result.schema, json, { templateDir: 'json-types', namespace: !config.copySchemasIntoModules }) : '' - const pullsForParamType = pullsParams ? types.getSchemaType(pullsParams, json, { destination: state.destination, section: state.section }) : '' - const pullsForJsonType = pullsResult ? types.getSchemaType(pullsResult, json, { templateDir: 'json-types' }) : '' - const pullsForParamJsonType = pullsParams ? types.getSchemaType(pullsParams, json, { templateDir: 'json-types' }) : '' + const pullsForParamType = pullsParams ? types.getSchemaType(pullsParams, json, { destination: state.destination, section: state.section, namespace: !config.copySchemasIntoModules }) : '' + const pullsForJsonType = pullsResult ? types.getSchemaType(pullsResult, json, { templateDir: 'json-types', namespace: !config.copySchemasIntoModules }) : '' + const pullsForParamJsonType = pullsParams ? types.getSchemaType(pullsParams, json, { templateDir: 'json-types', namespace: !config.copySchemasIntoModules }) : '' - const pullsEventParamName = event ? types.getSchemaInstantiation(event.result, json, event.name, { instantiationType: 'pull.param.name' }) : '' + const pullsEventParamName = event ? types.getSchemaInstantiation(event.result, json, event.name, { instantiationType: 'pull.param.name', namespace: !config.copySchemasIntoModules }) : '' let seeAlso = '' if (isPolymorphicPullMethod(methodObj) && pullsForType) { @@ -1398,7 +1374,7 @@ function insertMethodMacros(template, methodObj, json, templates, type = '', exa if (isTemporalSetMethod(methodObj)) { itemName = result.schema.items.title || 'item' itemName = itemName.charAt(0).toLowerCase() + itemName.substring(1) - itemType = types.getSchemaType(result.schema.items, json, { destination: state.destination, templateDir: state.typeTemplateDir, section: state.section }) + itemType = types.getSchemaType(result.schema.items, json, { destination: state.destination, templateDir: state.typeTemplateDir, section: state.section, namespace: !config.copySchemasIntoModules }) } template = insertExampleMacros(template, examples[methodObj.name] || [], methodObj, json, templates) @@ -1426,8 +1402,8 @@ function insertMethodMacros(template, methodObj, json, templates, type = '', exa .replace(/\$\{method\.params\.serialization\}/g, serializedParams) .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\.signature\.params\}/g, types.getMethodSignatureParams(methodObj, json, { destination: state.destination, section: state.section, namespace: !config.copySchemasIntoModules })) + .replace(/\$\{method\.signature\.result\}/g, types.getMethodSignatureResult(methodObj, json, { destination: state.destination, section: state.section, namespace: !config.copySchemasIntoModules })) .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) @@ -1438,8 +1414,8 @@ function insertMethodMacros(template, methodObj, json, templates, type = '', exa .replace(/\$\{event\.params\.table\.rows\}/g, eventParamsRows) .replace(/\$\{if\.event\.params\}(.*?)\$\{end\.if\.event\.params\}/gms, event && event.params.length ? '$1' : '') .replace(/\$\{if\.event\.callback\.params\}(.*?)\$\{end\.if\.event\.callback\.params\}/gms, event && eventHasOptionalParam(event) ? '$1' : '') - .replace(/\$\{event\.signature\.params\}/g, event ? types.getMethodSignatureParams(event, json, { destination: state.destination, section: state.section }) : '') - .replace(/\$\{event\.signature\.callback\.params\}/g, event ? types.getMethodSignatureParams(event, json, { destination: state.destination, section: state.section, callback: true }) : '') + .replace(/\$\{event\.signature\.params\}/g, event ? types.getMethodSignatureParams(event, json, { destination: state.destination, section: state.section, namespace: !config.copySchemasIntoModules }) : '') + .replace(/\$\{event\.signature\.callback\.params\}/g, event ? types.getMethodSignatureParams(event, json, { destination: state.destination, section: state.section, callback: true, namespace: !config.copySchemasIntoModules }) : '') .replace(/\$\{event\.params\.serialization\}/g, serializedEventParams) .replace(/\$\{event\.callback\.serialization\}/g, callbackSerializedList) .replace(/\$\{event\.callback\.initialization\}/g, callbackInitialization) @@ -1461,10 +1437,10 @@ function insertMethodMacros(template, methodObj, json, templates, type = '', exa .replace(/\$\{method\.result\.name\}/g, result.name) .replace(/\$\{method\.result\.summary\}/g, result.summary) .replace(/\$\{method\.result\.link\}/g, getLinkForSchema(result.schema, json)) //, baseUrl: options.baseUrl - .replace(/\$\{method\.result\.type\}/g, types.getSchemaType(result.schema, json, { templateDir: state.typeTemplateDir, title: true, asPath: false, destination: state.destination, result: true })) //, baseUrl: options.baseUrl - .replace(/\$\{method\.result\.json\}/g, types.getSchemaType(result.schema, json, { templateDir: 'json-types', destination: state.destination, section: state.section, title: true, code: false, link: false, asPath: false, expandEnums: false, namespace: true })) + .replace(/\$\{method\.result\.type\}/g, types.getSchemaType(result.schema, json, { templateDir: state.typeTemplateDir, title: true, asPath: false, destination: state.destination, result: true, namespace: !config.copySchemasIntoModules })) //, baseUrl: options.baseUrl + .replace(/\$\{method\.result\.json\}/g, types.getSchemaType(result.schema, json, { templateDir: 'json-types', destination: state.destination, section: state.section, title: true, code: false, link: false, asPath: false, expandEnums: false, namespace: !config.copySchemasIntoModules })) // todo: what does prefix do? - .replace(/\$\{event\.result\.type\}/g, isEventMethod(methodObj) ? types.getMethodSignatureResult(event, json, { destination: state.destination, section: state.section, callback: true }) : '') + .replace(/\$\{event\.result\.type\}/g, isEventMethod(methodObj) ? types.getMethodSignatureResult(event, json, { destination: state.destination, section: state.section, callback: true, namespace: !config.copySchemasIntoModules }) : '') .replace(/\$\{event\.result\.json\.type\}/g, resultJsonType) .replace(/\$\{event\.result\.json\.type\}/g, callbackResultJsonType) .replace(/\$\{event\.pulls\.param\.name\}/g, pullsEventParamName) @@ -1507,7 +1483,7 @@ function insertMethodMacros(template, methodObj, json, templates, type = '', exa const matches = [...template.matchAll(/\$\{method\.params\[([0-9]+)\]\.type\}/g)] matches.forEach(match => { const index = parseInt(match[1]) - template = template.replace(/\$\{method\.params\[([0-9]+)\]\.type\}/g, types.getSchemaType(methodObj.params[index].schema, json, { destination: state.destination, templateDir: state.typeTemplateDir })) + template = template.replace(/\$\{method\.params\[([0-9]+)\]\.type\}/g, types.getSchemaType(methodObj.params[index].schema, json, { destination: state.destination, templateDir: state.typeTemplateDir, namespace: !config.copySchemasIntoModules })) template = template.replace(/\$\{method\.params\[([0-9]+)\]\.name\}/g, methodObj.params[index].name) }) @@ -1612,7 +1588,7 @@ function insertExampleMacros(template, examples, method, json, templates) { function generateResult(result, json, templates, { name = '' } = {}) { - const type = types.getSchemaType(result, json, { templateDir: state.typeTemplateDir, destination: state.destination, section: state.section }) + const type = types.getSchemaType(result, json, { templateDir: state.typeTemplateDir, destination: state.destination, section: state.section, namespace: !config.copySchemasIntoModules }) if (result.type === 'object' && result.properties) { let content = getTemplate('/types/object', templates).split('\n') @@ -1633,13 +1609,13 @@ function generateResult(result, json, templates, { name = '' } = {}) { // if we get a real link use it if (link !== '#') { - return `[${types.getSchemaType(result, json, { destination: state.destination, templateDir: state.typeTemplateDir, section: state.section })}](${link})` + return `[${types.getSchemaType(result, json, { destination: state.destination, templateDir: state.typeTemplateDir, section: state.section, namespace: !config.copySchemasIntoModules })}](${link})` } // otherwise this was a schema with no title, and we'll just copy it here else { const schema = localizeDependencies(result, json) return getTemplate('/types/default', templates) - .replace(/\$\{type\}/, types.getSchemaShape(schema, json, { templateDir: state.typeTemplateDir })) + .replace(/\$\{type\}/, types.getSchemaShape(schema, json, { templateDir: state.typeTemplateDir, namespace: !config.copySchemasIntoModules })) } } else { @@ -1666,7 +1642,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, { moduleTitle: moduleTitle, result: true, namespace: true})) + .replace(/\$\{method\.param\.type\}/g, types.getSchemaType(type, json, { moduleTitle: moduleTitle, result: true, namespace: !config.copySchemasIntoModules})) ).join(', ') // most languages separate params w/ a comma, so leaving this here for now } // tuples get unwrapped @@ -1675,14 +1651,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, { moduleTitle: moduleTitle, result: true, namespace: true})) + .replace(/\$\{method\.param\.type\}/g, types.getSchemaType(type, json, { moduleTitle: moduleTitle, result: true, namespace: !config.copySchemasIntoModules})) ).join(', ') } // everything else is just output as-is else { const template = getTemplate('/parameters/result', templates) - const type = types.getSchemaType(result, json, { moduleTitle: moduleTitle, result: true, namespace: true}) + const type = types.getSchemaType(result, json, { moduleTitle: moduleTitle, result: true, namespace: !config.copySchemasIntoModules}) if (type === 'undefined') { console.log(`Warning: undefined type for ${name}`) } @@ -1695,7 +1671,7 @@ function generateResultParams(result, json, templates, { name = '' } = {}) { function insertSchemaMacros(template, title, schema, module) { return template.replace(/\$\{property\}/g, title) - .replace(/\$\{type\}/g, types.getSchemaType(schema, module, { templateDir: state.typeTemplateDir, destination: state.destination, section: state.section, code: false })) + .replace(/\$\{type\}/g, types.getSchemaType(schema, module, { templateDir: state.typeTemplateDir, destination: state.destination, section: state.section, code: false, namespace: !config.copySchemasIntoModules })) .replace(/\$\{type.link\}/g, getLinkForSchema(schema, module)) .replace(/\$\{description\}/g, schema.description || '') .replace(/\$\{name\}/g, title || '') @@ -1706,9 +1682,9 @@ function insertParameterMacros(template, param, method, module) { //| `${method.param.name}` | ${method.param.type} | ${method.param.required} | ${method.param.summary} ${method.param.constraints} | let constraints = getSchemaConstraints(param, module) - let type = types.getSchemaType(param.schema, module, { templateDir: state.typeTemplateDir, destination: state.destination, section: state.section, code: false, link: false, asPath: false, expandEnums: false }) //baseUrl: options.baseUrl + let type = types.getSchemaType(param.schema, module, { templateDir: state.typeTemplateDir, destination: state.destination, section: state.section, code: false, link: false, asPath: false, expandEnums: false, namespace: !config.copySchemasIntoModules }) //baseUrl: options.baseUrl let typeLink = getLinkForSchema(param.schema, module) - let jsonType = types.getSchemaType(param.schema, module, { templateDir: 'json-types', destination: state.destination, section: state.section, code: false, link: false, asPath: false, expandEnums: false }) + let jsonType = types.getSchemaType(param.schema, module, { templateDir: 'json-types', destination: state.destination, section: state.section, code: false, link: false, asPath: false, expandEnums: false, namespace: !config.copySchemasIntoModules }) if (constraints && type) { constraints = '
' + constraints @@ -1820,12 +1796,12 @@ function getProviderXValues(method) { function insertProviderXValues(template, moduleJson, xValues) { if (xValues['x-response']) { - const xResponseInst = types.getSchemaShape(xValues['x-response'], moduleJson, { templateDir: 'parameter-serialization', property: 'result', required: true, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) + const xResponseInst = types.getSchemaShape(xValues['x-response'], moduleJson, { templateDir: 'parameter-serialization', property: 'result', required: true, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true, namespace: !config.copySchemasIntoModules }) template = template.replace(/\$\{provider\.xresponse\.serialization\}/gms, xResponseInst) .replace(/\$\{provider\.xresponse\.name\}/gms, xValues['x-response'].title) } if (xValues['x-error']) { - const xErrorInst = types.getSchemaShape(xValues['x-error'], moduleJson, { templateDir: 'parameter-serialization', property: 'result', required: true, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true }) + const xErrorInst = types.getSchemaShape(xValues['x-error'], moduleJson, { templateDir: 'parameter-serialization', property: 'result', required: true, destination: state.destination, section: state.section, primitive: true, skipTitleOnce: true, namespace: !config.copySchemasIntoModules }) template = template.replace(/\$\{provider\.xerror\.serialization\}/gms, xErrorInst) .replace(/\$\{provider\.xerror\.name\}/gms, xValues['x-error'].title) } @@ -1891,7 +1867,7 @@ function insertProviderInterfaceMacros(template, capability, moduleJson = {}, te name: 'provider' }) const parametersSchema = method.params[0].schema - const parametersShape = types.getSchemaShape(parametersSchema, moduleJson, { destination: state.destination, templateDir: state.typeTemplateDir, section: state.section }) + const parametersShape = types.getSchemaShape(parametersSchema, moduleJson, { destination: state.destination, templateDir: state.typeTemplateDir, section: state.section, namespace: !config.copySchemasIntoModules }) let methodBlock = insertMethodMacros(getTemplateForMethod(method, templates), method, moduleJson, templates) methodBlock = methodBlock.replace(/\${parameters\.shape\}/g, parametersShape) const hasProviderParameters = parametersSchema && parametersSchema.properties && Object.keys(parametersSchema.properties).length > 0 @@ -1971,7 +1947,7 @@ function insertProviderParameterMacros(data = '', parameters, module = {}, optio Object.entries(parameters.properties).forEach(([name, param]) => { let constraints = getSchemaConstraints(param, module) - let type = types.getSchemaType(param, module, { destination: state.destination, templateDir: state.typeTemplateDir, section: state.section, code: true, link: true, asPath: options.asPath, baseUrl: options.baseUrl }) + let type = types.getSchemaType(param, module, { destination: state.destination, templateDir: state.typeTemplateDir, section: state.section, code: true, link: true, asPath: options.asPath, baseUrl: options.baseUrl, namespace: !config.copySchemasIntoModules }) if (constraints && type) { constraints = '
' + constraints diff --git a/src/macrofier/index.mjs b/src/macrofier/index.mjs index ee40f665..0252e75a 100644 --- a/src/macrofier/index.mjs +++ b/src/macrofier/index.mjs @@ -153,7 +153,6 @@ const macrofy = async ( const id = schema.$id externalSchemas[id] = JSON.parse(JSON.stringify(schema)) replaceUri(id, '', externalSchemas[id]) - console.dir(Object.keys(externalSchemas[id].definitions)) Object.values(openrpc.components.schemas).forEach(schema => { if (schema.$id && schema.$id !== id) { externalSchemas[id].definitions[schema.$id] = schema @@ -161,7 +160,7 @@ const macrofy = async ( }) }) - const aggregateMacros = engine.generateAggregateMacros(openrpc, modules.concat(staticModules).concat(Object.values(externalSchemas)), templates, libraryName) + const aggregateMacros = engine.generateAggregateMacros(openrpc, modules.concat(staticModules).concat(copySchemasIntoModules ? [] : Object.values(externalSchemas)), templates, libraryName) const outputFiles = Object.fromEntries(Object.entries(await readFiles( staticCodeList, staticContent)) .map( ([n, v]) => [path.join(output, n), v])) @@ -259,13 +258,11 @@ const macrofy = async ( } // Output any schema templates for each bundled external schema document - Object.values(externalSchemas).forEach( document => { + !copySchemasIntoModules && Object.values(externalSchemas).forEach( document => { if (mergeOnTitle && modules.find(m => m.info.title === document.title)) { - console.log('Skipping: ' + document.title + ': ' + document.$id) return // skip this one, it was already merged into the module w/ the same name } - console.log(document.title + ': ' + document.$id) if (templatesPerSchema || primaryOutput.length) { templatesPerSchema && templatesPerSchema.forEach( t => { const macros = engine.generateMacros(document, templates, exampleTemplates, {hideExcluded: hideExcluded, createPolymorphicMethods: createPolymorphicMethods, destination: t}) @@ -279,11 +276,6 @@ const macrofy = async ( }) primaryOutput && primaryOutput.forEach(output => { - console.log(`appending ${document.title} schemas to ${output}`) - console.dir(Object.keys(document.definitions), { depth: 100 }) - if (document.title === 'Entertainment') { - console.dir(Object.keys(document.definitions['https://meta.rdkcentral.com/firebolt/schemas/types'].definitions)) - } const macros = engine.generateMacros(document, templates, exampleTemplates, {hideExcluded: hideExcluded, copySchemasIntoModules: copySchemasIntoModules, createPolymorphicMethods: createPolymorphicMethods, destination: output}) macros.append = append outputFiles[output] = engine.insertMacros(outputFiles[output], macros) diff --git a/src/macrofier/types.mjs b/src/macrofier/types.mjs index 08a962c6..4958e46f 100644 --- a/src/macrofier/types.mjs +++ b/src/macrofier/types.mjs @@ -70,14 +70,14 @@ const indent = (str, padding) => { } // TODO: This is what's left of getMethodSignatureParams. We need to figure out / handle C's `FireboltTypes_StringHandle` -function getMethodSignatureParams(method, module, { destination, callback }) { +function getMethodSignatureParams(method, module, { destination, callback, namespace }) { const paramOptional = getTemplate('/parameters/optional') let polymorphicPull = method.tags.find(t => t.name === 'polymorphic-pull') return method.params.map(param => { if (polymorphicPull && (param.name === 'correlationId')) { return } - let type = getSchemaType(param.schema, module, { destination, namespace : true }) + let type = getSchemaType(param.schema, module, { destination, namespace }) if (callback && allocatedPrimitiveProxies[type]) { type = allocatedPrimitiveProxies[type] } @@ -103,8 +103,8 @@ function getMethodSignatureParams(method, module, { destination, callback }) { }).filter(param => param).join(', ') } -function getMethodSignatureResult(method, module, { destination, callback }) { - let type = getSchemaType(method.result.schema, module, { destination, namespace : true }) +function getMethodSignatureResult(method, module, { destination, callback, namespace }) { + let type = getSchemaType(method.result.schema, module, { destination, namespace }) let result = '' if (callback) { @@ -365,7 +365,7 @@ const insertObjectMacros = (content, schema, module, title, property, options) = .replace(/\$\{if\.base\.optional\}(.*?)\$\{end\.if\.base\.optional\}/gms, options.required ? '' : '$1') .replace(/\$\{if\.non\.object\}(.*?)\$\{end\.if\.non\.object\}/gms, isObject(localizedProp) ? '' : '$1') .replace(/\$\{if\.non\.array\}(.*?)\$\{end\.if\.non\.array\}/gms, (localizedProp.type === 'array') ? '' : '$1') - .replace(/\$\{if\.non\.anyOf\}(.*?)\$\{end\.if\.non\.anyOf\}/gms, (localizedProp.anyOf || localizedProp.anyOneOf) ? '' : '$1') + .replace(/\$\{if\.non\.anyOf\}(.*?)\$\{end\.if\.non\.anyOf\}/gms, (localizedProp.anyOf || localizedProp.oneOf) ? '' : '$1') .replace(/\$\{if\.non\.const\}(.*?)\$\{end\.if\.non\.const\}/gms, (typeof localizedProp.const === 'string') ? '' : '$1') let baseTitle = options.property @@ -509,11 +509,11 @@ const insertPrimitiveMacros = (content, schema, module, name, templateDir) => { return content } -const insertAnyOfMacros = (content, schema, module, name) => { +const insertAnyOfMacros = (content, schema, module, name, namespace) => { const itemTemplate = content if (content.split('\n').find(line => line.includes("${type}"))) { content = schema.anyOf.map((item, i) => itemTemplate - .replace(/\$\{type\}/g, getSchemaType(item, module)) + .replace(/\$\{type\}/g, getSchemaType(item, module, { namespace })) .replace(/\$\{delimiter\}(.*?)\$\{end.delimiter\}/g, i === schema.anyOf.length - 1 ? '' : '$1') ).join('') } @@ -537,7 +537,7 @@ const sanitize = (schema) => { return result } -function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', parent = '', property = '', required = false, parentLevel = 0, level = 0, summary, descriptions = true, destination, section, enums = true, skipTitleOnce = false, array = false, primitive = false, type = false } = {}) { +function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', parent = '', property = '', required = false, parentLevel = 0, level = 0, summary, descriptions = true, destination, section, enums = true, skipTitleOnce = false, array = false, primitive = false, type = false, namespace=true } = {}) { schema = sanitize(schema) state.destination = destination state.section = section @@ -560,7 +560,7 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', paren if (schema['$ref']) { const someJson = getReferencedSchema(schema['$ref'], module) if (someJson) { - return getSchemaShape(someJson, module, { templateDir, parent, property, required, parentLevel, level, summary, descriptions, destination, enums, array, primitive }) + return getSchemaShape(someJson, module, { templateDir, parent, property, required, parentLevel, level, summary, descriptions, destination, enums, array, primitive, namespace }) } throw "Unresolvable $ref: " + schema['$ref'] + ", in " + moduleTitle } @@ -571,7 +571,7 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', paren else if (!skipTitleOnce && (level > 0) && schema.title) { let enumType = (schema.type === 'string' && Array.isArray(schema.enum)) // TODO: allow the 'ref' template to actually insert the shape using getSchemaShape - const innerShape = getSchemaShape(schema, module, { skipTitleOnce: true, templateDir, parent, property, required, parentLevel, level, summary, descriptions, destination, enums: enumType, array, primitive }) + const innerShape = getSchemaShape(schema, module, { skipTitleOnce: true, templateDir, parent, property, required, parentLevel, level, summary, descriptions, destination, enums: enumType, array, primitive, namespace }) const shape = getTemplate(path.join(templateDir, 'ref' + suffix)) .replace(/\$\{shape\}/g, innerShape) @@ -583,15 +583,15 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', paren 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, required }) + shape = insertObjectAdditionalPropertiesMacros(additionalPropertiesTemplate, schema, module, theTitle, { level, parent, templateDir, namespace, required }) } else { const patternPropertiesTemplate = getTemplate(path.join(templateDir, 'patternProperties')) if (patternPropertiesTemplate && schema.patternProperties) { - shape = insertObjectPatternPropertiesMacros(patternPropertiesTemplate, schema, module, theTitle, { level, parent, templateDir, namespace: true, required }) + shape = insertObjectPatternPropertiesMacros(patternPropertiesTemplate, schema, module, theTitle, { level, parent, templateDir, namespace, required }) } else { let objectLevel = array ? 0 : level - shape = insertObjectMacros(getTemplate(path.join(templateDir, 'object' + (array ? '-array' : '') + suffix)) || genericTemplate, schema, module, theTitle, property, { parentLevel, level: objectLevel, parent, property, required, templateDir, descriptions, destination, section, enums, namespace: true, primitive }) + shape = insertObjectMacros(getTemplate(path.join(templateDir, 'object' + (array ? '-array' : '') + suffix)) || genericTemplate, schema, module, theTitle, property, { parentLevel, level: objectLevel, parent, property, required, templateDir, descriptions, destination, section, enums, namespace, primitive }) } } result = result.replace(/\$\{shape\}/g, shape) @@ -605,14 +605,14 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', paren const template = getTemplate(path.join(templateDir, 'anyOfSchemaShape' + suffix)) let shape if (template) { - shape = insertAnyOfMacros(template, schema, module, theTitle) + shape = insertAnyOfMacros(template, schema, module, theTitle, namespace) } 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)) || genericTemplate, schema, module, theTitle) + shape = insertAnyOfMacros(getTemplate(path.join(templateDir, 'anyOf' + suffix)) || genericTemplate, schema, module, theTitle, namespace) } if (shape) { result = result.replace(/\$\{shape\}/g, shape) @@ -644,7 +644,7 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', paren } delete union['$ref'] - return getSchemaShape(union, module, { templateDir, parent, property, required, parentLevel, level, summary, descriptions, destination, enums: false, array, primitive }) + return getSchemaShape(union, module, { templateDir, parent, property, required, parentLevel, level, summary, descriptions, destination, enums: false, array, primitive, namespace }) } else if (schema.type === "array" && schema.items && isSupportedTuple(schema)) { // tuple @@ -654,7 +654,7 @@ function getSchemaShape(schema = {}, module = {}, { templateDir = 'types', paren } else if (schema.type === "array" && schema.items && !Array.isArray(schema.items)) { // array - const items = getSchemaShape(schema.items, module, { templateDir, parent, property, required, parentLevel: parentLevel + 1, level, summary, descriptions, destination, enums: false, array: true, primitive }) + const items = getSchemaShape(schema.items, module, { templateDir, parent, property, required, parentLevel: parentLevel + 1, level, summary, descriptions, destination, enums: false, array: true, primitive, namespace }) const shape = insertArrayMacros(getTemplate(path.join(templateDir, 'array' + suffix)) || genericTemplate, schema, module, level, items, Array.isArray(required) ? required.includes(property) : required) result = result.replace(/\$\{shape\}/g, shape) .replace(/\$\{if\.object\}(.*?)\$\{end\.if\.object\}/gms, isObject(schema.items) ? '$1' : '') @@ -720,7 +720,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 } = {}) { +function getSchemaType(schema, module, { destination, templateDir = 'types', link = false, code = false, asPath = false, event = false, result = false, expandEnums = true, baseUrl = '', namespace = true } = {}) { const wrap = (str, wrapper) => wrapper + str + wrapper schema = sanitize(schema) @@ -736,8 +736,7 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin if (schema['$ref']) { const refSchema = getReferencedSchema(schema['$ref'], module) if (refSchema) { - const includeNamespace = (moduleTitle !== getXSchemaGroup(refSchema, module)) - return getSchemaType(refSchema, module, {destination, templateDir, link, 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 })// { 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? @@ -855,7 +854,7 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin schema.anyOf = schema.oneOf } // 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) + const shape = insertAnyOfMacros(getTemplate(path.join(templateDir, 'anyOf' + suffix)), schema, module, theTitle, namespace) return insertSchemaMacros(shape, schema, module, { name: theTitle, recursive: false })