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>(); } }
public TemplateSearchData(ITemplateInfo templateInfo, IDictionary <string, object>?data = null) { if (templateInfo is null) { throw new ArgumentNullException(nameof(templateInfo)); } #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 = new BlobStorageTemplateInfo(templateInfo); #pragma warning restore CS0618 // Type or member is obsolete AdditionalData = data ?? new Dictionary <string, object>(); }
private static bool TryReadTemplateList( ILogger logger, JObject cacheObject, out IReadOnlyList <ITemplateInfo>?templateList) { logger.LogDebug($"Reading template list"); try { // This is lifted from TemplateCache.ParseCacheContent - almost identical if (cacheObject.TryGetValue(nameof(TemplateDiscoveryMetadata.TemplateCache), StringComparison.OrdinalIgnoreCase, out JToken? templateInfoToken)) { List <ITemplateInfo> buildingTemplateList = new List <ITemplateInfo>(); if (templateInfoToken is JArray arr) { foreach (JToken entry in arr) { if (entry != null && entry.Type == JTokenType.Object) { try { buildingTemplateList.Add(BlobStorageTemplateInfo.FromJObject((JObject)entry)); } catch (ArgumentException ex) { logger.LogDebug($"Failed to read template info entry, missing mandatory fields. Details: {ex}"); } } } } logger.LogDebug($"Successfully read {buildingTemplateList.Count} templates."); templateList = buildingTemplateList; return(true); } logger.LogDebug($"Failed to read template info entries. Details: no TemplateCache property found."); templateList = null; return(false); } catch (Exception ex) { logger.LogDebug($"Failed to read template info entries. Details: {ex}"); templateList = null; return(false); } }
private static bool TryReadTemplateList( IEngineEnvironmentSettings environmentSettings, JObject cacheObject, string cacheVersion, out IReadOnlyList <ITemplateInfo> templateList) { try { // This is lifted from TemplateCache.ParseCacheContent - almost identical if (cacheObject.TryGetValue(nameof(TemplateDiscoveryMetadata.TemplateCache), StringComparison.OrdinalIgnoreCase, out JToken templateInfoToken)) { List <ITemplateInfo> buildingTemplateList = new List <ITemplateInfo>(); if (templateInfoToken is JArray arr) { foreach (JToken entry in arr) { if (entry != null && entry.Type == JTokenType.Object) { try { buildingTemplateList.Add(BlobStorageTemplateInfo.FromJObject((JObject)entry)); } catch (ArgumentException ex) { environmentSettings.Host.LogDiagnosticMessage($"Failed to read template info entry, missing mandatory fields. Details: {ex}", "Search cache"); } } } } templateList = buildingTemplateList; return(true); } templateList = null; return(false); } catch (Exception ex) { environmentSettings.Host.LogDiagnosticMessage($"Failed to read template info entries. Details: {ex}", "Search cache"); templateList = null; return(false); } }
public static BlobStorageTemplateInfo 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)); JToken?shortNameToken = entry.Get <JToken>(nameof(ShortNameList)); IEnumerable <string> shortNames = shortNameToken?.JTokenStringOrArrayToCollection(Array.Empty <string>()) ?? throw new ArgumentException($"{nameof(entry)} doesn't have {nameof(ShortNameList)} property.", nameof(entry)); BlobStorageTemplateInfo info = new BlobStorageTemplateInfo(identity, name, shortNames); 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.Description = entry.ToString(nameof(Description)); info.GroupIdentity = entry.ToString(nameof(GroupIdentity)); info.Precedence = entry.ToInt32(nameof(Precedence)); 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()) { IBaselineInfo baseline = new BaselineCacheInfo() { Description = item.Value.ToString(nameof(IBaselineInfo.Description)), DefaultOverrides = item.Value.ToStringDictionary(propertyName: nameof(IBaselineInfo.DefaultOverrides)) }; baselineInfo.Add(item.Name, baseline); } info.BaselineInfo = baselineInfo; } 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 bool readParameters = false; List <ITemplateParameter> templateParameters = new List <ITemplateParameter>(); JArray?parametersArray = entry.Get <JArray>(nameof(Parameters)); if (parametersArray != null) { foreach (JObject item in parametersArray) { templateParameters.Add(new BlobTemplateParameter(item)); } readParameters = true; } JObject?tagsObject = entry.Get <JObject>(nameof(TagsCollection)); Dictionary <string, string> tags = new Dictionary <string, string>(); if (tagsObject != null) { foreach (JProperty item in tagsObject.Properties()) { tags.Add(item.Name.ToString(), item.Value.ToString()); } } //try read tags and parameters - for compatibility reason tagsObject = entry.Get <JObject>("tags"); if (tagsObject != null) { Dictionary <string, ICacheTag> legacyTags = new Dictionary <string, ICacheTag>(); foreach (JProperty item in tagsObject.Properties()) { if (item.Value.Type == JTokenType.String) { tags[item.Name.ToString()] = item.Value.ToString(); legacyTags[item.Name.ToString()] = new BlobLegacyCacheTag( description: null, choicesAndDescriptions: new Dictionary <string, string>() { { item.Value.ToString(), string.Empty } }, defaultValue: item.Value.ToString(), defaultIfOptionWithoutValue: null); } else if (item.Value is JObject tagObj) { JObject?choicesObject = tagObj.Get <JObject>("ChoicesAndDescriptions"); if (choicesObject != null && !readParameters) { Dictionary <string, ParameterChoice> choicesAndDescriptions = new Dictionary <string, ParameterChoice>(StringComparer.OrdinalIgnoreCase); Dictionary <string, string> legacyChoices = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); foreach (JProperty cdPair in choicesObject.Properties()) { choicesAndDescriptions[cdPair.Name.ToString()] = new ParameterChoice(null, cdPair.Value.ToString()); legacyChoices[cdPair.Name.ToString()] = cdPair.Value.ToString(); } templateParameters.Add( new BlobTemplateParameter(item.Name.ToString(), "choice") { Choices = choicesAndDescriptions }); legacyTags[item.Name.ToString()] = new BlobLegacyCacheTag( description: tagObj.ToString("description"), choicesAndDescriptions: legacyChoices, defaultValue: tagObj.ToString("defaultValue"), defaultIfOptionWithoutValue: tagObj.ToString("defaultIfOptionWithoutValue")); } tags[item.Name.ToString()] = tagObj.ToString("defaultValue") ?? ""; } } info.Tags = legacyTags; } JObject?cacheParametersObject = entry.Get <JObject>("cacheParameters"); if (!readParameters && cacheParametersObject != null) { Dictionary <string, ICacheParameter> legacyParams = new Dictionary <string, ICacheParameter>(); foreach (JProperty item in cacheParametersObject.Properties()) { JObject paramObj = (JObject)item.Value; if (paramObj == null) { continue; } string dataType = paramObj.ToString(nameof(BlobTemplateParameter.DataType)) ?? "string"; templateParameters.Add(new BlobTemplateParameter(item.Name.ToString(), dataType)); legacyParams[item.Name.ToString()] = new BlobLegacyCacheParameter( description: paramObj.ToString("description"), dataType: paramObj.ToString(nameof(BlobTemplateParameter.DataType)) ?? "string", defaultValue: paramObj.ToString("defaultValue"), defaultIfOptionWithoutValue: paramObj.ToString("defaultIfOptionWithoutValue")); } info.CacheParameters = legacyParams; } info.TagsCollection = tags; info.Parameters = templateParameters; return(info); }