Skip to content

Commit e90b0f4

Browse files
Gijsreynadityapatwardhan
authored andcommitted
Fix Copilot remark
1 parent 740a9bc commit e90b0f4

1 file changed

Lines changed: 30 additions & 13 deletions

File tree

‎src/dsc/psresourceget.ps1‎

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -161,27 +161,37 @@ class PSResourceList {
161161
}
162162

163163
[string] ToJson() {
164-
$resourceJsonItems = if ($this.resources) {
165-
@($this.resources | ForEach-Object { $_.ToJson() })
164+
$resourceObjects = if ($this.resources) {
165+
@($this.resources | ForEach-Object {
166+
[string[]]$excludeProps = @('_inDesiredState')
167+
if ($null -eq $_._metadata) { $excludeProps += '_metadata' }
168+
$_ | Select-Object -ExcludeProperty $excludeProps
169+
})
166170
} else { @() }
167-
$resourcesArrayJson = '[' + ($resourceJsonItems -join ',') + ']'
168-
$repoNameJson = $this.repositoryName | ConvertTo-Json -Compress
169-
$retVal = "{`"repositoryName`":$repoNameJson,`"resources`":$resourcesArrayJson}"
171+
$retVal = [ordered]@{
172+
repositoryName = $this.repositoryName
173+
resources = [object[]]$resourceObjects
174+
} | ConvertTo-Json -Compress -Depth 5 -EnumsAsStrings
170175
Write-Trace -message "Serializing PSResourceList to JSON. RepositoryName: $($this.repositoryName), TrustedRepository: $($this.trustedRepository), Resources count: $($this.resources.Count)" -level debug
171176
Write-Trace -message "Serialized JSON: $retVal" -level trace
172177
return $retVal
173178
}
174179

175180
[string] ToJsonForTest() {
176181
Write-Trace -message "Serializing PSResourceList to JSON for test output. RepositoryName: $($this.repositoryName), TrustedRepository: $($this.trustedRepository), Resources count: $($this.resources.Count)" -level debug
177-
$resourceJsonItems = if ($this.resources) {
178-
@($this.resources | ForEach-Object { $_.ToJsonForTest() })
182+
$resourceObjects = if ($this.resources) {
183+
@($this.resources | ForEach-Object {
184+
[string[]]$excludeProps = @()
185+
if ($null -eq $_._metadata) { $excludeProps += '_metadata' }
186+
if ($excludeProps.Count -gt 0) { $_ | Select-Object -ExcludeProperty $excludeProps } else { $_ }
187+
})
179188
} else { @() }
180-
$resourcesArrayJson = '[' + ($resourceJsonItems -join ',') + ']'
181-
$repoNameJson = $this.repositoryName | ConvertTo-Json -Compress
182-
$trustedStr = ($this.trustedRepository).ToString().ToLower()
183-
$inDesiredStr = ($this._inDesiredState).ToString().ToLower()
184-
$retVal = "{`"repositoryName`":$repoNameJson,`"resources`":$resourcesArrayJson,`"trustedRepository`":$trustedStr,`"_inDesiredState`":$inDesiredStr}"
189+
$retVal = [ordered]@{
190+
repositoryName = $this.repositoryName
191+
resources = [object[]]$resourceObjects
192+
trustedRepository = $this.trustedRepository
193+
_inDesiredState = $this._inDesiredState
194+
} | ConvertTo-Json -Compress -Depth 5 -EnumsAsStrings
185195
Write-Trace -message "Serialized JSON: $retVal" -level trace
186196
return $retVal
187197
}
@@ -573,7 +583,14 @@ function WhatIfPSResourceList {
573583
if (-not $resourceDesiredState._exist -and $null -ne $currentResource -and $currentResource._exist) {
574584
$msg = "Would uninstall resource '$name'"
575585
Write-Trace -message "WhatIf: $msg." -level debug
576-
$resource = [PSResource]::new($name)
586+
$resource = [PSResource]::new(
587+
$currentResource.name,
588+
$currentResource.version,
589+
$currentResource.scope,
590+
$currentResource.repositoryName,
591+
$currentResource.preRelease
592+
)
593+
$resource._exist = $false
577594
$resource._metadata = [pscustomobject]@{ whatIf = @($msg) }
578595
$projectedResources += $resource
579596
}

0 commit comments

Comments
 (0)