Skip to content

Add parameter -Group to Get-Verb#2789

Merged
lzybkr merged 13 commits intoPowerShell:masterfrom
dchristian3188:Get-Verb
Jan 3, 2017
Merged

Add parameter -Group to Get-Verb#2789
lzybkr merged 13 commits intoPowerShell:masterfrom
dchristian3188:Get-Verb

Conversation

@dchristian3188
Copy link
Copy Markdown
Contributor

Adding group filter to get-verb. Please reference:
#2527

@msftclas
Copy link
Copy Markdown

Hi @dchristian3188, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!
You've already signed the contribution license agreement. Thanks!

The agreement was validated by Microsoft and real humans are currently evaluating your PR.

TTYL, MSBOT;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'd rather see enum here instead string.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@iSazonov What do you think would be the best way to accomplish this? Currently the groups are determined by looking at the class names and removing the word verb.

[System.Reflection.IntrospectionExtensions]::GetTypeInfo([PSObject]).Assembly.ExportedTypes | Where-Object {$_.Name -match '^Verbs.'}

I could put the current groups in a validate set but is that the best for long term support?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

DynamicParam maybe?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ValidateSet is reasonable, especially if there is an easy way to test.

I would like to see this command converted to C#.

Copy link
Copy Markdown
Collaborator

@iSazonov iSazonov Nov 28, 2016

Choose a reason for hiding this comment

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

You can see a enum using sample in Get/Set-Alias
The enum could be placed Verbs.cs
(About 30 parameters using this.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

thanks for the feedback. I'll get started on the changes

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@lzybkr Could you comment on the use of enum vs validate set ?
I attached the file with script and the resulting list of parameters whose type is enum (for Powershel 6 only).
Test-Params.txt

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In this case, an enum introduces a new public api.
If the only use of that enum is for parameter validation and tab completion, then ValidateSet is might be better than introducing a new api.
If we did use an enum, the parameter doesn't need to be an array, the enum could use Flags.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@lzybkr Thanks for great comment! I was asking about using VerbGroups in Issue addressed in the PR but there were no examples. So I don't see the need for the public api at all.

@PowerShellTeam PowerShellTeam added the Review - Needed The PR is being reviewed label Nov 28, 2016
@lzybkr lzybkr self-assigned this Nov 29, 2016
@lzybkr lzybkr changed the title Added Group Param to Get-Verb Add parameter -Group to Get-Verb Nov 29, 2016
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Revert this change.

Copy link
Copy Markdown
Contributor

@lzybkr lzybkr left a comment

Choose a reason for hiding this comment

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

Great tests!
I realize you mostly just rewrote the original function in C#, but I think the original implementation could have been better.

I realize the performance of this cmdlet really doesn't matter, but we have plenty of bad patterns that people just copy and I want to avoid introducing those patterns in addition to fixing the bad patterns over time.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The types all start with Verbs, so you can use type.Name.Substring(5) which avoids the search that Replace would do.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It doesn't really make sense to have BeginProcessing. This method generates a bunch of results that might be thrown away. It's better to avoid creating those results in the first place.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

After further reflection, I'm open to the proposal by @iSazonov to use an enum. It makes a lot of sense to use an enum in the objects returned by this cmdlet.

At this point, I'm just commenting (sort of thinking out loud) - I'm not requiring a change, but feel free to try it and see if it seems better.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The field name should be identical to the value. If we added an assertion to that effect, then we could use field.Name instead.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This list should not be necessary.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This list should not be necessary - it should be possible to generate all results without storing any in a list.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You will find SessionStateUtilities.MatchesAnyWildcardPattern useful in avoiding creating a list.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

More simply:

$_.FullyQualifiedErrorId | Should Be ...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be ... | Should BeNullOrEmpty

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We don't use -test anywhere else, so it just looks weird here and should probably be removed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If the parameter was an enum with flags, you wouldn't need any special code to cover this case, we wouldn't see the group specified multiple times.

@dchristian3188
Copy link
Copy Markdown
Contributor Author

Got it, thanks for the review Izbkr. I'll get started on the changes.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

(Get-Verb -Verb Add | Measure-Obj [](start = 8, length = 33)

this should be expressed as:
@(Get-Verb -Verb Add).Count | should be 1
no need to use measure-object (and the same for all of these uses of Measure-Object)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

thanks! I was trying to use (Get-Verb -Verb Add).Count but was getting null results. Didn't think of adding the "@".

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

if you're not sure whether you'll get a singleton or a collection you can always add @()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

-Verb -Group Common | Should not be $null [](start = 11, length = 41)

this doesn't actually validate that you got the proper group
i think you're looking for this:
(Get-Verb -Group Common).Group | Sort-Object -uniq | Should be Common

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Get-Verb -Group FakeGroupNeverExists -ErrorAction Stop [](start = 12, length = 54)

in case this doesn't throw, you need to add:
throw "Expected error did not occur"
or similar

Copy link
Copy Markdown
Contributor

@lzybkr lzybkr left a comment

Choose a reason for hiding this comment

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

Looking pretty good now, just a couple more perf issues to fix.

As long as you're making these changes, you might as well use our consistent formatting, a space after if and spaces after arguments, instead of foo(arg1,arg2), use foo(arg1, arg2).

If you don't make those changes, we'll fix it later with a tool, but it's nicer to be in the habit of following the conventions of the project.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This creates a ton of garbage - move it outside the loops.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This can be done once per type, not once per field in the type, correct?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

that makes alot of sense, I'll get this updated.

Copy link
Copy Markdown
Contributor

@lzybkr lzybkr left a comment

Choose a reason for hiding this comment

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

Looks good - please fix the merge conflict so we can merge the PR.

@kittholland
Copy link
Copy Markdown
Contributor

Since you are using the validateset with hardcoded group names, can you add a test enumerating the possible groups and ensuring all possibilities are handled in the validateset?

Author="Microsoft Corporation"
CompanyName="Microsoft Corporation"
Copyright=" Microsoft Corporation. All rights reserved."
Copyright="© Microsoft Corporation. All rights reserved."
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This change should be reverted - it changes the file to UTF8 encoding without a BOM.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Instead of reverting the change, you added a BOM.

That's a reasonable change to make (we have another PR to clean these up), but to keep this PR clean, I'd prefer you undo the change and not change the encoding.

@dchristian3188
Copy link
Copy Markdown
Contributor Author

Added an additional test to cover extra groups. Please let me know if any other changes are needed.

@lzybkr lzybkr merged commit 062e15d into PowerShell:master Jan 3, 2017
rjmholt pushed a commit to rjmholt/PowerShell that referenced this pull request Jan 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Review - Needed The PR is being reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants