Skip to content

Implicitly convert XML rval to string - #2678

Merged
Jason Shirk (lzybkr) merged 4 commits into
PowerShell:masterfrom
iSazonov:xmltostring
Dec 2, 2016
Merged

Implicitly convert XML rval to string#2678
Jason Shirk (lzybkr) merged 4 commits into
PowerShell:masterfrom
iSazonov:xmltostring

Conversation

@iSazonov

Copy link
Copy Markdown
Collaborator
  1. Implicitly convert XML rval to string
  2. Add test

Close #2459

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.

Calling ToString directly avoids any magic PowerShell might apply when converting a value to string.

You could just call LanguagePrimitives.ConvertTo<string>(setValue) - this will properly call into a CodeMethod or ScriptMethod named ToString if the object has one.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done.

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 should also test that we properly invoke ToString implemented as a CodeMethod and ScriptMethod. System.Type has a CodeMethod implementation of ToString, you can see the difference:

#57 PS> [System.Int32].ToString()
System.Int32
#58 PS> [string][System.Int32]
int

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done.
Only I could not create ToString ScriptMethod because ToString already exists.

@iSazonov
Ilya (iSazonov) force-pushed the xmltostring branch 2 times, most recently from d6f72d2 to 3ffe8fb Compare November 15, 2016 09:43
@PowerShellTeam PowerShellTeam added the Review - Needed The PR is being reviewed label Nov 22, 2016
@iSazonov

Ilya (iSazonov) commented Nov 28, 2016

Copy link
Copy Markdown
Collaborator Author

Jason Shirk (@lzybkr) could you continue the code review?

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 think this condition is unreachable now.

We can convert everything to string (including $null which turns into the empty string).

The only failure condition now is if the conversion to string fails with an exception, e.g. if ToString() throws an exception. This should be rare, but if it happens, I think we'd want to see that exception and not the SetValueException that is being thrown here.

In other words, I think this test and subsequent throw can be safely deleted.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

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 would add tests for:

  • $null
  • array of strings
  • array of integers
  • psobject wrapping each of the previous values

@iSazonov Ilya (iSazonov) Nov 28, 2016

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.
But I have doubts about [PSObject]::AsPSObject($null) and [PSObject]::AsPSObject(1)

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 always call ToString.

PS> $x = 1,2
PS> [System.Management.Automation.LanguagePrimitives]::ConvertTo($x, [string])
1 2
PS> $x.ToString()
System.Object[]

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

@mirichmo

Copy link
Copy Markdown
Member

Jason Shirk (@lzybkr) Do you have any other comments or concerns regarding this PR?

@lzybkr
Jason Shirk (lzybkr) merged commit 8abb6c3 into PowerShell:master Dec 2, 2016
@iSazonov

Copy link
Copy Markdown
Collaborator Author

Jason Shirk (@lzybkr) Thanks for code review and great comments!

@iSazonov
Ilya (iSazonov) deleted the xmltostring branch January 9, 2017 12:22
@iSazonov Ilya (iSazonov) removed the Review - Needed The PR is being reviewed label Mar 30, 2017
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj/PowerShell that referenced this pull request Aug 6, 2026
…ll#2678)

Using LanguagePrimitives.ConvertTo (to emulate what would happen if the property were a regular .Net property typed as string), we now convert the value assigned to an XML property to string.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants