Skip to content

Commit d3e5b64

Browse files
Inner templates improvements (#589)
* 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]>
1 parent c899a92 commit d3e5b64

8 files changed

+380
-108
lines changed

arm-ttk/Expand-AzTemplate.ps1

+15-8
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ function Expand-AzTemplate
146146
'MainTemplatePath', 'MainTemplateObject', 'MainTemplateText',
147147
'MainTemplateResources','MainTemplateVariables','MainTemplateParameters', 'MainTemplateOutputs', 'TemplateMetadata',
148148
'isParametersFile', 'ParameterFileName', 'ParameterObject', 'ParameterText',
149-
'InnerTemplates', 'InnerTemplatesText', 'ParentTemplateText', 'ParentTemplateObject',
149+
'InnerTemplates', 'InnerTemplatesText', 'InnerTemplatesNames','InnerTemplatesLocations','ParentTemplateText', 'ParentTemplateObject',
150150
'ExpandedTemplateText', 'ExpandedTemplateObject'
151151

152152
foreach ($_ in $WellKnownVariables) {
@@ -245,20 +245,27 @@ function Expand-AzTemplate
245245
Find-JsonContent -InputObject $fileObject.Object -Key template |
246246
Where-Object { $_.expressionEvaluationOptions.scope -eq 'inner' -or $_.jsonPath -like '*.policyRule.*' } |
247247
Sort-Object JSONPath -Descending
248-
})
249-
#* InnerTemplatesText (an array of the text of each inner template)
250-
$fileObject.InnerTemplatesText = @(if ($fileObject.innerTemplates) {
248+
})
249+
#* InnerTemplatesText (an array of the text of each inner template)
250+
$fileObject.InnerTemplatesText = @()
251+
#* InnerTemplateNames (an array of the name of each inner template)
252+
$fileObject.InnerTemplatesNames = @()
253+
#* InnerTemplateLocations (an array of the resolved locations of each inner template)
254+
$fileObject.InnerTemplatesLocations = @()
255+
if ($fileObject.innerTemplates) {
251256
$anyProblems = $false
252257
foreach ($it in $fileObject.innerTemplates) {
253258
$foundInnerTemplate = $it | Resolve-JSONContent -JsonText $fileObject.Text
254-
if (-not $foundInnerTemplate) { $anyProblems = $true; break }
255-
$foundInnerTemplate.Content -replace '"template"\s{0,}\:\s{0,}'
259+
if (-not $foundInnerTemplate) { $anyProblems = $true; continue }
260+
$fileObject.InnerTemplatesText += $foundInnerTemplate.Content -replace '^\s{0,}"template"\s{0,}\:\s{0,}'
261+
$fileObject.InnerTemplatesNames += $it.ParentObject[0].Name
262+
$fileObject.InnerTemplatesLocations += $foundInnerTemplate
256263
}
257-
264+
258265
if ($anyProblems) {
259266
Write-Error "Could not extract inner templates for '$TemplatePath'." -ErrorId InnerTemplate.Extraction.Error
260267
}
261-
})
268+
}
262269
$fileObject
263270
}
264271

arm-ttk/Find-JsonContent.ps1

+13-6
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@
9090
if (-not $InputObject) { return }
9191

9292

93-
$index = -1
93+
$index = -1
94+
$originalProperty = @() + $Property
9495
foreach ($in in $InputObject) {
9596
if (-not $in) { continue }
9697
$index++
@@ -106,12 +107,16 @@
106107
($NotLike -and $in.$key -notlike $Value) -or
107108
($NotMatch -and $in.$key -notmatch $Value) -or
108109
($in.$key -eq $Value -and -not ($NotLike -or $NotMatch))) {
109-
if ($InputObject -is [Collections.IList] -and $Property) {
110-
$property += "[$index].$($key)"
111-
} else {
112-
$property += $key
113-
}
110+
111+
112+
if ($InputObject -is [Collections.IList] -and $Property) {
113+
$property += "[$index]"
114+
}
115+
$property += $key
116+
114117
. $outputMatch $in
118+
119+
$property = $originalProperty
115120
}
116121
}
117122
} elseif ($PSCmdlet.ParameterSetName -eq 'Key') {
@@ -178,6 +183,8 @@
178183

179184
Find-JsonContent @mySplat -InputObject $prop.Value
180185
}
186+
187+
181188
}
182189
}
183190
}

arm-ttk/Resolve-JSONContent.ps1

+3-3
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@
256256
Line = [Regex]::new('(?>\r\n|\n|\A)', 'RightToLeft').Matches(
257257
$JSONText, $indexMatch.Index
258258
).Count
259-
Column = $listMatch.Groups["ListItem"].Index + $(
259+
Column = $listMatch.Groups["ListItem"].Index - $(
260260
$m = [Regex]::new('(?>\r\n|\n|\A)', 'RightToLeft').Match(
261261
$JSONText, $indexMatch.Index)
262262
$m.Index + $m.Length
@@ -276,9 +276,9 @@
276276
$JSONText, $propMatchIndex
277277
).Count
278278
Content = $JSONText.Substring($propMatchIndex, $propMatchLength)
279-
Column = $propMatch.Groups["Name"].Index - 1 + $(
279+
Column = $propMatch.Groups["Name"].Index - $(
280280
$m = [Regex]::new('(?>\r\n|\n|\A)', 'RightToLeft').Match(
281-
$JSONText, $propMatch.Groups["Name"].Index - 1)
281+
$JSONText, $propMatch.Groups["Name"].Index)
282282
$m.Index + $m.Length
283283
) + 1
284284
PSTypeName = 'JSON.Content.Location'

0 commit comments

Comments
 (0)