Add TryAdd/Remove extension methods in CollectionExtensions#18091
Conversation
|
@nbarbettini, It will cover your contributions to all .NET Foundation-managed open source projects. |
|
|
||
| namespace System.Collections.Generic | ||
| { | ||
| internal static class DictionaryExtensions |
There was a problem hiding this comment.
I think this is superseded by adding TryAdd to CollectionExtensions?
There was a problem hiding this comment.
It's actually still needed. Dictionary.TryAdd only exists in .NET Core, not in desktop (.NET Framework), so this extension method is used by the tests when compiled for desktop to provide a polyfill.
There was a problem hiding this comment.
Actually we would need to add the Remove API that is being added to CollectionExtensions in order for all the tests to pass in Desktop right, @stephentoub ?
There was a problem hiding this comment.
Yup. Or alternatively include the product CollectionExtensions file into the test project when building for desktop, if that's feasible.
There was a problem hiding this comment.
The reason I removed this is because was an ambiguous call with the new Collection.ExtensionsTryAdd. I'll investigate resolving the problem re: desktop.
There was a problem hiding this comment.
Maybe you could wrap this type around an #if !netcoreapp
| TypesMustExist : Type 'System.Data.SqlClient.SqlColumnEncryptionCngProvider' does not exist in the implementation but it does exist in the contract. | ||
| TypesMustExist : Type 'System.Data.SqlClient.SqlColumnEncryptionCspProvider' does not exist in the implementation but it does exist in the contract. | ||
| TypesMustExist : Type 'System.Data.SqlClient.SqlColumnEncryptionKeyStoreProvider' does not exist in the implementation but it does exist in the contract. | ||
| CannotRemoveBaseTypeOrInterface : Type 'System.Data.SqlClient.SqlCommand' does not implement interface 'System.ICloneable' in the implementation but it does in the contract. |
There was a problem hiding this comment.
Not really sure why these showed up - I didn't change anything in System.Data.SqlClient. Should these be ignored?
There was a problem hiding this comment.
This is created/updated automatically when running build.cmd and getting a successful build. This is a diff in APIs we have in between netcoreapp and desktop. So don't worry about this. You can either remove this change from your PR or leave it, sooner or later it will get updated.
|
FYI, I can squash some of these commits if you'd like. I had a little bit of churn while I was figuring out how everything worked. |
|
@nbarbettini, thanks for signing the contribution license agreement. We will now validate the agreement and then the pull request. |
|
Thanks for doing this! Netcoreapp:From your repo root run the following commands:
Netfx:From your repo root run the following commands:
Uap:From your repo root run the following commands:
UapAot:From your repo root run the following commands:
[EDIT]: @stephentoub is right we need that file for the tests in Desktop to pass. |
| } | ||
|
|
||
| return false; | ||
| } |
There was a problem hiding this comment.
This isn't ever removing the value from the dictionary.
There was a problem hiding this comment.
(which also highlights that the new tests are missing verification that the value is actually removed)
There was a problem hiding this comment.
Excellent point... d'oh. Fixed. 👍
|
To run the tests in Desktop and verify that the new tests that you added are passing you can run from your repo's root:
|
| <Link>Common\System\ObjectCloner.cs</Link> | ||
| </Compile> | ||
| <Compile Include="$(CommonTestPath)\System\Collections\DictionaryExtensions.cs"> | ||
| <Link>Common\System\Collections\DictionaryExtensions.cs</Link> |
There was a problem hiding this comment.
There is around 7 other projects that use DictionaryExtensions.cs file.
That is why CI is failing. You could verify this by running:
build.cmd
build-tests.cmd -skiptests
Can we leave this file as it was and add the new API there as well? Since there is a lot of test projects that use this file.
| public void TryAdd_KeyDoesntExistInIDictionary_ReturnsTrue() | ||
| { | ||
| IDictionary<string, string> dictionary = new SortedDictionary<string, string>(); | ||
| Assert.Equal(true, dictionary.TryAdd("key", "value")); |
There was a problem hiding this comment.
Nit: Assert.True or Assert.False instead of direct equality checks for booleans
| throw new ArgumentNullException(nameof(dictionary)); | ||
| } | ||
|
|
||
| value = default(TValue); |
There was a problem hiding this comment.
This should be moved down to where false is returned.
|
|
||
| value = default(TValue); | ||
|
|
||
| if (dictionary.TryGetValue(key, out var foundValue)) |
There was a problem hiding this comment.
Why use a temporary? You can just use out value.
|
Thanks for the great feedback (and patience) y'all. Had to step out for a bit and didn't get back to it until now. I reverted my deletion of Thanks for sticking with me 😄 |
|
Thanks for the update... please let us know if you are not able to figure it out so that we can guide you. |
|
@dotnet-bot test this please (logs gone) |
|
Sorry for the delay, it's been a busy week! Thanks for the hints @safern and @stephentoub. I was able to get all the tests to pass by selectively excluding the Let me know how it's looking, and if there's anything else I need to do. I can rebase the PR, and/or squash some of the churn in the history if you want. |
No worries! Actually we rather do it the way you did it. I gave you another option in case you got blocked, but the way you did it is excellent. Thanks for the update. The other thing you need to do now is fix your merge conflicts by fetching the latest changes in dotnet/master and rebasing your changes on top of it. Then update your PR. See: #18109 (comment) as a guidance. To update the PR after doing that you will have to do a force push. |
d6e7a33 to
e5f005b
Compare
|
@safern Rebased and resolved conflicts. 👍 |
| public void Remove_NullKeyIDictionary_ThrowsArgumentNullException() | ||
| { | ||
| IDictionary<string, string> dictionary = new SortedDictionary<string, string>(); | ||
| Assert.Throws<ArgumentNullException>("key", () => dictionary.Remove(null, out var value)); |
| public void Remove_NullIDictionary_ThrowsArgumentNullException() | ||
| { | ||
| IDictionary<string, string> dictionary = null; | ||
| Assert.Throws<ArgumentNullException>("dictionary", () => dictionary.Remove("key", out var value)); |
There was a problem hiding this comment.
We could assign a value to the out parameter before calling Remove and test that the value did not changed when an exception is thrown.
safern
left a comment
There was a problem hiding this comment.
Thanks @nbarbettini for contributing. Feel free to grab any issue with milestone 2.0.0. When grabbing the issue just ping on it so that we can assign it to you and we don't have two people working on the same issue.
|
Thanks @safern et al! 🎉 I'm really glad to be able to contribute. |
…8091) * Add TryAdd and Remove to CollectionExtensions
…8091) * Add TryAdd and Remove to CollectionExtensions
Adding
TryAddandRemovetoCollectionExtensions.Hopefully I did everything correctly. If not, I'm happy to revise the PR 😄
cc @safern @karelz @danmosemsft
Closes #17918