private void AddResourceMapping()

in src/Analyzer.TemplateProcessor/ArmTemplateProcessor.cs [291:333]


        private void AddResourceMapping(string expandedTemplatePath, string originalTemplatePath)
        {
            // Save all permutations of the resource path based off values already present 
            // in the dictionary with mapping. This is necessary to report an issue in
            // a copied nth grandchild resource.
            var tokens = expandedTemplatePath.Split('.');
            for (int i = 0; i < tokens.Length - 1; i++)
            {
                string segmentOfExpandedPath = string.Join('.', tokens[..(i + 1)]);

                // Each segment of a path in the expanded template corresponds to one resource in the original template,
                // not necessarily the same index of resource, since copy loops reorder resources after processing.
                // And each resource in the original template could be copied to multiple locations in the expanded template:
                string originalPathOfSegmentOfExpandedPath;
                if (expandedToOriginalMapping.TryGetValue(segmentOfExpandedPath, out originalPathOfSegmentOfExpandedPath))
                {
                    if (originalToExpandedMapping.TryGetValue(originalPathOfSegmentOfExpandedPath, out List<string> copiedLocationsOfPathSegment))
                    {
                        foreach (string copiedLocationOfPathSegment in copiedLocationsOfPathSegment)
                        {
                            // This check is done to avoid assuming that the resource was copied to other top-level resources that don't necessarily depend on it:
                            if (copiedLocationOfPathSegment.Split('.').Length > 1)
                            {
                                var fullExpandedPath = $"{copiedLocationOfPathSegment}.{string.Join('.', tokens[(i + 1)..])}";
                                ResourceMappings.TryAdd(fullExpandedPath, originalTemplatePath);
                            }
                        }
                    }
                }
            }

            if (!ResourceMappings.TryAdd(expandedTemplatePath, originalTemplatePath) && ResourceMappings[expandedTemplatePath] != originalTemplatePath)
            {
                throw new Exception("Error processing resource dependencies: " +
                    $"{expandedTemplatePath} currently maps to {ResourceMappings[expandedTemplatePath]}, instead of {originalTemplatePath}.");
            }

            expandedToOriginalMapping[expandedTemplatePath] = originalTemplatePath;
            if (!originalToExpandedMapping.TryAdd(originalTemplatePath, new List<string> { expandedTemplatePath }))
            {
                originalToExpandedMapping[originalTemplatePath].Add(expandedTemplatePath);
            }
        }