-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compiler): allow explicit lift qualifications of preflight objec…
…ts (#5935) Fixes: #76 Creates a `lift` builtin function that can be used in inflight code to explicitly add lift qualifications to a method: ```wing bring cloud; let bucket = new cloud.Bucket(); bucket.addObject("k", "value"); let some_ops = ["put", "list"]; // We can define a list of ops in preflight code to be used when explicitly qualifying a lift class Foo { pub inflight mehtod() { lift(bucket, some_ops); // Explicitly add some permissions to `bucket` using a preflight expression lift(bucket, ["delete"]); // Add more permissions to bucket using a literal log(bucket.get("k")); // Good old implicit qualification adds `get` permissions let b = bucket; // We can now use an inflight variable `b` to reference a preflight object `bucket` b.put("k2", "value2"); // We don't get a compiler error here, because explicit lifts are being used in the method disabling compiler qualification errors for k in b.list() { // `list` works on `bucket` because of explicit qualification and `b` references `bucket` log(k); } b.delete("k2"); // `delete` also works because of explicit qualification assert(bucket.tryGet("k2") == nil); `yay!` } } let foo = new Foo(); test "a test" { foo.mehtod(); } ``` ## 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
1 parent
ed54e0c
commit d55ea52
Showing
48 changed files
with
932 additions
and
164 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
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,40 @@ | ||
bring cloud; | ||
|
||
let bucket = new cloud.Bucket(); | ||
|
||
let prelight_string = "hi"; | ||
|
||
class Foo { | ||
pub inflight mehtod1() { | ||
let b = bucket; | ||
lift(b, ["put"]); // Explicit qualification with inflight object, lift call as non first statement | ||
// ^ Expected a preflight object as first argument to `lift` builtin, found inflight expression instead | ||
//^^^^^^^^^^^^^^^ lift() calls must be at the top of the method | ||
|
||
lift(prelight_string, ["contains"]); // Explicit qualification on preflight non-class | ||
// ^^^^^^^^^^^^^^^ Expected type to be "Resource", but got "str" instead | ||
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lift() calls must be at the top of the method | ||
|
||
let inflight_qualifier = "delete"; | ||
lift(bucket, [inflight_qualifier]); // Explicit qualification with inflight qualifiers, lift call as non first statement | ||
// ^^^^^^^^^^^^^^^^^^^^ Qualification list must not contain any inflight elements | ||
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lift() calls must be at the top of the method | ||
|
||
let inner_closure = () => { | ||
lift(bucket, ["get"]); // lift() call in inner closure | ||
//^^^^^^^^^^^^^^^^^^^^ lift() calls are only allowed in inflight methods and closures defined in preflight | ||
}; | ||
class Bar { | ||
pub inflight method() { | ||
lift(bucket, ["get"]); // lift() call in inner class | ||
//^^^^^^^^^^^^^^^^^^^^ lift() calls are only allowed in inflight methods and closures defined in preflight | ||
} | ||
} | ||
} | ||
|
||
pub inflight method2() { | ||
let b = bucket; | ||
b.put("k", "v"); // With no explicit qualification this should be an error | ||
//^ Expression of type "Bucket" references an unknown preflight object | ||
} | ||
} |
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,43 @@ | ||
bring cloud; | ||
|
||
let bucket = new cloud.Bucket(); | ||
bucket.addObject("k", "value"); | ||
|
||
let put_and_list = ["put", "list"]; | ||
|
||
class Foo { | ||
pub inflight mehtod() { | ||
lift(bucket, put_and_list); // Qualify `bucket` with a preflight expression | ||
lift(bucket, ["delete"]); // Qualify `bucket` with `delete` via literal | ||
let b = bucket; // Assign `bucket` to an inflight variable | ||
|
||
// `put` should work on `b` since we explicitly qualified `bucket` with `put` | ||
// no error generated here because of use of `lift()` in this method | ||
b.put("k2", "value2"); | ||
|
||
// validate `put` worked and that we can also `list` | ||
assert(b.list() == ["k", "k2"]); | ||
|
||
// Validate `delete` works | ||
b.delete("k2"); | ||
assert(bucket.tryGet("k2") == nil); | ||
} | ||
} | ||
|
||
let foo = new Foo(); | ||
|
||
test "explicit method lift qualification" { | ||
foo.mehtod(); | ||
} | ||
|
||
// Similar to the above test, but using a closure | ||
let inflight_closure = inflight () => { | ||
lift(bucket, ["put"]); | ||
let b = bucket; | ||
b.put("k3", "value3"); // Use inflight expression to access explicitly qualified `bucket` | ||
assert(bucket.get("k3") == "value3"); | ||
}; | ||
|
||
test "explicit closure lift qualification" { | ||
inflight_closure(); | ||
} |
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
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
Oops, something went wrong.