Merge pull request #8270 from ronaldbarendse/v8/bugfix/propertyvalueconverterbase

PropertyValueConverterBase cleanup: fix IsValue, obsolete HasValue, add documentation
This commit is contained in:
Shannon Deminick
2020-07-30 21:18:30 +10:00
committed by GitHub

View File

@@ -4,44 +4,57 @@ using Umbraco.Core.Models.PublishedContent;
namespace Umbraco.Core.PropertyEditors
{
/// <summary>
/// Provides a default overridable implementation for <see cref="IPropertyValueConverter"/> that does nothing.
/// Provides a default implementation for <see cref="IPropertyValueConverter" />.
/// </summary>
/// <seealso cref="Umbraco.Core.PropertyEditors.IPropertyValueConverter" />
public abstract class PropertyValueConverterBase : IPropertyValueConverter
{
/// <inheritdoc />
public virtual bool IsConverter(IPublishedPropertyType propertyType)
=> false;
/// <inheritdoc />
public virtual bool? IsValue(object value, PropertyValueLevel level)
{
switch (level)
{
case PropertyValueLevel.Source:
return value != null && (!(value is string) || string.IsNullOrWhiteSpace((string) value) == false);
// the default implementation uses the old magic null & string comparisons,
// other implementations may be more clever, and/or test the final converted object values
return value != null && (!(value is string stringValue) || !string.IsNullOrWhiteSpace(stringValue));
case PropertyValueLevel.Inter:
return null;
case PropertyValueLevel.Object:
return null;
default:
throw new NotSupportedException($"Invalid level: {level}.");
}
}
[Obsolete("This method is not part of the IPropertyValueConverter contract, therefore not used and will be removed in future versions; use IsValue instead.")]
public virtual bool HasValue(IPublishedProperty property, string culture, string segment)
{
// the default implementation uses the old magic null & string comparisons,
// other implementations may be more clever, and/or test the final converted object values
var value = property.GetSourceValue(culture, segment);
return value != null && (!(value is string) || string.IsNullOrWhiteSpace((string) value) == false);
return value != null && (!(value is string stringValue) || !string.IsNullOrWhiteSpace(stringValue));
}
/// <inheritdoc />
public virtual Type GetPropertyValueType(IPublishedPropertyType propertyType)
=> typeof (object);
=> typeof(object);
/// <inheritdoc />
public virtual PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType)
=> PropertyCacheLevel.Snapshot;
/// <inheritdoc />
public virtual object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview)
=> source;
/// <inheritdoc />
public virtual object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
=> inter;
/// <inheritdoc />
public virtual object ConvertIntermediateToXPath(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
=> inter?.ToString() ?? string.Empty;
}