public IEnumerable ResolveResourceType()

in src/Analyzer.Utilities/JsonPathResolver.cs [76:115]


        public IEnumerable<IJsonPathResolver> ResolveResourceType(string resourceType)
        {
            string fullPath = this.currentPath + ".resources[*]";
            if (!resolvedPaths.TryGetValue(fullPath, out var resolvedTokens))
            {
                var resources = this.currentScope.InsensitiveTokens("resources[*]");
                resolvedTokens = resources.Select(r => (FieldContent)r).ToList();
                resolvedPaths[fullPath] = resolvedTokens;
            }

            // When trying to resolve Microsoft.Web/sites/siteextensions, for example, we should consider that siteextensions can be a child resource of Microsoft.Web/sites (a parent of the original resource type)
            var resourceTypeParents = new List<string> { };
            var indexesOfTypesSeparators = Regex.Matches(resourceType, "/").Select(m => m.Index).Skip(1);
            foreach (var indexOfTypeSeparator in indexesOfTypesSeparators)
            {
                resourceTypeParents.Add(resourceType[..indexOfTypeSeparator]);
            }

            static bool resourceTypesAreEqual(FieldContent jTokenResourceType, string stringResourceType)
            {
                return string.Equals(jTokenResourceType.Value.InsensitiveToken("type")?.Value<string>(), stringResourceType, StringComparison.OrdinalIgnoreCase);
            }

            foreach (var resource in resolvedTokens)
            {
                if (resourceTypesAreEqual(resource, resourceType))
                {
                    yield return new JsonPathResolver(resource.Value, resource.Value.Path, this.resolvedPaths);
                }
                else if (resourceTypeParents.Exists(parentResourceType => resourceTypesAreEqual(resource.Value, parentResourceType)))
                {
                    // In this case we still haven't matched the whole resource type
                    var subScope = new JsonPathResolver(resource.Value, resource.Value.Path, this.resolvedPaths);
                    foreach (var newJsonPathResolver in subScope.ResolveResourceType(resourceType))
                    {
                        yield return newJsonPathResolver;
                    }
                }
            }
        }