using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using Umbraco.Core.Serialization; namespace Umbraco.Core.PropertyEditors { /// /// Represents a data type configuration editor. /// public class ConfigurationEditor : IConfigurationEditor { private IDictionary _defaultConfiguration; /// /// Initializes a new instance of the class. /// public ConfigurationEditor() { Fields = new List(); _defaultConfiguration = new Dictionary(); } /// /// Initializes a new instance of the class. /// protected ConfigurationEditor(List fields) { Fields = fields; } /// /// Gets the fields. /// [DataMember(Name = "fields")] public List Fields { get; } /// /// Gets a field by its property name. /// /// Can be used in constructors to add infos to a field that has been defined /// by a property marked with the . protected ConfigurationField Field(string name) => Fields.First(x => x.PropertyName == name); /// /// Gets the configuration as a typed object. /// public static TConfiguration ConfigurationAs(object obj) { if (obj == null) return default; if (obj is TConfiguration configuration) return configuration; throw new InvalidCastException( $"Cannot cast configuration of type {obj.GetType().Name} to {typeof(TConfiguration).Name}."); } /// /// Converts a configuration object into a serialized database value. /// public static string ToDatabase(object configuration, IConfigurationEditorJsonSerializer configurationEditorJsonSerializer) => configuration == null ? null : configurationEditorJsonSerializer.Serialize(configuration); /// [DataMember(Name = "defaultConfig")] public virtual IDictionary DefaultConfiguration { get => _defaultConfiguration; set => _defaultConfiguration = value; } /// public virtual object DefaultConfigurationObject => DefaultConfiguration; /// public virtual bool IsConfiguration(object obj) => obj is IDictionary; /// public virtual object FromDatabase(string configurationJson, IConfigurationEditorJsonSerializer configurationEditorJsonSerializer) => string.IsNullOrWhiteSpace(configurationJson) ? new Dictionary() : configurationEditorJsonSerializer.Deserialize>(configurationJson); /// public virtual object FromConfigurationEditor(IDictionary editorValues, object configuration) { // by default, return the posted dictionary // but only keep entries that have a non-null/empty value // rest will fall back to default during ToConfigurationEditor() var keys = editorValues.Where(x => x.Value == null || x.Value is string stringValue && string.IsNullOrWhiteSpace(stringValue)) .Select(x => x.Key).ToList(); foreach (var key in keys) editorValues.Remove(key); return editorValues; } /// public virtual IDictionary ToConfigurationEditor(object configuration) { // editors that do not override ToEditor/FromEditor have their configuration // as a dictionary of and, by default, we merge their default // configuration with their current configuration if (configuration == null) configuration = new Dictionary(); if (!(configuration is IDictionary c)) throw new ArgumentException( $"Expecting a {typeof(Dictionary).Name} instance but got {configuration.GetType().Name}.", nameof(configuration)); // clone the default configuration, and apply the current configuration values var d = new Dictionary(DefaultConfiguration); foreach (var (key, value) in c) d[key] = value; return d; } /// public virtual IDictionary ToValueEditor(object configuration) => ToConfigurationEditor(configuration); } }