Skip to content

Fixing HashMap to PSCustomObject object conversion (#2295) - #2533

Merged
Jason Shirk (lzybkr) merged 2 commits into
PowerShell:masterfrom
charub:hashmap-to-pscustomobject-conversion-fix
Dec 21, 2016
Merged

Jason Shirk (lzybkr) merged 2 commits into
PowerShell:masterfrom
charub:hashmap-to-pscustomobject-conversion-fix

Conversation

@charub

@charub Charu Bassi (charub) commented Oct 25, 2016 •

Copy link
Copy Markdown
Contributor

Fixing following issues :

  1. HashMap to PSCustomObject Conversion fails if full name of PSCustomObject type is specified.
  2. [pscu] expands to [System.Management.Automation.PSCustomObject] whereas [pso] expands to [PSObject].
  3. [PSCustomObject].FullName returned System.Management.Automation.PSObject instead of System.Management.Automation.PSCustomObject.

After the fix, these 3 should not happen.

@msftclas

Hi Charu Bassi (@charub), I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!


It looks like you're a Microsoft contributor (Charu Bassi). If you're full-time, we DON'T require a Contribution License Agreement. If you are a vendor, please DO sign the electronic Contribution License Agreement. It will take 2 minutes and there's no faxing! https://cla.microsoft.com.

TTYL, MSBOT;

@vors

Copy link
Copy Markdown
Collaborator

Charu Bassi (@charub) it fixes #2295 right?

@vors sergei (vors) self-assigned this Oct 26, 2016

@vors sergei (vors) left a comment

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 also love to see more tests:

  • ctor call with new() on type accelerator, on full type name
  • ctor call with New-Object with full name, short name
  • hashtable cast to full name

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.

Not sure about that: System.Management.Automation.PSCustomObject is a different type from System.Management.Automation.PSObject which we construct. Did you have a chance to figure out what's the difference between them?

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.

Don't need this change anymore. Only fixing the tab completion for 'pscu'

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.

We should avoid duplicated code, consider create a helper function.

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.

Don't need this change anymore. Only fixing the tab completion for 'pscu'

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 could be wrong, but it seems inconsistent with the current proposed conversion: the acelerator is for type PSCustomObject, but binder for hashtable does the conversion to PSObject. One option would be to completely eliminate PSCustomObject and set type accelerator to PSObject, if that doesn't break anything.

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 is a breaking change. I realize it feels wrong, but it's been this way since V1, we can't really change it.

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.

Don't need this change anymore.

@lzybkr Jason Shirk (lzybkr) left a comment

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.

I do question why anybody would want to use [System.Management.Automation.PSCustomObject]@{}.

If it only comes up because of tab completion, maybe we should special case tab completion of [pscustomobject].

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 is a breaking change. I realize it feels wrong, but it's been this way since V1, we can't really change it.

@charub
Charu Bassi (charub) force-pushed the hashmap-to-pscustomobject-conversion-fix branch from 65527df to 677bc79 Compare November 4, 2016 21:01
@charub

Copy link
Copy Markdown
Contributor Author

It seems the problem is the tab completion for pscu. 'PSCU' expands to [System.Management.Automation.PSCustomObject] when it should actually expand to [pscustomobject] which is an accelerator for System.Management.Automation.PSObject.
I agree with Jason that we should fix the tab completion.

@vors

Copy link
Copy Markdown
Collaborator

Ok, that sounds reasonable: if we just fix IntelliSense then we will probably avoid most of the problems.
Current fix seems too much of a special case for me. It's hard to track down that this code affects IntelliSense and nothing else.
I also noticed that completion for [pscu in Emacs PSReadLine mode (default on Unix) is completely screwed.
It turn into [PS instead of anything meanfull.
It could be a PSReadLine issue (not the fitst time I see problems with Emacs mode).

@lzybkr

Copy link
Copy Markdown
Contributor

This change seems fine (and necessary) to me, but perhaps it's not sufficient to fix the ambiguity that comes up in PSReadline.

We definitely want some tests.

It does fix more than Intellisense, e.g. if a method used PSCustomObject in it's signature, this change will affect the output of Get-Member.

@vors

Copy link
Copy Markdown
Collaborator

Charu Bassi (@charub) please add some tests and we will merge it.

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 would argue about correctness of this test.
It's a current behavior, but it's not the right one.
Maybe just drop the check for count and check for [1] == psobject and leave a comment explaining what's going on?

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.

As per the current behavior, pscu expands to System.Management.Automation.PSCustomObject. After this change, it will now expand to pscustomobject and psobject. both of these accelerators refer to System.Management.Automation.PSObject. I am afraid I don't understand your concern here.

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.

Is expanding to both psobject and pscustomobject a correct behavior for [pscu?
I don't think so. I think it should be pscustomobject only, because psobject doesn't start with pscu.

Why then make a test that verifies a wrong behavior?

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.

I think it's the correct behavior considering both are accelerators for [System.Management.Automation.PSObject].

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 cannot agree with that.

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.

empty hashtable or $null

This test is only for $null
Can you one more testcase for empty hashtable?

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.

Added another test for an empty hashtable

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.

fail to create object

Any object? -Property $null seems like a special case.

Again the same question: is it the right test or it's just the current behavior.

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.

you are right. Corrected the test.

@vors

Copy link
Copy Markdown
Collaborator

I'm currently waiting on the $result.CompletionMatches[1].CompletionText | Should Be "psobject" thread. Lets find solution and then we can merge the PR.

@PowerShellTeam PowerShellTeam added Review - Needed The PR is being reviewed and removed Review - Waiting on Author labels Dec 5, 2016
@charub
Charu Bassi (charub) force-pushed the hashmap-to-pscustomobject-conversion-fix branch from 01cf2c4 to 124114e Compare December 9, 2016 20:21
@charub

Copy link
Copy Markdown
Contributor Author

sergei (@vors) : You are right, pscu should only expand to pscustomobject and not psobject. Fixed the current behavior for tab completion when a type has more than 1 accelerators.

Resolving PowerShell#2295

Fixing the current behavior of tab completion for types with more than 1
type accelerator.
@charub
Charu Bassi (charub) force-pushed the hashmap-to-pscustomobject-conversion-fix branch from 124114e to ed6a8ae Compare December 9, 2016 23:19
}

internal static string FindBuiltinAccelerator(Type type)
internal static string FindBuiltinAccelerator(Type type,string expectedKey=null)

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.

Style:

  • space after comma,
  • spaces around =

$errorObj = $_
}
$obj | should be $null
$errorObj.FullyQualifiedErrorId | should be "CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand"

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.

Should we add a follow-up item on that to completely remove this type?
cc Jason Shirk (@lzybkr)

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's probably not worth the hassle - it's a public api and hence a breaking change, and there is one valid use of the type (though that use can be expressed differently).

@vors sergei (vors) left a comment

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.

Please, correct the style comments and it will be good for merge

@vors sergei (vors) added Review - Waiting on Author and removed Review - Needed The PR is being reviewed labels Dec 21, 2016
@lzybkr
Jason Shirk (lzybkr) merged commit 821c965 into PowerShell:master Dec 21, 2016
Rob Holt (rjmholt) pushed a commit to rjmholt/PowerShell that referenced this pull request Jan 9, 2017
Type completion would behave strangely when there were multiple aliases for a given type (currently only possible with type accelerators).

The fix is to only select the accelerator that matches what the user typed.

Closes PowerShell#2295
@TravisEz13 Travis Plunk (TravisEz13) added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Review - Waiting on Author labels May 27, 2020
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
Type completion would behave strangely when there were multiple aliases for a given type (currently only possible with type accelerators).

The fix is to only select the accelerator that matches what the user typed.

Closes PowerShell#2295
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants