aws-cdk-lib
Version:
Version 2 of the AWS Cloud Development Kit library
8 lines (7 loc) • 2.46 kB
JavaScript
Object.defineProperty(exports,"__esModule",{value:!0}),exports.matchSection=matchSection,exports.formatAllMatches=formatAllMatches,exports.formatAllMismatches=formatAllMismatches,exports.formatSectionMatchFailure=formatSectionMatchFailure,exports.formatFailure=formatFailure,exports.filterLogicalId=filterLogicalId;var sorting_1=()=>{var tmp=require("./sorting");return sorting_1=()=>tmp,tmp},match_1=()=>{var tmp=require("../match");return match_1=()=>tmp,tmp},matcher_1=()=>{var tmp=require("../matcher");return matcher_1=()=>tmp,tmp};function matchSection(section,props){const matcher=matcher_1().Matcher.isMatcher(props)?props:match_1().Match.objectLike(props),matching={},analyzed={},failures=new Array;return eachEntryInSection(section,(logicalId,entry)=>{analyzed[logicalId]=entry;const result=matcher.test(entry);result.finished(),result.hasFailed()?failures.push([logicalId,result]):matching[logicalId]=entry}),Object.keys(matching).length>0?{match:!0,matches:matching,analyzedCount:Object.keys(analyzed).length,analyzed}:(failures.sort((0,sorting_1().sortKeyComparator)(([logicalId,result])=>[result.failCost,logicalId])),{match:!1,closestResults:Object.fromEntries(failures.slice(0,3)),analyzedCount:Object.keys(analyzed).length,analyzed})}function eachEntryInSection(section,cb){for(const logicalId of Object.keys(section??{})){const resource=section[logicalId];cb(logicalId,resource)}}function formatAllMatches(matches){return[leftPad(JSON.stringify(matches,void 0,2))].join(`
`)}function formatAllMismatches(analyzed,matches={}){return["The following resources do not match the given definition:",...Object.keys(analyzed).filter(id=>!(id in matches)).map(id=>` ${id}`)].join(`
`)}function formatSectionMatchFailure(qualifier,result,what="Template"){return[`${what} has ${result.analyzedCount} ${qualifier}`,result.analyzedCount>0?", but none match as expected":"",`.
`,formatFailure(result.closestResults)].join("")}function formatFailure(closestResults){const keys=Object.keys(closestResults);return keys.length===0?"No matches found":[`The ${keys.length} closest matches:`,...keys.map(key=>`${key} :: ${closestResults[key].renderMismatch()}`)].join(`
`)}function leftPad(x,indent=2){const pad=" ".repeat(indent);return pad+x.split(`
`).join(`
${pad}`)}function filterLogicalId(section,logicalId){return logicalId==="*"?section:Object.entries(section??{}).filter(([k,_])=>k===logicalId).reduce((agg,[k,v])=>({...agg,[k]:v}),{})}
;