Skip to content

Conversation

@iSazonov
Copy link
Collaborator

  1. Implicitly convert XML rval to string
  2. Add test

Close #2459

Copy link
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
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
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
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 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
Copy link
Collaborator Author

iSazonov commented Nov 28, 2016

@lzybkr could you continue the code review?

Copy link
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
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
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

Copy link
Collaborator Author

@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.

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

Copy link
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
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
Member

mirichmo commented Dec 2, 2016

@lzybkr Do you have any other comments or concerns regarding this PR?

@lzybkr lzybkr merged commit 8abb6c3 into PowerShell:master Dec 2, 2016
@iSazonov
Copy link
Collaborator Author

iSazonov commented Dec 3, 2016

@lzybkr Thanks for code review and great comments!

@iSazonov iSazonov deleted the xmltostring branch January 9, 2017 12:22
@iSazonov iSazonov removed the Review - Needed The PR is being reviewed label Mar 30, 2017
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