예제 #1
0
            internal static TemplateInfo FromJObject(JObject entry)
            {
                string identity                 = entry.ToString(nameof(Identity)) ?? throw new ArgumentException($"{nameof(entry)} doesn't have {nameof(Identity)} property.", nameof(entry));
                string name                     = entry.ToString(nameof(Name)) ?? throw new ArgumentException($"{nameof(entry)} doesn't have {nameof(Name)} property.", nameof(entry));
                string mountPointUri            = entry.ToString(nameof(MountPointUri)) ?? throw new ArgumentException($"{nameof(entry)} doesn't have {nameof(MountPointUri)} property.", nameof(entry));
                string configPlace              = entry.ToString(nameof(ConfigPlace)) ?? throw new ArgumentException($"{nameof(entry)} doesn't have {nameof(ConfigPlace)} property.", nameof(entry));
                JToken?shortNameToken           = entry.Get <JToken>(nameof(ShortNameList));
                IEnumerable <string> shortNames = shortNameToken.JTokenStringOrArrayToCollection(Array.Empty <string>());

                TemplateInfo info = new TemplateInfo(identity, name, shortNames, mountPointUri, configPlace);

                info.Author = entry.ToString(nameof(Author));
                JArray?classificationsArray = entry.Get <JArray>(nameof(Classifications));

                if (classificationsArray != null)
                {
                    List <string> classifications = new List <string>();
                    foreach (JToken item in classificationsArray)
                    {
                        classifications.Add(item.ToString());
                    }
                    info.Classifications = classifications;
                }

                info.DefaultName   = entry.ToString(nameof(DefaultName));
                info.Description   = entry.ToString(nameof(Description));
                info.GeneratorId   = Guid.Parse(entry.ToString(nameof(GeneratorId)));
                info.GroupIdentity = entry.ToString(nameof(GroupIdentity));
                info.Precedence    = entry.ToInt32(nameof(Precedence));

                info.LocaleConfigPlace = entry.ToString(nameof(LocaleConfigPlace));
                info.HostConfigPlace   = entry.ToString(nameof(HostConfigPlace));
                info.ThirdPartyNotices = entry.ToString(nameof(ThirdPartyNotices));

                JObject?baselineJObject = entry.Get <JObject>(nameof(ITemplateInfo.BaselineInfo));
                Dictionary <string, IBaselineInfo> baselineInfo = new Dictionary <string, IBaselineInfo>();

                if (baselineJObject != null)
                {
                    foreach (JProperty item in baselineJObject.Properties())
                    {
                        var defaultOverrides = item.Value.ToStringDictionary(propertyName: nameof(IBaselineInfo.DefaultOverrides));
                        if (defaultOverrides is null)
                        {
                            continue;
                        }

                        IBaselineInfo baseline = new BaselineCacheInfo(defaultOverrides, item.Value.ToString(nameof(IBaselineInfo.Description)));
                        baselineInfo.Add(item.Name, baseline);
                    }
                    info.BaselineInfo = baselineInfo;
                }

                //read parameters
                JArray?parametersArray = entry.Get <JArray>(nameof(Parameters));

                if (parametersArray != null)
                {
                    List <ITemplateParameter> templateParameters = new List <ITemplateParameter>();
                    foreach (JObject item in parametersArray)
                    {
                        templateParameters.Add(new TemplateParameter(item));
                    }
                    info.Parameters = templateParameters;
                }

                //read tags
                // tags are just "name": "description"
                // e.g.: "language": "C#"
                JObject?tagsObject = entry.Get <JObject>(nameof(TagsCollection));

                if (tagsObject != null)
                {
                    Dictionary <string, string> tags = new Dictionary <string, string>();
                    foreach (JProperty item in tagsObject.Properties())
                    {
                        tags.Add(item.Name.ToString(), item.Value.ToString());
                    }
                    info.TagsCollection = tags;
                }

                info.HostData = entry.Get <JObject>(nameof(info.HostData));
                JArray?postActionsArray = entry.Get <JArray>(nameof(info.PostActions));

                if (postActionsArray != null)
                {
                    List <Guid> postActions = new List <Guid>();
                    foreach (JToken item in postActionsArray)
                    {
                        if (Guid.TryParse(item.ToString(), out Guid id))
                        {
                            postActions.Add(id);
                        }
                    }
                    info.PostActions = postActions;
                }

                //read parameters
                JArray?constraintsArray = entry.Get <JArray>(nameof(info.Constraints));

                if (constraintsArray != null)
                {
                    List <TemplateConstraintInfo> constraints = new List <TemplateConstraintInfo>();
                    foreach (JObject item in constraintsArray)
                    {
                        string?type = item.ToString(nameof(TemplateConstraintInfo.Type));
                        if (string.IsNullOrWhiteSpace(type))
                        {
                            throw new ArgumentException($"{nameof(entry)} has {nameof(info.Constraints)} property which item doesn't have {nameof(TemplateConstraintInfo.Type)}.", nameof(entry));
                        }
                        constraints.Add(new TemplateConstraintInfo(type !, item.ToString(nameof(TemplateConstraintInfo.Args))));
                    }
                    info.Constraints = constraints;
                }

                return(info);
            }
        public static TemplateInfo FromJObject(JObject entry)
        {
            TemplateInfo info = new TemplateInfo();

            info.ConfigMountPointId = Guid.Parse(entry.ToString(nameof(TemplateInfo.ConfigMountPointId)));
            info.Author             = entry.ToString(nameof(TemplateInfo.Author));
            JArray classificationsArray = entry.Get <JArray>(nameof(TemplateInfo.Classifications));

            List <string> classifications = new List <string>();

            info.Classifications = classifications;
            //using (Timing.Over("Read classifications"))
            foreach (JToken item in classificationsArray)
            {
                classifications.Add(item.ToString());
            }

            info.DefaultName   = entry.ToString(nameof(TemplateInfo.DefaultName));
            info.Description   = entry.ToString(nameof(TemplateInfo.Description));
            info.Identity      = entry.ToString(nameof(TemplateInfo.Identity));
            info.GeneratorId   = Guid.Parse(entry.ToString(nameof(TemplateInfo.GeneratorId)));
            info.GroupIdentity = entry.ToString(nameof(TemplateInfo.GroupIdentity));
            info.Precedence    = entry.ToInt32(nameof(TemplateInfo.Precedence));
            info.Name          = entry.ToString(nameof(TemplateInfo.Name));
            info.ShortName     = entry.ToString(nameof(TemplateInfo.ShortName));

            // parse the cached tags
            Dictionary <string, ICacheTag> tags = new Dictionary <string, ICacheTag>();
            JObject tagsObject = entry.Get <JObject>(nameof(TemplateInfo.Tags));

            if (tagsObject != null)
            {
                foreach (JProperty item in tagsObject.Properties())
                {
                    Dictionary <string, string> choicesAndDescriptions = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
                    JObject cdToken = item.Value.Get <JObject>(nameof(ICacheTag.ChoicesAndDescriptions));
                    foreach (JProperty cdPair in cdToken.Properties())
                    {
                        choicesAndDescriptions.Add(cdPair.Name.ToString(), cdPair.Value.ToString());
                    }

                    ICacheTag cacheTag = new CacheTag(
                        item.Value.ToString(nameof(ICacheTag.Description)),
                        choicesAndDescriptions,
                        item.Value.ToString(nameof(ICacheTag.DefaultValue)));
                    tags.Add(item.Name.ToString(), cacheTag);
                }
            }
            info.Tags = tags;

            // parse the cached params
            JObject cacheParamsObject = entry.Get <JObject>(nameof(TemplateInfo.CacheParameters));
            Dictionary <string, ICacheParameter> cacheParams = new Dictionary <string, ICacheParameter>();

            if (cacheParamsObject != null)
            {
                foreach (JProperty item in cacheParamsObject.Properties())
                {
                    ICacheParameter param = new CacheParameter
                    {
                        DataType     = item.Value.ToString(nameof(ICacheParameter.DataType)),
                        DefaultValue = item.Value.ToString(nameof(ICacheParameter.DefaultValue)),
                        Description  = item.Value.ToString(nameof(ICacheParameter.Description))
                    };

                    cacheParams[item.Name.ToString()] = param;
                }
            }
            info.CacheParameters = cacheParams;

            info.ConfigPlace = entry.ToString(nameof(TemplateInfo.ConfigPlace));
            info.LocaleConfigMountPointId = Guid.Parse(entry.ToString(nameof(TemplateInfo.LocaleConfigMountPointId)));
            info.LocaleConfigPlace        = entry.ToString(nameof(TemplateInfo.LocaleConfigPlace));

            info.HostConfigMountPointId = Guid.Parse(entry.ToString(nameof(TemplateInfo.HostConfigMountPointId)));
            info.HostConfigPlace        = entry.ToString(nameof(TemplateInfo.HostConfigPlace));

            return(info);
        }