Skip to content

Commit

Permalink
fix: unable to lift maps with keys that require quotes (#3825)
Browse files Browse the repository at this point in the history
Fixes #3119

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
  • Loading branch information
MarkMcCulloh authored Aug 15, 2023
1 parent 207a338 commit 2270700
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 11 deletions.
14 changes: 14 additions & 0 deletions examples/tests/valid/captures.w
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,17 @@ new cloud.Function(
handler,
env: emptyEnv
) as "AnotherFunction";


let headers = {
"my-fancy-header" => "my-fancy-value",
"not-even-real\"" => "wow` !",
};
let api = new cloud.Api();
api.get("/hello", inflight (req) => {
return cloud.ApiResponse {
status: 200,
headers: headers,
body: "Hello, world!"
};
});
2 changes: 1 addition & 1 deletion libs/wingsdk/src/core/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function liftObject(scope: IConstruct, obj: any): string {
const lines = [];
lines.push("{");
for (const [k, v] of Object.entries(obj)) {
lines.push(`${k}: ${liftObject(scope, v)},`);
lines.push(`\"${k.replace(/"/g, '\\"')}\": ${liftObject(scope, v)},`);
}
lines.push("}");
return lines.join("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ if (!(this.my_array[1].minutes === 20)) { throw new Error(\`assertion failed: \\
}
};
})())({
my_array: [{seconds: 600,minutes: 10,hours: 0.16666666666666666,},{seconds: 1200,minutes: 20,hours: 0.3333333333333333,}]
my_array: [{\\"seconds\\": 600,\\"minutes\\": 10,\\"hours\\": 0.16666666666666666,},{\\"seconds\\": 1200,\\"minutes\\": 20,\\"hours\\": 0.3333333333333333,}]
})).handle(event);
};",
"simulator.json": {
Expand Down Expand Up @@ -650,7 +650,7 @@ if (!(this.my_capture.hours === 2)) { throw new Error(\`assertion failed: \\"thi
}
};
})())({
my_capture: {seconds: 7200,minutes: 120,hours: 2,}
my_capture: {\\"seconds\\": 7200,\\"minutes\\": 120,\\"hours\\": 2,}
})).handle(event);
};",
"simulator.json": {
Expand Down Expand Up @@ -981,7 +981,7 @@ if (!(this.my_map.get('bar')[1].seconds === 40 * 60)) { throw new Error(\`assert
}
};
})())({
my_map: new Map([[\\"foo\\",[{seconds: 600,minutes: 10,hours: 0.16666666666666666,},{seconds: 1200,minutes: 20,hours: 0.3333333333333333,}]],[\\"bar\\",[{seconds: 1800,minutes: 30,hours: 0.5,},{seconds: 2400,minutes: 40,hours: 0.6666666666666666,}]]])
my_map: new Map([[\\"foo\\",[{\\"seconds\\": 600,\\"minutes\\": 10,\\"hours\\": 0.16666666666666666,},{\\"seconds\\": 1200,\\"minutes\\": 20,\\"hours\\": 0.3333333333333333,}]],[\\"bar\\",[{\\"seconds\\": 1800,\\"minutes\\": 30,\\"hours\\": 0.5,},{\\"seconds\\": 2400,\\"minutes\\": 40,\\"hours\\": 0.6666666666666666,}]]])
})).handle(event);
};",
"simulator.json": {
Expand Down Expand Up @@ -1504,7 +1504,7 @@ if (!(Array.from(this.my_set)[1].seconds === 1200)) { throw new Error(\`assertio
}
};
})())({
my_set: new Set([{seconds: 600,minutes: 10,hours: 0.16666666666666666,},{seconds: 1200,minutes: 20,hours: 0.3333333333333333,}])
my_set: new Set([{\\"seconds\\": 600,\\"minutes\\": 10,\\"hours\\": 0.16666666666666666,},{\\"seconds\\": 1200,\\"minutes\\": 20,\\"hours\\": 0.3333333333333333,}])
})).handle(event);
};",
"simulator.json": {
Expand Down Expand Up @@ -1720,7 +1720,7 @@ if (!(Object.keys(this.my_capture).length === 3)) { throw new Error(\`assertion
}
};
})())({
my_capture: {hello: \\"dude\\",world: \\"cup\\",foo: \\"bar\\",}
my_capture: {\\"hello\\": \\"dude\\",\\"world\\": \\"cup\\",\\"foo\\": \\"bar\\",}
})).handle(event);
};",
"simulator.json": {
Expand Down Expand Up @@ -1829,7 +1829,7 @@ if (!(this.my_struct.bar.get('bar') === 4)) { throw new Error(\`assertion failed
}
};
})())({
my_struct: {foo: new Map([[\\"foo\\",1],[\\"bar\\",2]]),bar: new Map([[\\"foo\\",3],[\\"bar\\",4]]),}
my_struct: {\\"foo\\": new Map([[\\"foo\\",1],[\\"bar\\",2]]),\\"bar\\": new Map([[\\"foo\\",3],[\\"bar\\",4]]),}
})).handle(event);
};",
"simulator.json": {
Expand Down Expand Up @@ -1942,13 +1942,13 @@ if (!(await bar.get(\\"foo\\") === \\"bar\\")) { throw new Error(\`assertion fai
}
};
})())({
my_struct: {bucky: (function(env) {
my_struct: {\\"bucky\\": (function(env) {
let handle = process.env[env];
if (!handle) {
throw new Error(\\"Missing environment variable: \\" + env);
}
return $simulator.findInstance(handle);
})(\\"BUCKET_HANDLE_4fecd6d0\\"),mapy: new Map([[\\"foo\\",(function(env) {
})(\\"BUCKET_HANDLE_4fecd6d0\\"),\\"mapy\\": new Map([[\\"foo\\",(function(env) {
let handle = process.env[env];
if (!handle) {
throw new Error(\\"Missing environment variable: \\" + env);
Expand All @@ -1960,7 +1960,7 @@ my_struct: {bucky: (function(env) {
throw new Error(\\"Missing environment variable: \\" + env);
}
return $simulator.findInstance(handle);
})(\\"BUCKET_HANDLE_0120daf4\\")]]),arry: {boom: [(function(env) {
})(\\"BUCKET_HANDLE_0120daf4\\")]]),\\"arry\\": {\\"boom\\": [(function(env) {
let handle = process.env[env];
if (!handle) {
throw new Error(\\"Missing environment variable: \\" + env);
Expand Down
2 changes: 1 addition & 1 deletion libs/wingsdk/turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"outputs": ["lib/**", ".jsii"]
},
"post-compile": {
"dependsOn": ["compile"],
"dependsOn": ["^compile", "compile"],
"inputs": [".jsii", "src/**/*.md"],
"outputs": ["../../docs/docs/04-standard-library/*/*.md"]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ module.exports = function({ $bucket1, $bucket2, $bucket3 }) {

```

## inflight.$Closure2-1.js
```js
module.exports = function({ $headers }) {
class $Closure2 {
constructor({ }) {
const $obj = (...args) => this.handle(...args);
Object.setPrototypeOf($obj, this);
return $obj;
}
async handle(req) {
return ({"status": 200,"headers": $headers,"body": "Hello, world!"});
}
}
return $Closure2;
}

```

## main.tf.json
```json
{
Expand All @@ -51,6 +69,18 @@ module.exports = function({ $bucket1, $bucket2, $bucket3 }) {
}
}
},
"data": {
"aws_region": {
"Region": {
"//": {
"metadata": {
"path": "root/Default/Region",
"uniqueId": "Region"
}
}
}
}
},
"output": {
"WING_TEST_RUNNER_FUNCTION_ARNS": {
"value": "[]"
Expand All @@ -62,6 +92,48 @@ module.exports = function({ $bucket1, $bucket2, $bucket3 }) {
]
},
"resource": {
"aws_api_gateway_deployment": {
"cloudApi_api_deployment_545514BF": {
"//": {
"metadata": {
"path": "root/Default/Default/cloud.Api/api/deployment",
"uniqueId": "cloudApi_api_deployment_545514BF"
}
},
"lifecycle": {
"create_before_destroy": true
},
"rest_api_id": "${aws_api_gateway_rest_api.cloudApi_api_2B334D75.id}",
"triggers": {
"redeployment": "996be997492f7193f147683623ec3ca01a8e752f"
}
}
},
"aws_api_gateway_rest_api": {
"cloudApi_api_2B334D75": {
"//": {
"metadata": {
"path": "root/Default/Default/cloud.Api/api/api",
"uniqueId": "cloudApi_api_2B334D75"
}
},
"body": "{\"openapi\":\"3.0.3\",\"paths\":{\"/hello\":{\"get\":{\"operationId\":\"get-hello\",\"responses\":{\"200\":{\"description\":\"200 response\",\"content\":{}}},\"parameters\":[],\"x-amazon-apigateway-integration\":{\"uri\":\"arn:aws:apigateway:${data.aws_region.Region.name}:lambda:path/2015-03-31/functions/${aws_lambda_function.cloudApi_cloudApi-OnRequest-86898773_701F5CA7.arn}/invocations\",\"type\":\"aws_proxy\",\"httpMethod\":\"POST\",\"responses\":{\"default\":{\"statusCode\":\"200\"}},\"passthroughBehavior\":\"when_no_match\",\"contentHandling\":\"CONVERT_TO_TEXT\"}}}}}",
"name": "api-c895068c"
}
},
"aws_api_gateway_stage": {
"cloudApi_api_stage_BBB283E4": {
"//": {
"metadata": {
"path": "root/Default/Default/cloud.Api/api/stage",
"uniqueId": "cloudApi_api_stage_BBB283E4"
}
},
"deployment_id": "${aws_api_gateway_deployment.cloudApi_api_deployment_545514BF.id}",
"rest_api_id": "${aws_api_gateway_rest_api.cloudApi_api_2B334D75.id}",
"stage_name": "prod"
}
},
"aws_iam_role": {
"AnotherFunction_IamRole_74447271": {
"//": {
Expand All @@ -72,6 +144,15 @@ module.exports = function({ $bucket1, $bucket2, $bucket3 }) {
},
"assume_role_policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Principal\":{\"Service\":\"lambda.amazonaws.com\"},\"Effect\":\"Allow\"}]}"
},
"cloudApi_cloudApi-OnRequest-86898773_IamRole_6300C24F": {
"//": {
"metadata": {
"path": "root/Default/Default/cloud.Api/cloud.Api-OnRequest-86898773/IamRole",
"uniqueId": "cloudApi_cloudApi-OnRequest-86898773_IamRole_6300C24F"
}
},
"assume_role_policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Principal\":{\"Service\":\"lambda.amazonaws.com\"},\"Effect\":\"Allow\"}]}"
},
"cloudFunction_IamRole_5A4430DC": {
"//": {
"metadata": {
Expand Down Expand Up @@ -102,6 +183,16 @@ module.exports = function({ $bucket1, $bucket2, $bucket3 }) {
"policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Action\":[\"s3:List*\",\"s3:PutObject*\",\"s3:Abort*\",\"s3:GetObject*\",\"s3:GetBucket*\",\"s3:GetBucketPublicAccessBlock\"],\"Resource\":[\"${aws_s3_bucket.cloudBucket.arn}\",\"${aws_s3_bucket.cloudBucket.arn}/*\"],\"Effect\":\"Allow\"},{\"Action\":[\"s3:List*\",\"s3:GetObject*\",\"s3:GetBucket*\",\"s3:GetBucketPublicAccessBlock\"],\"Resource\":[\"${aws_s3_bucket.PublicBucket.arn}\",\"${aws_s3_bucket.PublicBucket.arn}/*\"],\"Effect\":\"Allow\"},{\"Action\":[\"s3:List*\",\"s3:GetObject*\",\"s3:GetBucket*\"],\"Resource\":[\"${aws_s3_bucket.PrivateBucket.arn}\",\"${aws_s3_bucket.PrivateBucket.arn}/*\"],\"Effect\":\"Allow\"}]}",
"role": "${aws_iam_role.AnotherFunction_IamRole_74447271.name}"
},
"cloudApi_cloudApi-OnRequest-86898773_IamRolePolicy_DAC639E5": {
"//": {
"metadata": {
"path": "root/Default/Default/cloud.Api/cloud.Api-OnRequest-86898773/IamRolePolicy",
"uniqueId": "cloudApi_cloudApi-OnRequest-86898773_IamRolePolicy_DAC639E5"
}
},
"policy": "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"none:null\",\"Resource\":\"*\"}]}",
"role": "${aws_iam_role.cloudApi_cloudApi-OnRequest-86898773_IamRole_6300C24F.name}"
},
"cloudFunction_IamRolePolicy_618BF987": {
"//": {
"metadata": {
Expand Down Expand Up @@ -134,6 +225,16 @@ module.exports = function({ $bucket1, $bucket2, $bucket3 }) {
"policy_arn": "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
"role": "${aws_iam_role.AnotherFunction_IamRole_74447271.name}"
},
"cloudApi_cloudApi-OnRequest-86898773_IamRolePolicyAttachment_6E485A17": {
"//": {
"metadata": {
"path": "root/Default/Default/cloud.Api/cloud.Api-OnRequest-86898773/IamRolePolicyAttachment",
"uniqueId": "cloudApi_cloudApi-OnRequest-86898773_IamRolePolicyAttachment_6E485A17"
}
},
"policy_arn": "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
"role": "${aws_iam_role.cloudApi_cloudApi-OnRequest-86898773_IamRole_6300C24F.name}"
},
"cloudFunction_IamRolePolicyAttachment_288B9653": {
"//": {
"metadata": {
Expand Down Expand Up @@ -198,6 +299,32 @@ module.exports = function({ $bucket1, $bucket2, $bucket3 }) {
"subnet_ids": []
}
},
"cloudApi_cloudApi-OnRequest-86898773_701F5CA7": {
"//": {
"metadata": {
"path": "root/Default/Default/cloud.Api/cloud.Api-OnRequest-86898773/Default",
"uniqueId": "cloudApi_cloudApi-OnRequest-86898773_701F5CA7"
}
},
"environment": {
"variables": {
"WING_FUNCTION_NAME": "cloud-Api-OnRequest-86898773-c8ed6547",
"WING_TARGET": "tf-aws"
}
},
"function_name": "cloud-Api-OnRequest-86898773-c8ed6547",
"handler": "index.handler",
"publish": true,
"role": "${aws_iam_role.cloudApi_cloudApi-OnRequest-86898773_IamRole_6300C24F.arn}",
"runtime": "nodejs18.x",
"s3_bucket": "${aws_s3_bucket.Code.bucket}",
"s3_key": "${aws_s3_object.cloudApi_cloudApi-OnRequest-86898773_S3Object_12D28469.key}",
"timeout": 30,
"vpc_config": {
"security_group_ids": [],
"subnet_ids": []
}
},
"cloudFunction": {
"//": {
"metadata": {
Expand Down Expand Up @@ -257,6 +384,21 @@ module.exports = function({ $bucket1, $bucket2, $bucket3 }) {
}
}
},
"aws_lambda_permission": {
"cloudApi_api_permission-GET-df16733f_0EEF8FF5": {
"//": {
"metadata": {
"path": "root/Default/Default/cloud.Api/api/permission-GET-df16733f",
"uniqueId": "cloudApi_api_permission-GET-df16733f_0EEF8FF5"
}
},
"action": "lambda:InvokeFunction",
"function_name": "${aws_lambda_function.cloudApi_cloudApi-OnRequest-86898773_701F5CA7.function_name}",
"principal": "apigateway.amazonaws.com",
"source_arn": "${aws_api_gateway_rest_api.cloudApi_api_2B334D75.execution_arn}/*/GET/hello",
"statement_id": "AllowExecutionFromAPIGateway-GET-df16733f"
}
},
"aws_s3_bucket": {
"Code": {
"//": {
Expand Down Expand Up @@ -416,6 +558,17 @@ module.exports = function({ $bucket1, $bucket2, $bucket3 }) {
"key": "<ASSET_KEY>",
"source": "<ASSET_SOURCE>"
},
"cloudApi_cloudApi-OnRequest-86898773_S3Object_12D28469": {
"//": {
"metadata": {
"path": "root/Default/Default/cloud.Api/cloud.Api-OnRequest-86898773/S3Object",
"uniqueId": "cloudApi_cloudApi-OnRequest-86898773_S3Object_12D28469"
}
},
"bucket": "${aws_s3_bucket.Code.bucket}",
"key": "<ASSET_KEY>",
"source": "<ASSET_SOURCE>"
},
"cloudFunction_S3Object_71908BAD": {
"//": {
"metadata": {
Expand Down Expand Up @@ -499,6 +652,37 @@ class $Root extends $stdlib.std.Resource {
super._registerBind(host, ops);
}
}
class $Closure2 extends $stdlib.std.Resource {
constructor(scope, id, ) {
super(scope, id);
this._addInflightOps("handle", "$inflight_init");
this.display.hidden = true;
}
static _toInflightType(context) {
return $stdlib.core.NodeJsCode.fromInline(`
require("./inflight.$Closure2-1.js")({
$headers: ${context._lift(headers)},
})
`);
}
_toInflight() {
return $stdlib.core.NodeJsCode.fromInline(`
(await (async () => {
const $Closure2Client = ${$Closure2._toInflightType(this).text};
const client = new $Closure2Client({
});
if (client.$inflight_init) { await client.$inflight_init(); }
return client;
})())
`);
}
_registerBind(host, ops) {
if (ops.includes("handle")) {
$Closure2._registerBindObject(headers, host, []);
}
super._registerBind(host, ops);
}
}
const bucket1 = this.node.root.newAbstract("@winglang/sdk.cloud.Bucket",this,"cloud.Bucket");
const bucket2 = this.node.root.newAbstract("@winglang/sdk.cloud.Bucket",this,"PublicBucket",({"public": true}));
const bucket3 = this.node.root.newAbstract("@winglang/sdk.cloud.Bucket",this,"PrivateBucket",{ public: false });
Expand All @@ -508,6 +692,9 @@ class $Root extends $stdlib.std.Resource {
this.node.root.newAbstract("@winglang/sdk.cloud.Function",this,"cloud.Function",handler,{ env: ({}) });
const emptyEnv = ({});
this.node.root.newAbstract("@winglang/sdk.cloud.Function",this,"AnotherFunction",handler,{ env: emptyEnv });
const headers = ({"my-fancy-header": "my-fancy-value","not-even-real\"": "wow` !"});
const api = this.node.root.newAbstract("@winglang/sdk.cloud.Api",this,"cloud.Api");
(api.get("/hello",new $Closure2(this,"$Closure2")));
}
}
const $App = $stdlib.core.App.for(process.env.WING_TARGET);
Expand Down

0 comments on commit 2270700

Please sign in to comment.