UNPKG

wrekenfile-converter

Version:

Convert OpenAPI and Postman specs into Wrekenfiles, with chunking for vector database storage

112 lines 3.72 kB
"use strict"; /** * Utilities for working with STRUCTS in generated Wrekenfiles. * - Filters out unused structs (no STRUCT(...) reference anywhere) */ Object.defineProperty(exports, "__esModule", { value: true }); exports.filterStructsByUsage = filterStructsByUsage; /** * Extract all struct names from a type string, e.g.: * - "STRUCT(User)" -> ["User"] * - "[]STRUCT(model.User)" -> ["model.User"] * - "map[string]STRUCT(api.Request)" -> ["api.Request"] */ function extractAllStructNames(typeString) { const names = []; const structPattern = /STRUCT\(([^)]+)\)/g; let match; while ((match = structPattern.exec(typeString)) !== null) { if (match[1]) { names.push(match[1]); } } return names; } /** * Filter STRUCTS map on a Wrekenfile object to only keep structs that are actually used. * Usage is any occurrence of STRUCT(Name) in: * - METHOD.INPUTS / RETURNS / ERRORS * - HTTP.BODY.TYPE * - ASYNC.RESULT.TYPE * plus transitive references inside other structs. */ function filterStructsByUsage(wrekenfile) { if (!wrekenfile || !wrekenfile.STRUCTS) return; const structs = wrekenfile.STRUCTS; const used = new Set(); const addFromType = (typeVal) => { if (!typeVal || typeof typeVal !== 'string') return; for (const name of extractAllStructNames(typeVal)) { if (name) used.add(name); } }; // Collect from METHODS const methods = wrekenfile.METHODS || {}; for (const methodData of Object.values(methods)) { // INPUTS if (Array.isArray(methodData.INPUTS)) { for (const input of methodData.INPUTS) { for (const value of Object.values(input)) { if (typeof value === 'string') { addFromType(value); } else if (value && typeof value === 'object') { addFromType(value.TYPE || value.type); } } } } // RETURNS if (Array.isArray(methodData.RETURNS)) { for (const ret of methodData.RETURNS) { addFromType(ret.RETURNTYPE); } } // ERRORS if (Array.isArray(methodData.ERRORS)) { for (const err of methodData.ERRORS) { addFromType(err.TYPE); } } // HTTP.BODY.TYPE if (methodData.HTTP && methodData.HTTP.BODY) { addFromType(methodData.HTTP.BODY.TYPE); } // ASYNC.RESULT.TYPE if (methodData.ASYNC && methodData.ASYNC.RESULT) { addFromType(methodData.ASYNC.RESULT.TYPE); } } // Transitive closure: structs referenced from other structs let changed = true; while (changed) { changed = false; for (const [name, fields] of Object.entries(structs)) { if (!used.has(name)) continue; for (const field of fields || []) { const t = (field && (field.TYPE || field.type)); if (!t) continue; for (const nested of extractAllStructNames(t)) { if (nested && !used.has(nested)) { used.add(nested); changed = true; } } } } } // Rebuild STRUCTS with only used entries const filtered = {}; for (const [name, def] of Object.entries(structs)) { if (used.has(name)) { filtered[name] = def; } } wrekenfile.STRUCTS = filtered; } //# sourceMappingURL=struct-utils.js.map