// assumes that parameters are consistent with the datatype configuration // value can be an enumeration of string, or a serialized value using storageType format private static void SetTagsValue(IProperty property, object?value, TagsStorageType storageType, IJsonSerializer serializer, char delimiter, string?culture) { if (value == null) { value = Enumerable.Empty <string>(); } // if value is already an enumeration of strings, just use it if (value is IEnumerable <string> tags1) { property.AssignTags(tags1, false, storageType, serializer, delimiter, culture); return; } // otherwise, deserialize value based upon storage type switch (storageType) { case TagsStorageType.Csv: var tags2 = value.ToString() !.Split(new[] { delimiter }, StringSplitOptions.RemoveEmptyEntries); property.AssignTags(tags2, false, storageType, serializer, delimiter, culture); break; case TagsStorageType.Json: try { IEnumerable <string>?tags3 = serializer.Deserialize <IEnumerable <string> >(value.ToString() !); property.AssignTags(tags3 ?? Enumerable.Empty <string>(), false, storageType, serializer, delimiter, culture); } catch (Exception ex) { StaticApplicationLogging.Logger.LogWarning( ex, "Could not automatically convert stored json value to an enumerable string '{Json}'", value.ToString()); } break; default: throw new ArgumentOutOfRangeException(nameof(storageType)); } }
/// <summary> /// Assign tags. /// </summary> /// <param name="property">The property.</param> /// <param name="serializer"></param> /// <param name="tags">The tags.</param> /// <param name="merge">A value indicating whether to merge the tags with existing tags instead of replacing them.</param> /// <param name="culture">A culture, for multi-lingual properties.</param> /// <param name="propertyEditors"></param> /// <param name="dataTypeService"></param> public static void AssignTags(this IProperty property, PropertyEditorCollection propertyEditors, IDataTypeService dataTypeService, IJsonSerializer serializer, IEnumerable <string> tags, bool merge = false, string?culture = null) { if (property == null) { throw new ArgumentNullException(nameof(property)); } TagConfiguration?configuration = property.GetTagConfiguration(propertyEditors, dataTypeService); if (configuration == null) { throw new NotSupportedException($"Property with alias \"{property.Alias}\" does not support tags."); } property.AssignTags(tags, merge, configuration.StorageType, serializer, configuration.Delimiter, culture); }