aws-cdk-lib
Version:
Version 2 of the AWS Cloud Development Kit library
8 lines (7 loc) • 2.48 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0}),exports.filterLogicalId=exports.formatFailure=exports.formatSectionMatchFailure=exports.formatAllMismatches=exports.formatAllMatches=exports.matchSection=void 0;const sorting_1=require("./sorting"),match_1=require("../match"),matcher_1=require("../matcher");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(sorting_1.sortKeyComparator(([logicalId,result])=>[result.failCost,logicalId])),{match:!1,closestResults:Object.fromEntries(failures.slice(0,3)),analyzedCount:Object.keys(analyzed).length,analyzed})}exports.matchSection=matchSection;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(`
`)}exports.formatAllMatches=formatAllMatches;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(`
`)}exports.formatAllMismatches=formatAllMismatches;function formatSectionMatchFailure(qualifier,result,what="Template"){return[`${what} has ${result.analyzedCount} ${qualifier}`,result.analyzedCount>0?", but none match as expected":"",`.
`,formatFailure(result.closestResults)].join("")}exports.formatSectionMatchFailure=formatSectionMatchFailure;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(`
`)}exports.formatFailure=formatFailure;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}),{})}exports.filterLogicalId=filterLogicalId;