private static bool TryReadPackToTemplateMap(JObject cacheObject, out IReadOnlyDictionary <string, PackToTemplateEntry> packToTemplateMap)
        {
            try
            {
                if (!cacheObject.TryGetValue(nameof(TemplateDiscoveryMetadata.PackToTemplateMap), out JToken packToTemplateMapToken) ||
                    !(packToTemplateMapToken is JObject packToTemplateMapObject))
                {
                    packToTemplateMap = null;
                    return(false);
                }

                Dictionary <string, PackToTemplateEntry> workingPackToTemplateMap = new Dictionary <string, PackToTemplateEntry>();

                foreach (JProperty packEntry in packToTemplateMapObject.Properties())
                {
                    if (packEntry != null)
                    {
                        string  packName   = packEntry.Name.ToString();
                        JObject entryValue = (JObject)packEntry.Value;

                        if (entryValue.TryGetValue(nameof(PackToTemplateEntry.Version), StringComparison.OrdinalIgnoreCase, out JToken versionToken) &&
                            versionToken.Type == JTokenType.String &&
                            entryValue.TryGetValue(nameof(PackToTemplateEntry.TemplateIdentificationEntry), StringComparison.OrdinalIgnoreCase, out JToken identificationToken) &&
                            identificationToken is JArray identificationArray)
                        {
                            string version = versionToken.Value <string>();
                            List <TemplateIdentificationEntry> templatesInPack = new List <TemplateIdentificationEntry>();

                            foreach (JObject templateIdentityInfo in identificationArray)
                            {
                                string identity      = templateIdentityInfo.Value <string>(nameof(TemplateIdentificationEntry.Identity));
                                string groupIdentity = templateIdentityInfo.Value <string>(nameof(TemplateIdentificationEntry.GroupIdentity));

                                TemplateIdentificationEntry deserializedEntry = new TemplateIdentificationEntry(identity, groupIdentity);
                                templatesInPack.Add(deserializedEntry);
                            }

                            workingPackToTemplateMap[packName] = new PackToTemplateEntry(version, templatesInPack);
                            if (entryValue.TryGetValue(nameof(PackToTemplateEntry.TotalDownloads), out JToken totalDownloadsToken) &&
                                long.TryParse(totalDownloadsToken.Value <string>(), out long totalDownloads))
                            {
                                workingPackToTemplateMap[packName].TotalDownloads = totalDownloads;
                            }
                        }
                    }
                }

                packToTemplateMap = workingPackToTemplateMap;
                return(true);
            }
            catch
            {
                packToTemplateMap = null;
                return(false);
            }
        }
Example #2
0
        private static bool TryReadPackToTemplateMap(ILogger logger, JObject cacheObject, out IReadOnlyDictionary <string, PackToTemplateEntry>?packToTemplateMap)
        {
            logger.LogDebug($"Reading package information.");
            try
            {
                if (!cacheObject.TryGetValue(nameof(TemplateDiscoveryMetadata.PackToTemplateMap), out JToken? packToTemplateMapToken) ||
                    !(packToTemplateMapToken is JObject packToTemplateMapObject))
                {
                    logger.LogDebug($"Failed to read package info entries. Details: no PackToTemplateMap property found.");
                    packToTemplateMap = null;
                    return(false);
                }

                Dictionary <string, PackToTemplateEntry> workingPackToTemplateMap = new Dictionary <string, PackToTemplateEntry>();

                foreach (JProperty packEntry in packToTemplateMapObject.Properties())
                {
                    if (packEntry != null)
                    {
                        string  packName   = packEntry.Name.ToString();
                        JObject entryValue = (JObject)packEntry.Value;

                        if (entryValue.TryGetValue(nameof(PackToTemplateEntry.Version), StringComparison.OrdinalIgnoreCase, out JToken? versionToken) &&
                            versionToken.Type == JTokenType.String &&
                            entryValue.TryGetValue(nameof(PackToTemplateEntry.TemplateIdentificationEntry), StringComparison.OrdinalIgnoreCase, out JToken? identificationToken) &&
                            identificationToken is JArray identificationArray)
                        {
                            string?version = versionToken.Value <string>();
                            if (version == null)
                            {
                                throw new Exception("Version value is null.");
                            }
                            List <TemplateIdentificationEntry> templatesInPack = new List <TemplateIdentificationEntry>();

                            foreach (JObject templateIdentityInfo in identificationArray)
                            {
                                string?identity      = templateIdentityInfo.Value <string>(nameof(TemplateIdentificationEntry.Identity));
                                string?groupIdentity = templateIdentityInfo.Value <string>(nameof(TemplateIdentificationEntry.GroupIdentity));

                                if (identity == null)
                                {
                                    throw new Exception("Identity value is null.");
                                }
                                TemplateIdentificationEntry deserializedEntry = new TemplateIdentificationEntry(identity, groupIdentity);
                                templatesInPack.Add(deserializedEntry);
                            }

                            workingPackToTemplateMap[packName] = new PackToTemplateEntry(version, templatesInPack);
                            if (entryValue.TryGetValue(nameof(PackToTemplateEntry.TotalDownloads), out JToken? totalDownloadsToken) &&
                                long.TryParse(totalDownloadsToken.Value <string>(), out long totalDownloads))
                            {
                                workingPackToTemplateMap[packName].TotalDownloads = totalDownloads;
                            }
                        }
                    }
                }

                logger.LogDebug($"Successfully read {workingPackToTemplateMap.Count} packages.");
                packToTemplateMap = workingPackToTemplateMap;
                return(true);
            }
            catch (Exception ex)
            {
                logger.LogDebug($"Failed to read package info entries. Details: {ex}");
                packToTemplateMap = null;
                return(false);
            }
        }