Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk): add Map.tryGet(), Map.get() throws on key not found #3771

Closed

Conversation

giovanni-orciuolo
Copy link

This PR is directly related to this issue: #3575

I modified Map.get() to throw an error if the key is not found, and I added Map.tryGet() which returns nil if the key is not found. I also updated documentation accordingly.

Please note that this new behaviour will break current implementations that are relying on the forced typing of T? as return value for Map.get() (since now it throws on key not found no matter the return type). All these implementations should switch to Map.tryGet(). In alternative, we can inverse the behaviour and make it so that tryGet() throws and Map.get() returns T?.

Checklist

  • Title matches Winglang's style guide
  • Description explains motivation and solution
  • Tests added (always)
  • 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.

@giovanni-orciuolo giovanni-orciuolo requested a review from a team as a code owner August 10, 2023 09:58
Copy link
Contributor

@hasanaburayyan hasanaburayyan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥🚀 awesome job! I added 2 comments feel free to re-request review after you get a chance to review.

examples/tests/valid/container_types.w Outdated Show resolved Hide resolved
libs/wingsdk/src/std/map.ts Show resolved Hide resolved
Chriscbr and others added 3 commits August 10, 2023 14:26
…ng#3743)

Fixes winglang#3640

This change affects a lot of files, so I've implemented the changes and updated examples in the first commit, and all snapshot updates in the second commit.

## 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)
- [ ] 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)*.
@giovanni-orciuolo
Copy link
Author

It seems like hangar tests are failing with the error ERROR: Unexpected token 'throw'. It might be related to the macro expansion invalidating the usage of the throw keyword.

@hasanaburayyan
Copy link
Contributor

It seems like hangar tests are failing with the error ERROR: Unexpected token 'throw'. It might be related to the macro expansion invalidating the usage of the throw keyword.

@giovanni-orciuolo I think its because the macro's function is not wrapped in braces {}

@giovanni-orciuolo
Copy link
Author

I was wondering why the return was always explicit. Ok! I will commit with braces

