Skip to content

Commit

Permalink
Inner templates improvements (#589)
Browse files Browse the repository at this point in the history
* Find-JsonContent:  Fixing Underlying issue of #583
2nd or more JSONPath in the same hierarchy would be returned corrupted

* Adding unit test for Find-JSONContent and multiple list items

* Test-AzTemplate:  Fixing #581, #583, #580, #583
* Returning one result per inner template node (this cleans up reporting) (#580, #581)
* Accepting return objects with a .JSONPath for easier line association
* Adding exception handling around [Matches] (#583)
* Adding additional context to Test-AzTemplate return value

* Parameters-Must-Be-Referenced:  Adding .JSONPath to TargetObject to enhance returns

* Test-AzTemplate:  Ensures empty results are counted as passed.

* Test-AzTemplate:  Fixing Typo the stopped errors from being populated

* Test-AzTemplate:  Adding -Verbose message when Resolve-JSONObject fails

* Test-AzTemplate: stringifying .TargetObject.JSONPath (for when more than one TargetObject is returned).  Adding leading space to NestedTemplate Grouping (Output Differentiation)

* Restoring .lineNumber property to TargetObject

* Adding test case for grouping with multiple inner templates

* Updating InnerTemplates Tests (Adding Tests to Reproduce #583)

* Expand-AzTemplate:  Adding InnerTemplatesText, InnerTemplatesNames, InnerTemplatesLocations to return.

* Resolve-JSONContent:  Fixing .Column

* Test-AzTemplate: Sorting on InnerTemplateStart, Adjusting Line Numbers for InnerTemplates, Carrying More InnerTemplate Context into Return

* Adding Isolated InnerTemplates Test File

* Adding detailed test for innertemplates

* Adding detailed test for innertemplates

* Removing older InnerTemplateTest

* Test-AzTemplate:  Safeguarding InnerTemplate Expansion

* Delete MultipleInnerTemplates.json

* Adding Test for Resolve-JSONContent.Column

* Fixing Test for Resolve-JSONContent.Column

Co-authored-by: James Brundage <@github.com>
Co-authored-by: Brian Moore <[email protected]>
  • Loading branch information
StartAutomating and bmoore-msft authored Mar 4, 2022
1 parent c899a92 commit d3e5b64
Show file tree
Hide file tree
Showing 8 changed files with 380 additions and 108 deletions.
23 changes: 15 additions & 8 deletions arm-ttk/Expand-AzTemplate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ function Expand-AzTemplate
'MainTemplatePath', 'MainTemplateObject', 'MainTemplateText',
'MainTemplateResources','MainTemplateVariables','MainTemplateParameters', 'MainTemplateOutputs', 'TemplateMetadata',
'isParametersFile', 'ParameterFileName', 'ParameterObject', 'ParameterText',
'InnerTemplates', 'InnerTemplatesText', 'ParentTemplateText', 'ParentTemplateObject',
'InnerTemplates', 'InnerTemplatesText', 'InnerTemplatesNames','InnerTemplatesLocations','ParentTemplateText', 'ParentTemplateObject',
'ExpandedTemplateText', 'ExpandedTemplateObject'

foreach ($_ in $WellKnownVariables) {
Expand Down Expand Up @@ -245,20 +245,27 @@ function Expand-AzTemplate
Find-JsonContent -InputObject $fileObject.Object -Key template |
Where-Object { $_.expressionEvaluationOptions.scope -eq 'inner' -or $_.jsonPath -like '*.policyRule.*' } |
Sort-Object JSONPath -Descending
})
#* InnerTemplatesText (an array of the text of each inner template)
$fileObject.InnerTemplatesText = @(if ($fileObject.innerTemplates) {
})
#* InnerTemplatesText (an array of the text of each inner template)
$fileObject.InnerTemplatesText = @()
#* InnerTemplateNames (an array of the name of each inner template)
$fileObject.InnerTemplatesNames = @()
#* InnerTemplateLocations (an array of the resolved locations of each inner template)
$fileObject.InnerTemplatesLocations = @()
if ($fileObject.innerTemplates) {
$anyProblems = $false
foreach ($it in $fileObject.innerTemplates) {
$foundInnerTemplate = $it | Resolve-JSONContent -JsonText $fileObject.Text
if (-not $foundInnerTemplate) { $anyProblems = $true; break }
$foundInnerTemplate.Content -replace '"template"\s{0,}\:\s{0,}'
if (-not $foundInnerTemplate) { $anyProblems = $true; continue }
$fileObject.InnerTemplatesText += $foundInnerTemplate.Content -replace '^\s{0,}"template"\s{0,}\:\s{0,}'
$fileObject.InnerTemplatesNames += $it.ParentObject[0].Name
$fileObject.InnerTemplatesLocations += $foundInnerTemplate
}

if ($anyProblems) {
Write-Error "Could not extract inner templates for '$TemplatePath'." -ErrorId InnerTemplate.Extraction.Error
}
})
}
$fileObject
}

Expand Down
19 changes: 13 additions & 6 deletions arm-ttk/Find-JsonContent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@
if (-not $InputObject) { return }


$index = -1
$index = -1
$originalProperty = @() + $Property
foreach ($in in $InputObject) {
if (-not $in) { continue }
$index++
Expand All @@ -106,12 +107,16 @@
($NotLike -and $in.$key -notlike $Value) -or
($NotMatch -and $in.$key -notmatch $Value) -or
($in.$key -eq $Value -and -not ($NotLike -or $NotMatch))) {
if ($InputObject -is [Collections.IList] -and $Property) {
$property += "[$index].$($key)"
} else {
$property += $key
}


if ($InputObject -is [Collections.IList] -and $Property) {
$property += "[$index]"
}
$property += $key

. $outputMatch $in

$property = $originalProperty
}
}
} elseif ($PSCmdlet.ParameterSetName -eq 'Key') {
Expand Down Expand Up @@ -178,6 +183,8 @@

Find-JsonContent @mySplat -InputObject $prop.Value
}


}
}
}
Expand Down
6 changes: 3 additions & 3 deletions arm-ttk/Resolve-JSONContent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
Line = [Regex]::new('(?>\r\n|\n|\A)', 'RightToLeft').Matches(
$JSONText, $indexMatch.Index
).Count
Column = $listMatch.Groups["ListItem"].Index + $(
Column = $listMatch.Groups["ListItem"].Index - $(
$m = [Regex]::new('(?>\r\n|\n|\A)', 'RightToLeft').Match(
$JSONText, $indexMatch.Index)
$m.Index + $m.Length
Expand All @@ -276,9 +276,9 @@
$JSONText, $propMatchIndex
).Count
Content = $JSONText.Substring($propMatchIndex, $propMatchLength)
Column = $propMatch.Groups["Name"].Index - 1 + $(
Column = $propMatch.Groups["Name"].Index - $(
$m = [Regex]::new('(?>\r\n|\n|\A)', 'RightToLeft').Match(
$JSONText, $propMatch.Groups["Name"].Index - 1)
$JSONText, $propMatch.Groups["Name"].Index)
$m.Index + $m.Length
) + 1
PSTypeName = 'JSON.Content.Location'
Expand Down
Loading

0 comments on commit d3e5b64

Please sign in to comment.