Esempio n. 1
0
        internal TemplatePackageSearchData(JObject jObject, ILogger logger, IReadOnlyDictionary <string, Func <object, object> >?additionalDataReaders = null)
        {
            if (jObject is null)
            {
                throw new ArgumentNullException(nameof(jObject));
            }

            if (logger is null)
            {
                throw new ArgumentNullException(nameof(logger));
            }
            string?name = jObject.ToString(nameof(Name));

            Name = !string.IsNullOrWhiteSpace(name) ? name !
                : throw new ArgumentException($"{nameof(jObject)} doesn't have {nameof(Name)} property or it is not a string.", nameof(jObject));
            Version        = jObject.ToString(nameof(Version));
            TotalDownloads = jObject.ToInt32(nameof(TotalDownloads));
            Owners         = jObject.Get <JToken>(nameof(Owners)).JTokenStringOrArrayToCollection(Array.Empty <string>());
            Verified       = jObject.ToBool(nameof(Verified));

            Description = jObject.ToString(nameof(Description));
            IconUrl     = jObject.ToString(nameof(IconUrl));

            JArray?templatesData = jObject.Get <JArray>(nameof(Templates));

            if (templatesData == null)
            {
                throw new ArgumentException($"{nameof(jObject)} doesn't have {nameof(Templates)} property or it is not an array.", nameof(jObject));
            }
            List <TemplateSearchData> templates = new List <TemplateSearchData>();

            foreach (JToken template in templatesData)
            {
                try
                {
                    if (template is JObject templateObj)
                    {
                        templates.Add(new TemplateSearchData(templateObj, logger, additionalDataReaders));
                    }
                    else
                    {
                        throw new Exception($"Unexpected data in template package cache data, property: {nameof(Templates)}.");
                    }
                }
                catch (Exception ex)
                {
                    logger.LogDebug($"Template package {Name}: Failed to read template data {template}, details: {ex}.");
                }
            }
            Templates = templates;
            //read additional data
            if (additionalDataReaders != null)
            {
                AdditionalData = TemplateSearchCache.ReadAdditionalData(jObject, additionalDataReaders, logger);
            }
            else
            {
                AdditionalData = new Dictionary <string, object>();
            }
        }
Esempio n. 2
0
        internal TemplateSearchData(JObject jObject, ILogger logger, IReadOnlyDictionary <string, Func <object, object> >?additionalDataReaders = null)
        {
            if (jObject is null)
            {
                throw new ArgumentNullException(nameof(jObject));
            }

            if (logger is null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

#pragma warning disable CS0618 // Type or member is obsolete. The code will be moved to TemplateSearchData.Json when BlobStorageTemplateInfo is ready to be removed.
            TemplateInfo = BlobStorageTemplateInfo.FromJObject(jObject);
#pragma warning restore CS0618 // Type or member is obsolete

            //read additional data
            if (additionalDataReaders != null)
            {
                AdditionalData = TemplateSearchCache.ReadAdditionalData(jObject, additionalDataReaders, logger);
            }
            else
            {
                AdditionalData = new Dictionary <string, object>();
            }
        }