Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public int First
public SwitchParameter Wait { get; set; }

/// <summary>
/// Used to display the object at specified index.
/// Used to display the object at the specified index.
/// </summary>
/// <value></value>
[Parameter(ParameterSetName = "IndexParameter")]
Expand All @@ -177,11 +177,36 @@ public int[] Index
{
_index = value;
_indexSpecified = true;
_isIncludeIndex = true;
Array.Sort(_index);
}
}

/// <summary>
/// Used to display all objects at the specified indices.
/// </summary>
/// <value></value>
[Parameter(ParameterSetName = "SkipIndexParameter")]
[ValidateRangeAttribute(0, int.MaxValue)]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
public int[] SkipIndex
{
get
{
return _index;
}
set
{
_index = value;
_indexSpecified = true;
_isIncludeIndex = false;
Array.Sort(_index);
}
}

private int[] _index;
private bool _indexSpecified;
private bool _isIncludeIndex;

#endregion

Expand Down Expand Up @@ -641,55 +666,101 @@ protected override void BeginProcessing()
_selectObjectQueue = new SelectObjectQueue(_first, _last, Skip, SkipLast, _firstOrLastSpecified);
}

private int _indexOfCurrentObject = 0;
private int _indexCount = 0;
/// <summary>
/// Handles processing of InputObject.
/// </summary>
protected override void ProcessRecord()
{
if (InputObject != AutomationNull.Value && InputObject != null)
{
if (!_indexSpecified)
if (_indexSpecified)
{
ProcessIndexed();
}
else
{
_selectObjectQueue.Enqueue(InputObject);
PSObject streamingInputObject = _selectObjectQueue.StreamingDequeue();
if (streamingInputObject != null)
{
ProcessObjectAndHandleErrors(streamingInputObject);
}

if (_selectObjectQueue.AllRequestedObjectsProcessed && !this.Wait)
{
this.EndProcessing();
throw new StopUpstreamCommandsException(this);
}
}
else
}
}

/// <summary>
/// The index of the active index filter.
/// </summary>
private int _currentFilterIndex;

/// <summary>
/// The index of the object being processed.
/// </summary>
private int _currentObjectIndex;

/// <summary>
/// Handles processing of InputObject if -Index or -SkipIndex is specified.
/// </summary>
private void ProcessIndexed()
{
if (_isIncludeIndex)
{
if (_currentFilterIndex < _index.Length)
{
if (_indexOfCurrentObject < _index.Length)
int nextIndexToOutput = _index[_currentFilterIndex];
if (_currentObjectIndex == nextIndexToOutput)
{
int currentlyRequestedIndex = _index[_indexOfCurrentObject];
if (_indexCount == currentlyRequestedIndex)
ProcessObjectAndHandleErrors(InputObject);
while ((_currentFilterIndex < _index.Length) && (_index[_currentFilterIndex] == nextIndexToOutput))
{
ProcessObjectAndHandleErrors(InputObject);
while ((_indexOfCurrentObject < _index.Length) && (_index[_indexOfCurrentObject] == currentlyRequestedIndex))
{
_indexOfCurrentObject++;
}
_currentFilterIndex++;
}
}
}

if (!this.Wait && _indexOfCurrentObject >= _index.Length)
if (!Wait && _currentFilterIndex >= _index.Length)
{
EndProcessing();
throw new StopUpstreamCommandsException(this);
}

_currentObjectIndex++;
}
else
{
if (_currentFilterIndex < _index.Length)
{
int nextIndexToSkip = _index[_currentFilterIndex];
if (_currentObjectIndex != nextIndexToSkip)
{
this.EndProcessing();
throw new StopUpstreamCommandsException(this);
ProcessObjectAndHandleErrors(InputObject);
}
else
{
while ((_currentFilterIndex < _index.Length) && (_index[_currentFilterIndex] == nextIndexToSkip))
{
_currentFilterIndex++;
}
}

_indexCount++;
}
else
{
ProcessObjectAndHandleErrors(InputObject);
}

_currentObjectIndex++;
}
}

/// <summary>
/// Completes the processing of Input.
/// </summary>
protected override void EndProcessing()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,18 @@ Describe "Select-Object DRT basic functionality" -Tags "CI" {
$results[0] | Should -BeExactly "3"
}

It "Select-Object with SkipIndex should work" {
$results = "1", "2", "3" | Select-Object -SkipIndex 0, 2
$results | Should -HaveCount 1
$results[0] | Should -BeExactly "2"
}

It "Select-Object with SkipIndex should work with index out of range" {
$results = 0..10 | Select-Object -SkipIndex 5, 6, 7, 8, 11
$results | Should -HaveCount 7
$results -join ',' | Should -BeExactly "0,1,2,3,4,9,10"
}

It "Select-Object should handle dynamic (DLR) properties"{
$dynObj = [TestDynamic]::new()
$results = $dynObj, $dynObj | Select-Object -ExpandProperty FooProp
Expand Down