コード例 #1
0
        private static ResultWithErrors <ResourceTypeSchema[]> BuildSchemaCacheFromFilePaths(IEnumerable <string> filePaths)
        {
            var schemasByPath = filePaths
                                .Select(File.ReadAllText)
                                .Select(JObject.Parse)
                                .ToInsensitiveDictionary(
                keySelector: schema => GetRelativeSchemaPath(schema["id"].ToObject <string>()),
                elementSelector: schema => schema as JToken);

            var externalReferenceSchemasResult = SchemaUtils.GetExternalReferenceSchemas(schemasByPath, SchemaUtils.ExternalReferenceWhitelist.ToArray());

            if (externalReferenceSchemasResult.Errors.Any())
            {
                throw new InvalidOperationException($"Failed to initialize the offline schemas cache");
            }

            var schemaRefsByFile = SchemaUtils.TopLevelReferenceSchemas
                                   .SelectMany(schemaPath => SchemaUtils.GetExternalReferences(schemasByPath[schemaPath]))
                                   .Select(property => property.Value.ToObject <string>())
                                   .ToLookupOrdinalInsensitively(SchemaUtils.GetFilePathFromUri);

            var schemaKeysFound = schemaRefsByFile
                                  .Select(grouping => grouping.Key)
                                  .Where(filePath => !SchemaUtils.ExternalReferenceWhitelist.Contains(filePath))
                                  .Where(filePath => schemasByPath.ContainsKey(filePath))
                                  .ToArray();

            var schemaGroups = schemaKeysFound.GroupByInsensitively(keySelector: schemaKey => SchemaUtils.GetSchemaGroupKey(schemaKey));

            var schemaNormalizationResults = schemaGroups
                                             .Select(schemaGroup => schemaGroup.ToInsensitiveDictionary(keySelector: schemaKey => schemaKey, elementSelector: schemaKey => schemasByPath[schemaKey]))
                                             .SelectMany(schemaGroup => SchemaUtils.NormalizeAndFilterSchemaGroup(
                                                             schemaGroup: schemaGroup,
                                                             externalReferenceSchemas: externalReferenceSchemasResult.Value,
                                                             schemaRefsByFile: schemaRefsByFile))
                                             .ToInsensitiveDictionary(
                keySelector: normalizationResult => normalizationResult.Key,
                elementSelector: normalizationResult => normalizationResult.Value);

            var resourceTypeSchemasResults = schemaNormalizationResults
                                             .Where(kvp => kvp.Value.Value != null)
                                             .Select(kvp => new OfflineSchema(
                                                         providerNamespace: SchemaUtils.GetProviderNamespaceFromSchemaKey(kvp.Key),
                                                         apiVersion: SchemaUtils.GetVersionFromSchemaKey(kvp.Key),
                                                         schemaContent: kvp.Value.Value))
                                             .Select(schema => schema.GetResourceTypeSchemasResult());

            return(new ResultWithErrors <ResourceTypeSchema[]>
            {
                Value = resourceTypeSchemasResults.CollectValues().ToArray(),
                Errors = schemaNormalizationResults.Values.CollectErrors()
                         .ConcatArray(resourceTypeSchemasResults.CollectErrors()),
            });
        }