-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Remove [ValidateNotNullOrEmpty] from -InputObject on Get-Random to allow empty string #10644
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ public class GetRandomCommand : PSCmdlet | |
|
|
||
| private const string RandomNumberParameterSet = "RandomNumberParameterSet"; | ||
| private const string RandomListItemParameterSet = "RandomListItemParameterSet"; | ||
| private static readonly object[] _nullInArray = new object[] { null }; | ||
|
|
||
| private enum MyParameterSet | ||
| { | ||
|
|
@@ -275,7 +276,7 @@ private double ConvertToDouble(object o, double defaultIfNull) | |
| /// List from which random elements are chosen. | ||
| /// </summary> | ||
| [Parameter(ParameterSetName = RandomListItemParameterSet, ValueFromPipeline = true, Position = 0, Mandatory = true)] | ||
| [ValidateNotNullOrEmpty] | ||
|
SteveL-MSFT marked this conversation as resolved.
|
||
| [System.Management.Automation.AllowNull] | ||
|
iSazonov marked this conversation as resolved.
|
||
| [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] | ||
| public object[] InputObject { get; set; } | ||
|
|
||
|
|
@@ -491,19 +492,24 @@ protected override void ProcessRecord() | |
| { | ||
| if (EffectiveParameterSet == MyParameterSet.RandomListItem) | ||
| { | ||
| foreach (object item in InputObject) | ||
| // this allows for $null to be in an array passed to InputObject | ||
| foreach (object item in InputObject ?? _nullInArray) | ||
|
SteveL-MSFT marked this conversation as resolved.
|
||
| { | ||
| if (_numberOfProcessedListItems < Count) // (3) | ||
| // (3) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the comment for?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Codefactor was complaining about the trailing comment so I moved it above. If you look at the large comment above, it references (1), (2), (3) explaining the code so I left it. |
||
| if (_numberOfProcessedListItems < Count) | ||
| { | ||
| Debug.Assert(_chosenListItems.Count == _numberOfProcessedListItems, "Initial K elements should all be included in chosenListItems"); | ||
| _chosenListItems.Add(item); | ||
| } | ||
| else | ||
| { | ||
| Debug.Assert(_chosenListItems.Count == Count, "After processing K initial elements, the length of chosenItems should stay equal to K"); | ||
| if (Generator.Next(_numberOfProcessedListItems + 1) < Count) // (1) | ||
|
|
||
| // (1) | ||
| if (Generator.Next(_numberOfProcessedListItems + 1) < Count) | ||
| { | ||
| int indexToReplace = Generator.Next(_chosenListItems.Count); // (2) | ||
| // (2) | ||
| int indexToReplace = Generator.Next(_chosenListItems.Count); | ||
| _chosenListItems[indexToReplace] = item; | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.