8000 Implicitly convert XML rval to string by iSazonov · Pull Request #2678 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Implicitly convert XML rval to string #2678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 2, 2016
Merged

Conversation

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

Close #2459

@@ -4911,7 +4911,8 @@ protected override object PropertyGet(PSProperty property)
/// <param name="convertIfPossible">instructs the adapter to convert before setting, if the adapter supports conversion</param>
protected override void PropertySet(PSProperty property, object setValue, bool convertIfPossible)
{
string valueString = setValue as string;
// XML is always a string so implicitly convert to string
string valueString = setValue?.ToString();
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.

# rval will be implicitly converted to `string` type
{ $x.root.data = $rval } | Should Not Throw
$x.root.data | Should Be $rval.ToString()
}
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?

@@ -4911,7 +4911,8 @@ protected override object PropertyGet(PSProperty property)
/// <param name="convertIfPossible">instructs the adapter to convert before setting, if the adapter supports conversion</param>
protected override void PropertySet(PSProperty property, object setValue, bool convertIfPossible)
{
string valueString = setValue as string;
// XML is always a string so implicitly convert to string
string valueString = LanguagePrimitives.ConvertTo<string>(setValue);
if (valueString == null)
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.

@{ rval = @{testprop = 1}; value = 'hash (psobject)' },
@{ rval = 1; value = 'int (codemethod)' },
@{ rval = "teststring"; value = 'string (codemethod)' }
}
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)

param($rval)
{
{ $x.root.data = $rval } | Should Not Throw
$x.root.data | Should Be $rval.ToString()
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
0