-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: unsafeCast builtin function (#4483)
Fixes #4161 For situations where application code needs to interact with foreign interfaces, or where the compiler cannot infer all type information, most modern languages support some mechanism for "casting" or performing unsafe conversions between types ([1](https://web.mit.edu/rust-lang_v1.25/arch/amd64_ubuntu1404/share/doc/rust/html/book/first-edition/casting-between-types.html), [2](https://pkg.go.dev/unsafe#Pointer), [3](https://www.baeldung.com/java-type-casting), [4](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/typecasting/)). Indeed, we've already identified some use cases in #4161 that require casting, like performing low-level escape hatches in CDK applications. To that end, this PR introduces a global function named `unsafeCast` that allows performing type casting. Below is an example where `unsafeCast` is used to override a piece of low-level Terraform configuration created by `cloud.Bucket`: ```js bring cloud; bring util; bring "@cdktf/provider-aws" as aws; let b = new cloud.Bucket(); if util.env("WING_TARGET") == "tf-aws" { let s3Bucket: aws.s3Bucket.S3Bucket = unsafeCast(b.node.findChild("Default")); s3Bucket.addOverride("bucket_prefix", "my-prefix-"); log(s3Bucket.node.path); } ``` `unsafeCast` is experimental and subject to change. Prior to Wing 1.0, it may be worthwhile to improve support for casting in (at least) two ways: 1. Upgrade `unsafeCast` into a dedicated piece of language syntax. 2. Limit the kinds of casts that are allowed (see comment: #4161 (comment)) ## Checklist - [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted) - [x] Description explains motivation and solution - [x] Tests added (always) - [x] Docs updated (only required for features) - [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing *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
Showing
12 changed files
with
195 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
bring cloud; | ||
bring util; | ||
bring "@cdktf/provider-aws" as aws; | ||
|
||
let b = new cloud.Bucket(); | ||
|
||
if util.env("WING_TARGET") == "tf-aws" { | ||
let s3Bucket: aws.s3Bucket.S3Bucket = unsafeCast(b.node.findChild("Default")); | ||
|
||
s3Bucket.addOverride("bucket_prefix", "my-prefix-"); | ||
log(s3Bucket.node.path); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
tools/hangar/__snapshots__/test_corpus/valid/casting.test.w_compile_tf-aws.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# [casting.test.w](../../../../../examples/tests/valid/casting.test.w) | compile | tf-aws | ||
|
||
## main.tf.json | ||
```json | ||
{ | ||
"//": { | ||
"metadata": { | ||
"backend": "local", | ||
"overrides": { | ||
"aws_s3_bucket": [ | ||
"bucket_prefix" | ||
] | ||
}, | ||
"stackName": "root", | ||
"version": "0.17.0" | ||
}, | ||
"outputs": { | ||
"root": { | ||
"Default": { | ||
"cloud.TestRunner": { | ||
"TestFunctionArns": "WING_TEST_RUNNER_FUNCTION_ARNS" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"output": { | ||
"WING_TEST_RUNNER_FUNCTION_ARNS": { | ||
"value": "[]" | ||
} | ||
}, | ||
"provider": { | ||
"aws": [ | ||
{} | ||
] | ||
}, | ||
"resource": { | ||
"aws_s3_bucket": { | ||
"cloudBucket": { | ||
"//": { | ||
"metadata": { | ||
"path": "root/Default/Default/cloud.Bucket/Default", | ||
"uniqueId": "cloudBucket" | ||
} | ||
}, | ||
"bucket_prefix": "my-prefix-", | ||
"force_destroy": false | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## preflight.js | ||
```js | ||
const $stdlib = require('@winglang/sdk'); | ||
const $plugins = ((s) => !s ? [] : s.split(';'))(process.env.WING_PLUGIN_PATHS); | ||
const $outdir = process.env.WING_SYNTH_DIR ?? "."; | ||
const $wing_is_test = process.env.WING_IS_TEST === "true"; | ||
const std = $stdlib.std; | ||
const cloud = $stdlib.cloud; | ||
const util = $stdlib.util; | ||
const aws = require("@cdktf/provider-aws"); | ||
class $Root extends $stdlib.std.Resource { | ||
constructor(scope, id) { | ||
super(scope, id); | ||
const b = this.node.root.newAbstract("@winglang/sdk.cloud.Bucket",this,"cloud.Bucket"); | ||
if ((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })((util.Util.env("WING_TARGET")),"tf-aws"))) { | ||
const s3Bucket = (b.node.findChild("Default")); | ||
(s3Bucket.addOverride("bucket_prefix","my-prefix-")); | ||
{console.log(s3Bucket.node.path)}; | ||
} | ||
} | ||
} | ||
const $App = $stdlib.core.App.for(process.env.WING_TARGET); | ||
new $App({ outdir: $outdir, name: "casting.test", rootConstruct: $Root, plugins: $plugins, isTestEnvironment: $wing_is_test, entrypointDir: process.env['WING_SOURCE_DIR'], rootId: process.env['WING_ROOT_ID'] }).synth(); | ||
|
||
``` | ||
12 changes: 12 additions & 0 deletions
12
tools/hangar/__snapshots__/test_corpus/valid/casting.test.w_test_sim.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# [casting.test.w](../../../../../examples/tests/valid/casting.test.w) | test | sim | ||
|
||
## stdout.log | ||
```log | ||
pass ─ casting.test.wsim (no tests) | ||
Tests 1 passed (1) | ||
Test Files 1 passed (1) | ||
Duration <DURATION> | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters