Files
Umbraco-CMS/src/Umbraco.Core/PropertyEditors/ConfigurationEditor.cs

129 lines
5.0 KiB
C#
Raw Normal View History

2018-01-24 11:44:44 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
2018-03-05 14:59:23 +01:00
using Umbraco.Core.Serialization;
2018-01-24 11:44:44 +01:00
namespace Umbraco.Core.PropertyEditors
2018-01-24 11:44:44 +01:00
{
/// <summary>
/// Represents a data type configuration editor.
/// </summary>
2018-02-15 14:49:32 +01:00
public class ConfigurationEditor : IConfigurationEditor
2018-01-24 11:44:44 +01:00
{
private IDictionary<string, object> _defaultConfiguration;
2018-01-24 11:44:44 +01:00
/// <summary>
/// Initializes a new instance of the <see cref="ConfigurationEditor"/> class.
/// </summary>
public ConfigurationEditor()
{
Fields = new List<ConfigurationField>();
_defaultConfiguration = new Dictionary<string, object>();
2018-01-24 11:44:44 +01:00
}
/// <summary>
/// Initializes a new instance of the <see cref="ConfigurationEditor"/> class.
/// </summary>
protected ConfigurationEditor(List<ConfigurationField> fields)
{
Fields = fields;
}
/// <summary>
/// Gets the fields.
/// </summary>
[DataMember(Name = "fields")]
2018-01-24 11:44:44 +01:00
public List<ConfigurationField> Fields { get; }
/// <summary>
2018-02-15 14:49:32 +01:00
/// Gets a field by its property name.
2018-01-24 11:44:44 +01:00
/// </summary>
2018-02-05 17:48:54 +01:00
/// <remarks>Can be used in constructors to add infos to a field that has been defined
/// by a property marked with the <see cref="ConfigurationFieldAttribute"/>.</remarks>
protected ConfigurationField Field(string name)
=> Fields.First(x => x.PropertyName == name);
2018-01-24 11:44:44 +01:00
/// <summary>
/// Gets the configuration as a typed object.
/// </summary>
2018-01-24 11:44:44 +01:00
public static TConfiguration ConfigurationAs<TConfiguration>(object obj)
{
if (obj == null) return default;
2018-01-24 11:44:44 +01:00
if (obj is TConfiguration configuration) return configuration;
throw new InvalidCastException(
$"Cannot cast configuration of type {obj.GetType().Name} to {typeof(TConfiguration).Name}.");
2018-01-24 11:44:44 +01:00
}
2018-11-15 08:13:40 +01:00
/// <summary>
/// Converts a configuration object into a serialized database value.
/// </summary>
public static string ToDatabase(object configuration, IConfigurationEditorJsonSerializer configurationEditorJsonSerializer)
=> configuration == null ? null : configurationEditorJsonSerializer.Serialize(configuration);
2018-11-15 08:13:40 +01:00
2018-02-15 14:49:32 +01:00
/// <inheritdoc />
[DataMember(Name = "defaultConfig")]
public virtual IDictionary<string, object> DefaultConfiguration
{
get => _defaultConfiguration;
set => _defaultConfiguration = value;
}
2018-02-05 17:48:54 +01:00
/// <inheritdoc />
public virtual object DefaultConfigurationObject => DefaultConfiguration;
2018-02-15 14:49:32 +01:00
/// <inheritdoc />
public virtual bool IsConfiguration(object obj) => obj is IDictionary<string, object>;
2018-02-07 13:35:59 +01:00
2018-02-15 14:49:32 +01:00
/// <inheritdoc />
public virtual object FromDatabase(string configurationJson, IConfigurationEditorJsonSerializer configurationEditorJsonSerializer)
2018-02-05 17:48:54 +01:00
=> string.IsNullOrWhiteSpace(configurationJson)
? new Dictionary<string, object>()
: configurationEditorJsonSerializer.Deserialize<Dictionary<string, object>>(configurationJson);
2018-02-05 17:48:54 +01:00
2018-02-15 14:49:32 +01:00
/// <inheritdoc />
public virtual object FromConfigurationEditor(IDictionary<string, object> editorValues, object configuration)
2018-01-24 11:44:44 +01:00
{
// by default, return the posted dictionary
2018-02-05 17:48:54 +01:00
// 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();
2018-02-05 17:48:54 +01:00
foreach (var key in keys) editorValues.Remove(key);
return editorValues;
2018-01-24 11:44:44 +01:00
}
2018-02-15 14:49:32 +01:00
/// <inheritdoc />
public virtual IDictionary<string, object> ToConfigurationEditor(object configuration)
2018-01-24 11:44:44 +01:00
{
// editors that do not override ToEditor/FromEditor have their configuration
// as a dictionary of <string, object> and, by default, we merge their default
// configuration with their current configuration
2018-01-26 17:55:20 +01:00
if (configuration == null)
configuration = new Dictionary<string, object>();
2018-01-24 11:44:44 +01:00
if (!(configuration is IDictionary<string, object> c))
throw new ArgumentException(
$"Expecting a {typeof(Dictionary<string, object>).Name} instance but got {configuration.GetType().Name}.",
nameof(configuration));
2018-01-24 11:44:44 +01:00
// clone the default configuration, and apply the current configuration values
2018-02-05 17:48:54 +01:00
var d = new Dictionary<string, object>(DefaultConfiguration);
foreach (var (key, value) in c)
2018-02-05 17:48:54 +01:00
d[key] = value;
return d;
2018-01-24 11:44:44 +01:00
}
2018-02-05 17:48:54 +01:00
2018-02-15 14:49:32 +01:00
/// <inheritdoc />
public virtual IDictionary<string, object> ToValueEditor(object configuration)
2018-02-05 17:48:54 +01:00
=> ToConfigurationEditor(configuration);
2018-03-05 14:59:23 +01:00
2018-01-24 11:44:44 +01:00
}
}