Skip to content

Commit

Permalink
Moved information about the type inside the value
Browse files Browse the repository at this point in the history
  • Loading branch information
otaviomacedo committed Sep 6, 2023
1 parent 3b4cc04 commit ea728ff
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31679,18 +31679,20 @@ function parseJsonPayload(payload) {
return payload;
}
}
function decodeParameters(obj, specialTypes = {}) {
function decodeParameters(obj) {
return Object.fromEntries(Object.entries(obj).map(([key, value]) => {
try {
return [key, decodeValue(value, specialTypes[key])];
return [key, decodeValue(value)];
} catch {
return [key, value];
}
}));
}
function decodeValue(value, specialType) {
if (specialType === "ArrayBufferView") {
return new TextEncoder().encode(value);
function decodeValue(value) {
if (value != null && !Array.isArray(value) && typeof value === "object") {
if (value.$type === "ArrayBufferView") {
return new TextEncoder().encode(value.string);
}
}
return JSON.parse(value);
}
Expand Down Expand Up @@ -31747,7 +31749,7 @@ var AwsApiCallHandler = class extends CustomResourceHandler {
const sdkPkg = getServicePackage(request2.service);
const client = getServiceClient(sdkPkg);
const Command = getSdkCommand(sdkPkg, request2.api);
const commandInput = (request2.parameters && decodeParameters(request2.parameters, request2.specialTypes)) ?? {};
const commandInput = (request2.parameters && decodeParameters(request2.parameters)) ?? {};
console.log(`SDK request to ${sdkPkg.service}.${request2.api} with parameters ${JSON.stringify(commandInput)}`);
const response = await client.send(new Command(commandInput));
if (response.Payload) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
{
"version": "34.0.0",
"files": {
"6074e2eaa531b0ceff09fcd8268e8d021a94df977a63ccc572404a8c8e4e70cc": {
"020364985d409cf8bf611edf535aedca3c0d7c0dd7f21ba847b79a41d9bf0dce": {
"source": {
"path": "asset.6074e2eaa531b0ceff09fcd8268e8d021a94df977a63ccc572404a8c8e4e70cc.bundle",
"path": "asset.020364985d409cf8bf611edf535aedca3c0d7c0dd7f21ba847b79a41d9bf0dce.bundle",
"packaging": "zip"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "6074e2eaa531b0ceff09fcd8268e8d021a94df977a63ccc572404a8c8e4e70cc.zip",
"objectKey": "020364985d409cf8bf611edf535aedca3c0d7c0dd7f21ba847b79a41d9bf0dce.zip",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
},
"8e6b4e24a29425a4a249361002d401a788e300129c05040f677749b6d1daad4f": {
"d10734fe0522d2d30eb5916fd9a45d4ddf9ec42fd308c30e000cbd7ad11f7a39": {
"source": {
"path": "integtestDefaultTestDeployAssert24D5C536.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "8e6b4e24a29425a4a249361002d401a788e300129c05040f677749b6d1daad4f.json",
"objectKey": "d10734fe0522d2d30eb5916fd9a45d4ddf9ec42fd308c30e000cbd7ad11f7a39.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
]
},
"InvocationType": "\"RequestResponse\"",
"Payload": "{\"name\":\"world\"}"
"Payload": {
"$type": "ArrayBufferView",
"string": "{\"name\":\"world\"}"
}
},
"flattenResponse": "false",
"salt": "1693993263186",
"specialTypes": {
"Payload": "ArrayBufferView"
}
"salt": "1693996483504"
},
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
Expand Down Expand Up @@ -95,7 +95,7 @@
"S3Bucket": {
"Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}"
},
"S3Key": "6074e2eaa531b0ceff09fcd8268e8d021a94df977a63ccc572404a8c8e4e70cc.zip"
"S3Key": "020364985d409cf8bf611edf535aedca3c0d7c0dd7f21ba847b79a41d9bf0dce.zip"
},
"Timeout": 120,
"Handler": "index.handler",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"validateOnSynth": false,
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}",
"cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/8e6b4e24a29425a4a249361002d401a788e300129c05040f677749b6d1daad4f.json",
"stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/d10734fe0522d2d30eb5916fd9a45d4ddf9ec42fd308c30e000cbd7ad11f7a39.json",
"requiresBootstrapStackVersion": 6,
"bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version",
"additionalDependencies": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class AwsApiCallHandler extends CustomResourceHandler<AwsApiCallRequest,
const client = getServiceClient(sdkPkg);

const Command = getSdkCommand(sdkPkg, request.api);
const commandInput = (request.parameters && decodeParameters(request.parameters, request.specialTypes)) ?? {};
const commandInput = (request.parameters && decodeParameters(request.parameters)) ?? {};

console.log(`SDK request to ${sdkPkg.service}.${request.api} with parameters ${JSON.stringify(commandInput)}`);
const response = await client.send(new Command(commandInput));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,6 @@ export interface AwsApiCallRequest {
* @default - return all data
*/
readonly outputPaths?: string[];

/**
* Types that can't be properly serialized with JSON.stringify()
* are marked here. For example, an entry "foo" -> "ArrayBufferView"
* signals to the decoder that "foo" should be handled separately
* and treated as an array of bytes.
*/
readonly specialTypes?: Record<string, string>;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export function parseJsonPayload(payload: any): any {
}
}

export function decodeParameters(obj: Record<string, string>, specialTypes: Record<string, string> = {}): any {
export function decodeParameters(obj: Record<string, any>): any {
return Object.fromEntries(Object.entries(obj).map(([key, value]) => {
try {
return [key, decodeValue(value, specialTypes[key])];
return [key, decodeValue(value)];
} catch {
// if the value cannot be parsed, leave it unchanged
// this will end up as a string
Expand All @@ -21,9 +21,11 @@ export function decodeParameters(obj: Record<string, string>, specialTypes: Reco
}));
}

function decodeValue(value: string, specialType?: string): any {
if (specialType === 'ArrayBufferView') {
return new TextEncoder().encode(value);
function decodeValue(value: any): any {
if (value != null && !Array.isArray(value) && typeof value === 'object') {
if (value.$type === 'ArrayBufferView') {
return new TextEncoder().encode(value.string);
}
}

return JSON.parse(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,26 +231,18 @@ export class AssertionsProvider extends Construct {
}
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, encodeValue(value)]));

function encodeValue(value: any): string {
function encodeValue(value: any): any {
if (ArrayBuffer.isView(value)) {
return new TextDecoder().decode(value as Uint8Array);
return {
$type: 'ArrayBufferView',
string: new TextDecoder().decode(value as Uint8Array),
};
}

return JSON.stringify(value);
}
}

public findSpecialTypes(parameters: any): Record<string, string> {
if (parameters == null) {
return {};
}
return Object.fromEntries(
Object.entries(parameters)
.map(([k, v]) => [k, ArrayBuffer.isView(v) ? 'ArrayBufferView' : undefined])
.filter(([_, v]) => v != null),
);
}

/**
* Create a policy statement from a specific api call
*/
Expand Down
1 change: 0 additions & 1 deletion packages/@aws-cdk/integ-tests-alpha/lib/assertions/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ export class AwsApiCall extends ApiCallBase {
flattenResponse: Lazy.string({ produce: () => this.flattenResponse }),
outputPaths: Lazy.list({ produce: () => this.outputPaths }),
salt: Date.now().toString(),
specialTypes: this.provider.findSpecialTypes(props.parameters),
},
resourceType: `${SDK_RESOURCE_TYPE_PREFIX}${this.name}`.substring(0, 60),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ describe('utils', () => {
test('decode parameters', () => {
const result = decodeParameters({
foo: 'normal string',
bar: 'abc',
bar: { $type: 'ArrayBufferView', string: 'abc' },
zee: '{"hello": "world"}',
}, {
bar: 'ArrayBufferView',
});

expect(result).toEqual({
Expand Down

0 comments on commit ea728ff

Please sign in to comment.