polamoros and others added 16 commits August 10, 2023 18:31
…named (winglang#3739)

- Set non-user-defined Function resources as children of the related resource.
- Use inflight names for Function resources generated by the compiler.
- Enhance the ELK configuration.
- Fix the issue where the icon for test resources was not being displayed.
- Enable test selection between test explorer and map view.

**Current**
![image](https://github.com/winglang/wing/assets/5547636/e670fb5e-c384-4d86-866a-708c19ce73d6)

**New**
![image](https://github.com/winglang/wing/assets/5547636/7ae9f22d-b342-452b-b3c6-b3d441bf43d1)

*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)*.
In winglang#3743 I introduced some code to avoid name collisions in our code generation by hashing file paths. It seems this was adequate for many cases, but it caused problems for contribution since our `hangar` E2E tests currently compile most Wing programs using absolute paths. That means in these scenarios the hashes will be machine dependent. According to @MarkMcCulloh these tests are needed since there were bugs with absolute paths in the past. To that end, this PR changes the name deduplication strategy to be based around incrementing numbers.

## Checklist

- [ ] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [ ] Description explains motivation and solution
- [ ] Tests added (always)
- [ ] 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)*.
…f-aws` target (winglang#3778)

Closes winglang#3764

*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)*.
…#3776)

This PR changes the _bind methods, making them public and removing the internal tag. It makes more sense to make this method public, as a Wing class can rewrite the method when necessary.

Closes winglang#3775 


## 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)*.
Following @eladb's recommendation in PR winglang#3398, I removed the import of `target-awscdk` and `target-tf-aws`, and checked if the `host` implements the methods of the `IAwsFunction` interface. This way, the error that was occurring due to not having `aws-cdk-lib` globally installed no longer happens.

## 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)
- [ ] 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)*.
## 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)
- [x] 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)*.
Removes the `initialMessages` property from `cloud.Queue` to reduce API surface area in the standard library. This capability has only worked on the `sim` target to date, and it's possible to easily achieve the capability (across clouds) using `cloud.OnDeploy`:

```js
bring cloud;

let queue = new cloud.Queue();
new cloud.OnDeploy(inflight () => {
  queue.push("hello");
  queue.push("world");
});
```

This style of API would have more merit if it's something we could provided an optimized implementation for, but it doesn't appear there are dedicated Terraform resources available that represent AWS SQS messages or Google PubSub items. `OnDeploy` also offers more flexibility since it allows the user to dynamically calculate the queue's initial messages based on the results of other inflight operations.

Closes winglang#281.

BREAKING CHANGE: The field `initialMessages` of `cloud.Queue` has been removed. Instead, use the `cloud.OnDeploy` resource to perform any deploy-time initialization logic.

## 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)*.
The changes in this PR make it possible to call `fromJson` on any compatible struct definition. 

## Implementation notes:
Previously we treated the jsification of structs as a no-op since there is not a JS equivalent. So with this change structs now JSify into a `Struct` file that contains a class with a static `jsonSchema` and a `fromJson` function which will allow for field validation at runtime. 

The schema generated adheres to: https://json-schema.org/understanding-json-schema/

take this simple example Wing code:
```js
struct MyStruct {
	myField: str;
	myOtherField: num;
}
```

this will now generate a JS file named `MyStruct.Struct.js` which looks like this:
```js
module.exports = function(stdStruct, fromInline) {
  class MyStruct {
    static jsonSchema() {
      return {
        id: "/MyStruct",
        type: "object",
        properties: {
          myField: { type: "string" },
          myOtherField: { type: "number" },
        },
        required: [
          "myField",
          "myOtherField",
        ],
        $defs: {
        }
      }
    }
    static fromJson(obj) {
      return stdStruct._validate(obj, this.jsonSchema())
    }
    static _toInflightType(context) {
      return fromInline(`require("./MyStruct.Struct.js")(${ context._lift(stdStruct) })`);
    }
  }
  return MyStruct;
};

```

The piece that brings this all together is the addition of the `Struct` class in our std that only has a `fromJson()` methods at the moment that is a Wing macro. The macro just calls the `fromJson()` method in the generated javascript.

### Misc
We want to stop the user at compile time from calling `fromJson` on a struct that cannot be represented by a Json value ie
```js
struct MyStruct {
	b: cloud.Bucket;
}
let j = {};
MyStruct.fromJson(j);
```

to prevent this I added a check in the typechecker for structs to confirm that if `fromJson` is called that all the fields in the struct are valid for conversion attempt

See image below for error when attempting:
<img width="664" alt="image" src="https://github.com/winglang/wing/assets/45375125/785a2fa6-8823-4fa2-aaa5-4bc8f7ed597f">


Closes: winglang#3653
Closes: winglang#3139

## TODO:
- [x] separate the work done here and the remaining work into different tickets.
- [x] update language reference

## Followup issues that are out of scope for this PR:
- winglang#3792
- winglang#3790
- winglang#3789
- winglang#3788

## 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)
- [x] 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)*.
Fixes: winglang#3024 winglang#3539 winglang#3342 
The problem with the token was the way cdktf is tokenizing strings, I assume it tried to create a new token every time we call `api.url`, or maybe tokenize a token. Now they're all the same- no matter if you call it on preflight or inflight it or assign it to another variable- it'll generate exactly the same token number.

other than that:
- brought back the aws api SDK tests
- changed sim response to be an empty string (in express it's an empty object by default)

## 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)
- [ ] 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)*.
This is a tiny fix removing dead capturing code. For some reason there's code that tries to capture the parent class of a inflight class in the irrelevant case that we're inside an inflight context. In this case the code is dead because an inflight class within an inflight class generates inlined code.

The PR also adds some coloring improvements to the env printing I used to debug this and fixed a comment related to parent class capturing.

## 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
- [ ] Tests added (always)
- [ ] 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)*.
From the few runs I looked into, it seems like the reason for failure is either a network error installing wing (is it blocked by NPM in any way?) or occasional fails in the tests themselves...
I added a retry option to make them more stable:
https://github.com/winglang/wing/actions/runs/5818534372 

## 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
- [ ] Tests added (always)
- [ ] 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)*.
## Checklist

- [ ] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [ ] Description explains motivation and solution
- [ ] Tests added (always)
- [ ] 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)*.
Fixing the source_hash in the sdk snapshots to be a string and not a path

## 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
- [ ] Tests added (always)
- [ ] 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)*.
Fixes winglang#3492 
* Happened because of un-handled SIGINT termination
* Prevented the simulator from being started twice (perhaps more work is needed- for stopping compilation from happening twice)
* Changed the error to a warning while cleaning resources- to be able to clean later resources if one fails

Here is the result:
https://www.loom.com/share/56bddc36c7724d3589a23aebf1141f7a

## 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
- [ ] Tests added (always)
- [ ] 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)*.
tsuf239 and others added 6 commits October 1, 2023 12:21
…4304)

Noticed that today (took less time to fix than to open an issue)
Tested on awscdk:
<img width="763" alt="image" src="https://github.com/winglang/wing/assets/39455181/75389558-2860-4e39-8f99-d436b7fe47c0">
and tf-aws:
<img width="709" alt="image" src="https://github.com/winglang/wing/assets/39455181/0c301b4c-0232-499c-896f-b36ebadab496">

Haven't tested on azure (will be done after that task: winglang#3914 )

Please allow merge only after winglang#4245 

## Checklist

- [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [ ] Description explains motivation and solution
- [x] Tests added (always)
- [ ] 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)*.
Closes winglang#3327

*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)*.
…ang#4349)

Adding another field to issue templates, to indicate whether this issue is ready to start implementation or whether some further discussion is needed first.

## Checklist

- [ ] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [ ] Description explains motivation and solution
- [ ] Tests added (always)
- [ ] 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)*.
)

## Checklist
tests are passing now
tf-aws:
<img width="682" alt="image" src="https://github.com/winglang/wing/assets/39455181/082a8633-3a98-450a-a3d5-6d437689132e">
awscdk:
<img width="665" alt="image" src="https://github.com/winglang/wing/assets/39455181/f85314db-b7db-4150-96ba-581dfb66b364">

(also fixes: winglang#1526)
- [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)
- [ ] 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)*.
fixes [winglang#4139](winglang#4139)
https://www.loom.com/share/5b33e2635ed04be2a8502d7fc5f1116f
## 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
- [ ] Tests added (always)
- [ ] 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)*.
*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)*.
@github-actions
Copy link

github-actions bot commented Oct 2, 2023

Hi,

This PR has not seen activity in 20 days. Therefore, we are marking the PR as stale for now. It will be closed after 7 days.
If you need help with the PR, do not hesitate to reach out in the winglang community slack at winglang.slack.com.
Feel free to re-open this PR when it is still relevant and ready to be worked on again.
Thanks!

@github-actions github-actions bot added the Stale label Oct 2, 2023
giovanni-orciuolo and others added 3 commits October 2, 2023 09:19
…nglang#4329)

Closes winglang#3332

*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)*.
This is the first time I am writing tests, so admittedly my tests are not that versatile. But as I also believe that std tests should be straightforward, I do not totally mind it. Since `keys()` and `values()` methods of maps seems to return an array that fails to assert "is the same as another array of same elements" (which seems to be a [bug](winglang#4118)), I had to iterate over the array values and confirm it works as expected.

closes: winglang#4363

## Checklist

- [ ] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [ ] Description explains motivation and solution
- [ ] Tests added (always)
- [ ] 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)*.
@staycoolcall911
Copy link
Contributor

Hey @giovanni-orciuolo, thanks for fixing that test. Looks like your branch has some merge conflicts that prevent it from merging to main. Could you please resolve these conflicts so we can merge this PR in?
Thanks! We're here if you have any questions

Lancear and others added 2 commits October 2, 2023 13:56
- Inform the reader to install within wsl on windows
- Add some guides for wsl specific issues
- Remove an unused link
- Update wording for the codespace badge

## 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)
- [x] 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)*.
@giovanni-orciuolo
Copy link
Author

I was quite behind with commits so I think I did a mess with the merge operation, sorry! I have a bit of free time from work so that's why I can work on this PR, sorry for the prolonged absence.

@staycoolcall911 staycoolcall911 added the 🚧 pr/do-not-merge PRs with this label will not be automatically merged by mergify. label Oct 2, 2023
@staycoolcall911
Copy link
Contributor

Thanks @giovanni-orciuolo! Yeah - I added the pr/do-not-merge label to make sure we don't accidentally merge all these changes. If it's easier for you you may open a new PR from main with your changes and someone from our team will approve quickly. Thanks for staying on this 🙏

@staycoolcall911 staycoolcall911 dismissed hasanaburayyan’s stale review October 2, 2023 15:35

I wanna give it a fresh review before we merge to main, please

@github-actions github-actions bot removed the Stale label Oct 3, 2023
@monadabot monadabot added the ⚠️ pr/review-mutation PR has been mutated and will not auto-merge. Clear this label if the changes look good! label Oct 12, 2023
mergify bot pushed a commit that referenced this pull request Oct 12, 2023
Includes the changes from: #3771 that giovanni did.

Closes: #3575

## 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)
- [x] 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)*.

Co-authored-by: Giovanni Orciuolo <[email protected]>
BREAKING CHANGE: `Map.get()` now throws if key does not exist.
@staycoolcall911
Copy link
Contributor

Closing this PR now that the changes were migrated to #4523

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🚧 pr/do-not-merge PRs with this label will not be automatically merged by mergify. ⚠️ pr/review-mutation PR has been mutated and will not auto-merge. Clear this label if the changes look good!
Projects
None yet
Development

Successfully merging this pull request may close these issues.