Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(custom-resources): does not accept strings where numbers are expected, skips recursive types #27112

Merged
merged 11 commits into from
Sep 14, 2023
78 changes: 52 additions & 26 deletions scripts/update-sdkv3-parameters-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ async function main(argv: string[]) {
const dir = argv[0];

const blobMapping: TypeCoercionMap = {};
const numberMapping: TypeCoercionMap = {};

for (const entry of await fs.readdir(dir, { withFileTypes: true, encoding: 'utf-8' })) {
if (entry.isFile() && entry.name.endsWith('.json')) {
Expand All @@ -25,7 +26,7 @@ async function main(argv: string[]) {
continue;
}
try {
await doFile(blobMapping, contents);
await doFile(blobMapping, numberMapping, contents);
} catch (e) {
throw new Error(`Error handling ${entry.name}: ${e}`);
}
Expand All @@ -39,13 +40,16 @@ async function main(argv: string[]) {
// Sort the map so we're independent of the order the OS gave us the files in, or what the filenames are
const sortedMapping = Object.fromEntries(Object.entries(blobMapping).sort(sortByKey));

renderMappingToTypeScript(sortedMapping);
console.log(JSON.stringify({
uint88ArrayParameters: blobMapping,
numberParameters: numberMapping,
}))
}

/**
* Recurse through all the types of a singly Smithy model, and record the blobs
*/
async function doFile(blobMap: TypeCoercionMap, model: SmithyFile) {
async function doFile(blobMap: TypeCoercionMap, numberMap: TypeCoercionMap, model: SmithyFile) {
const shapes = model.shapes;

const service = Object.values(shapes).find(isShape('service'));
Expand Down Expand Up @@ -89,9 +93,16 @@ async function doFile(blobMap: TypeCoercionMap, model: SmithyFile) {
addToBlobs(opName, memberPath);
return;
}

if (isNumber(shape)) {
addToNumbers(opName, memberPath);
return;
}

if (isShape('structure')(shape) || isShape('union')(shape)) {
const allKeys = Object.keys(shape.members ?? {}).sort();
for (const [field, member] of Object.entries(shape.members ?? {}).sort(sortByKey)) {
recurse(member.target, opName, [...memberPath, field], seen);
recurse(member.target, opName, [...memberPath, uniquePrefix(field, allKeys)], seen);
}
return;
}
Expand All @@ -106,6 +117,19 @@ async function doFile(blobMap: TypeCoercionMap, model: SmithyFile) {
}
}

/**
* Return the shortest prefix of 'name' that is unique among 'names'
*/
function uniquePrefix(name: string, names: string[]): string {
for (let i = 1; i < name.length; i++) {
const slice = name.substring(0, i);
if (names.filter((n) => n.startsWith(slice)).length === 1) {
return slice;
}
}
return name;
}

function addToBlobs(opName: string, memberPath: string[]) {
if (!blobMap[shortName]) {
blobMap[shortName] = {};
Expand All @@ -115,6 +139,16 @@ async function doFile(blobMap: TypeCoercionMap, model: SmithyFile) {
}
blobMap[shortName][opName].push(memberPath.join('.'));
}

function addToNumbers(opName: string, memberPath: string[]) {
if (!numberMap[shortName]) {
numberMap[shortName] = {};
}
if (!numberMap[shortName][opName]) {
numberMap[shortName][opName] = [];
}
numberMap[shortName][opName].push(memberPath.join('.'));
}
}

interface TypeCoercionMap {
Expand All @@ -137,7 +171,9 @@ type SmithyShape =
| { type: string }
;

interface SmithyTarget { target: string };
interface SmithyTarget {
target: string
}

interface SmithyTraits {
'aws.api#service'?: {
Expand All @@ -155,29 +191,19 @@ function isShape<A extends string>(key: A) {
return (x: SmithyShape): x is Extract<SmithyShape, { type: A }> => x.type === key;
}

function sortByKey<A>(e1: [string, A], e2: [string, A]) {
return e1[0].localeCompare(e2[0]);
function isNumber(shape: SmithyShape): boolean {
return isShape('integer')(shape) ||
isShape('float')(shape) ||
isShape('double')(shape) ||
isShape('long')(shape) ||
isShape('short')(shape) ||
isShape('bigInteger')(shape) ||
isShape('bigDecimal')(shape) ||
isShape('byte')(shape);
}

/**
* Render the given mapping to a TypeScript source file
*/
function renderMappingToTypeScript(blobMap: TypeCoercionMap) {
const lines = new Array<string>();

lines.push(
`// This file was generated from the aws-sdk-js-v3 at ${new Date()}`,
'/* eslint-disable quote-props,comma-dangle */',
'export interface TypeCoercionMap {',
' [service: string]: {',
' [action: string]: string[]',
' }',
'};'
);

lines.push('export const UINT8ARRAY_PARAMETERS: TypeCoercionMap = ' + JSON.stringify(blobMap, undefined, 2).replace(/"/g, '\'') + ';');

console.log(lines.join('\n'));
function sortByKey<A>(e1: [string, A], e2: [string, A]) {
return e1[0].localeCompare(e2[0]);
}

main(process.argv.slice(2)).catch((e) => {
Expand Down
Loading