Skip to content

Enable type conversion when passing properties#297

Merged
devblackops merged 1 commit intomasterfrom
enable-type-conversion-when-passing-properties
Apr 13, 2020
Merged

Enable type conversion when passing properties#297
devblackops merged 1 commit intomasterfrom
enable-type-conversion-when-passing-properties

Conversation

@whut
Copy link
Copy Markdown
Member

@whut whut commented Apr 7, 2020

Set-Item does not perform type conversion, but Set-Variable does.

For example with such properties:

properties {
  [uri]$applicationUrl = $null
}

Without this change passing the URL like this:

psake -properties @{ applicationUrl = "http://app.example.com" }

Will make the $applicationUrl variable to be of type [string] instead of [uri]. This is unexpected, as normally variable defined with type accelerator will automatically convert other types to the specified type, for example

[uri]$myUrl = "http://my.example.com"
$myUrl = "http://other.example.com" # this still is [uri], not a [string]

Set-Variable preserve behavior of variables defined with type accelerators, while Set-Item sadly does not:).

BTW. there is a workaround: you can pass the type info again when passing parameter in psake, but this is error prone, as this is not needed normally in PowerShell, only in psake: psake -properties @{ applicationUrl = [uri]"http://app.example.com" }

Description

Related Issue

Motivation and Context

How Has This Been Tested?

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Set-Item does not perform type conversion, but Set-Variable does.

For example with such properties:

```powershell
properties {
  [uri]$applicationUrl = $null
}
```

Without this change passing the URL like this:

```shell
psake -properties @{ applicationUrl = "http://app.example.com" }
```

Will make the $applicationUrl variable to be of type [string] instead of [uri]. This is unexpected, as normally variable defined with type accelerator will automatically convert other types to the specified type, for example

```powershell
[uri]$myUrl = "http://my.example.com"
$myUrl = "http://other.example.com" # this still is [uri], not a [string]
```

Set-Variable preserve behavior of variables defined with type accelerators, while Set-Item sadly does not:).

BTW. there is a workaround: you can pass the type info again when passing parameter in psake, but this is error prone, as this is not needed normally in PowerShell, only in psake: `psake -properties @{ applicationUrl = [uri]"http://app.example.com" }`
@devblackops devblackops self-assigned this Apr 13, 2020
@devblackops devblackops self-requested a review April 13, 2020 05:19
@devblackops
Copy link
Copy Markdown
Member

This is a good fix @whut. Thanks!

@devblackops devblackops merged commit 538bdcf into master Apr 13, 2020
@HeyItsGilbert HeyItsGilbert deleted the enable-type-conversion-when-passing-properties branch September 21, 2024 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants