Skip to content

Commit

Permalink
Remove reference to non-existent types from CLIENT_TYPES_MAP records (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Jan 6, 2023
1 parent b98c59a commit ac605d5
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-foxes-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Remove reference to non-existent types from CLIENT_TYPES_MAP records
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import { hasReferenceToKeysInValues } from "./hasReferenceToKeysInValues";
import { hasKeyReferenceInClientTypesMap } from "./hasKeyReferenceInClientTypesMap";
import { hasKeyReferenceInValue } from "./hasKeyReferenceinValue";

/**
* Checks if any of the values have reference to key, and replaces them recursively.
*/
export const getClientTypesMapWithKeysRemovedFromValues = (
clientTypesMap: Record<string, string>
) => {
if (!hasReferenceToKeysInValues(clientTypesMap)) {
if (!hasKeyReferenceInClientTypesMap(clientTypesMap)) {
return clientTypesMap;
}

const newClientTypeMap = {};
const keys = Object.keys(clientTypesMap);

for (const [key, value] of Object.entries(clientTypesMap)) {
const refs = keys.filter((key) => value.includes(`<${key}>`));
const refs = keys.filter((key) => hasKeyReferenceInValue(key, value));
if (refs.length === 0) {
newClientTypeMap[key] = value;
} else {
newClientTypeMap[key] = refs.reduce(
(acc, ref) => acc.replace(`<${ref}>`, `<${clientTypesMap[ref]}>`),
(acc, ref) =>
acc
.replace(`Array<${ref}>`, `Array<${clientTypesMap[ref]}>`)
.replace(`Record<string, ${ref}>`, `Record<string, ${clientTypesMap[ref]}>`),
value
);
}
Expand Down
15 changes: 15 additions & 0 deletions scripts/generateClientTypesMap/hasKeyReferenceInClientTypesMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { hasKeyReferenceInValue } from "./hasKeyReferenceinValue";

/**
* Checks if any of the values in the clientTypesMap has a reference to any of the keys.
*/
export const hasKeyReferenceInClientTypesMap = (clientTypesMap: Record<string, string>) => {
const keys = Object.keys(clientTypesMap);
for (const value of Object.values(clientTypesMap)) {
if (keys.some((key) => hasKeyReferenceInValue(key, value))) {
// console.log({ keys, value });
return true;
}
}
return false;
};
8 changes: 8 additions & 0 deletions scripts/generateClientTypesMap/hasKeyReferenceinValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Returns true if value contains reference to key.
*
* Reference is defined as key name inside brackets in Array/Record type
* i.e. `Array<key>` or `Record<string, key>` for `key`.
*/
export const hasKeyReferenceInValue = (key: string, value: string) =>
value.includes(`Array<${key}>`) || value.includes(`Record<string, ${key}>`);
13 changes: 0 additions & 13 deletions scripts/generateClientTypesMap/hasReferenceToKeysInValues.ts

This file was deleted.

16 changes: 8 additions & 8 deletions src/transforms/v2-to-v3/config/CLIENT_TYPES_MAP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ export const CLIENT_TYPES_MAP: Record<string, Record<string, string>> = {
ComponentEvents: "Record<string, ComponentEvent>",
ComponentList: "Array<Component>",
ComponentName: "string",
ComponentOverrides: "Record<string, ComponentOverridesValue>",
ComponentOverrides: "Record<string, Record<string, string>>",
ComponentOverridesValue: "Record<string, string>",
ComponentProperties: "Record<string, ComponentProperty>",
ComponentPropertyList: "Array<ComponentProperty>",
Expand Down Expand Up @@ -1228,7 +1228,7 @@ export const CLIENT_TYPES_MAP: Record<string, Record<string, string>> = {
ConfigurationEventTime: "Date",
CustomComponentName: "string",
CWEMonitorEnabled: "boolean",
DetectedWorkload: "Record<string, WorkloadMetaData>",
DetectedWorkload: "Record<string, Record<string, string>>",
EbsCause: "string",
EbsEvent: "string",
EbsRequestId: "string",
Expand Down Expand Up @@ -5835,7 +5835,7 @@ export const CLIENT_TYPES_MAP: Record<string, Record<string, string>> = {
Boolean: "boolean",
ByteValue: "number",
DatasourcePackageIngestDetails: "Record<string, DatasourcePackageIngestDetail>",
DatasourcePackageIngestHistory: "Record<string, LastIngestStateChangeDates>",
DatasourcePackageIngestHistory: "Record<string, Record<string, TimestampForCollection>>",
DatasourcePackageIngestStates: "Record<string, DatasourcePackageIngestState>",
DatasourcePackageList: "Array<DatasourcePackage>",
EmailAddress: "string",
Expand Down Expand Up @@ -14078,7 +14078,7 @@ export const CLIENT_TYPES_MAP: Record<string, Record<string, string>> = {
RuntimeHintValuesList: "Array<RuntimeHintValue>",
SensitiveNonEmptyString: "string",
SessionId: "string",
SlotHintsIntentMap: "Record<string, SlotHintsSlotMap>",
SlotHintsIntentMap: "Record<string, Record<string, RuntimeHintDetails>>",
SlotHintsSlotMap: "Record<string, RuntimeHintDetails>",
Slots: "Record<string, Slot>",
String: "string",
Expand Down Expand Up @@ -15502,8 +15502,8 @@ export const CLIENT_TYPES_MAP: Record<string, Record<string, string>> = {
__mapOf__string: "Record<string, string>",
__string: "string",
__timestampUnix: "Date",
ConfigurationAliasesRequest: "Record<string, __mapOf__string>",
ConfigurationAliasesResponse: "Record<string, __mapOf__string>",
ConfigurationAliasesRequest: "Record<string, Record<string, string>>",
ConfigurationAliasesResponse: "Record<string, Record<string, string>>",
HttpPackageConfigurations: "Array<HttpPackageConfiguration>",
Integer: "number",
MaxResults: "number",
Expand Down Expand Up @@ -21994,7 +21994,7 @@ export const CLIENT_TYPES_MAP: Record<string, Record<string, string>> = {
AccessTypeList: "Array<AccessType>",
AccountList: "Array<string>",
AccountSourcesList: "Array<AccountSources>",
AllDimensionsMap: "Record<string, TwoDimensionsMap>",
AllDimensionsMap: "Record<string, Record<string, Array<string>>>",
AutoEnableNewRegionConfigurationList: "Array<AutoEnableNewRegionConfiguration>",
AwsAccountId: "string",
AwsSourceTypeList: "Array<AwsLogSourceType>",
Expand All @@ -22013,7 +22013,7 @@ export const CLIENT_TYPES_MAP: Record<string, Record<string, string>> = {
LogsStatusList: "Array<LogsStatus>",
Long: "number",
RegionSet: "Array<Region>",
RegionSourceTypesAccountsList: "Array<Record<string, TwoDimensionsMap>>",
RegionSourceTypesAccountsList: "Array<Record<string, Record<string, Array<string>>>>",
RetentionSettingList: "Array<RetentionSetting>",
RetentionSettingRetentionPeriodInteger: "number",
RoleArn: "string",
Expand Down

0 comments on commit ac605d5

Please sign in to comment.