Allow IPropertyValueConverter to determine if a property has value

This commit is contained in:
Stephan
2018-07-16 19:14:00 +02:00
parent 2051caaa01
commit 05317bb5e6
6 changed files with 40 additions and 25 deletions

View File

@@ -77,7 +77,7 @@ namespace Umbraco.Core.Models.PublishedContent
/// <summary>
/// Gets the published content type containing the property type.
/// </summary>
/// </summary>
public PublishedContentType ContentType { get; internal set; } // internally set by PublishedContentType constructor
/// <summary>
@@ -193,6 +193,20 @@ namespace Umbraco.Core.Models.PublishedContent
_modelClrType = _converter == null ? typeof (object) : _converter.GetPropertyValueType(this);
}
/// <summary>
/// Determines whether a source value is an actual value, or not a value.
/// </summary>
/// <remarks>Used by property.HasValue and, for instance, in fallback scenarios.</remarks>
public bool IsValue(object value)
{
// if we have a converter, use the converter,
// otherwise use the old magic null & string comparisons
return _converter == null
? value != null && (!(value is string) || string.IsNullOrWhiteSpace((string) value) == false)
: _converter.IsValue(value);
}
/// <summary>
/// Gets the property cache level.
/// </summary>

View File

@@ -17,6 +17,11 @@ namespace Umbraco.Core.PropertyEditors
/// <returns>A value indicating whether the converter supports a property type.</returns>
bool IsConverter(PublishedPropertyType propertyType);
/// <summary>
/// Determines whether a source value is an actual value, or not a value.
/// </summary>
bool IsValue(object value);
/// <summary>
/// Gets the type of values returned by the converter.
/// </summary>

View File

@@ -9,33 +9,24 @@ namespace Umbraco.Core.PropertyEditors
public abstract class PropertyValueConverterBase : IPropertyValueConverter
{
public virtual bool IsConverter(PublishedPropertyType propertyType)
{
return false;
}
=> false;
public bool IsValue(object value)
=> value != null && (!(value is string) || string.IsNullOrWhiteSpace((string) value) == false);
public virtual Type GetPropertyValueType(PublishedPropertyType propertyType)
{
return typeof (object);
}
=> typeof (object);
public virtual PropertyCacheLevel GetPropertyCacheLevel(PublishedPropertyType propertyType)
{
return PropertyCacheLevel.Snapshot;
}
=> PropertyCacheLevel.Snapshot;
public virtual object ConvertSourceToIntermediate(IPublishedElement owner, PublishedPropertyType propertyType, object source, bool preview)
{
return source;
}
=> source;
public virtual object ConvertIntermediateToObject(IPublishedElement owner, PublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
{
return inter;
}
=> inter;
public virtual object ConvertIntermediateToXPath(IPublishedElement owner, PublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
{
return inter?.ToString() ?? string.Empty;
}
=> inter?.ToString() ?? string.Empty;
}
}

View File

@@ -47,6 +47,9 @@ namespace Umbraco.Tests.Published
private class SimpleConverter1 : IPropertyValueConverter
{
public bool IsValue(object value)
=> value != null && (!(value is string) || string.IsNullOrWhiteSpace((string) value) == false);
public bool IsConverter(PublishedPropertyType propertyType)
=> propertyType.EditorAlias.InvariantEquals("Umbraco.Void");
@@ -117,6 +120,9 @@ namespace Umbraco.Tests.Published
_cacheLevel = cacheLevel;
}
public bool IsValue(object value)
=> value != null && (!(value is string) || string.IsNullOrWhiteSpace((string) value) == false);
public bool IsConverter(PublishedPropertyType propertyType)
=> propertyType.EditorAlias.InvariantEquals("Umbraco.Void");

View File

@@ -210,6 +210,9 @@ namespace Umbraco.Tests.Published
public int SourceConverts { get; private set; }
public int InterConverts { get; private set; }
public bool IsValue(object value)
=> value != null && (!(value is string) || string.IsNullOrWhiteSpace((string) value) == false);
public bool IsConverter(PublishedPropertyType propertyType)
=> propertyType.EditorAlias.InvariantEquals("Umbraco.Void");
@@ -235,4 +238,4 @@ namespace Umbraco.Tests.Published
=> ((int) inter).ToString();
}
}
}
}

View File

@@ -90,11 +90,7 @@ namespace Umbraco.Web.PublishedCache.NuCache
public override bool HasValue(string culture = null, string segment = null)
{
ContextualizeVariation(ref culture, ref segment);
var sourceValue = GetSourceValue(culture, segment);
return sourceValue != null &&
(!(sourceValue is string) || string.IsNullOrWhiteSpace((string) sourceValue) == false);
return PropertyType.IsValue(GetSourceValue(culture, segment));
}
// used to cache the CacheValues of this property