Skip to content

Commit

Permalink
Added null checking when looking for best match culture in DoInnerGet…
Browse files Browse the repository at this point in the history
…VortoValue (relates to issue #41)
  • Loading branch information
mattbrailsford committed Mar 2, 2016
1 parent 1784520 commit 46b7443
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/Our.Umbraco.Vorto/Extensions/IPublishedContentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ private static bool DoInnerHasVortoValue(this IPublishedContent content, string
{
var bestMatchCultureName = vortoModel.FindBestMatchCulture(cultureName);
if (!bestMatchCultureName.IsNullOrWhiteSpace()
&& vortoModel.Values.ContainsKey(bestMatchCultureName)
&& vortoModel.Values[bestMatchCultureName] != null
&& !vortoModel.Values[bestMatchCultureName].ToString().IsNullOrWhiteSpace())
return true;
Expand Down Expand Up @@ -72,10 +73,13 @@ private static T DoInnerGetVortoValue<T>(this IPublishedContent content, string
{
// Get the serialized value
var bestMatchCultureName = vortoModel.FindBestMatchCulture(cultureName);
var value = vortoModel.Values[bestMatchCultureName];

if (value != null && !value.ToString().IsNullOrWhiteSpace())
if (!bestMatchCultureName.IsNullOrWhiteSpace()
&& vortoModel.Values.ContainsKey(bestMatchCultureName)
&& vortoModel.Values[bestMatchCultureName] != null
&& !vortoModel.Values[bestMatchCultureName].ToString().IsNullOrWhiteSpace())
{
var value = vortoModel.Values[bestMatchCultureName];

// Get target datatype
var targetDataType = VortoHelper.GetTargetDataTypeDefinition(vortoModel.DtdGuid);

Expand All @@ -85,30 +89,28 @@ private static T DoInnerGetVortoValue<T>(this IPublishedContent content, string
// just ignoring these when looking up converters.
// NB: IPropertyEditorValueConverter not to be confused with
// IPropertyValueConverter which are the ones most people are creating
var properyType = CreateDummyPropertyType(targetDataType.Id, targetDataType.PropertyEditorAlias, content.ContentType);
var properyType = CreateDummyPropertyType(
targetDataType.Id,
targetDataType.PropertyEditorAlias,
content.ContentType);

// Try convert source to object
var converted = properyType.ConvertSourceToObject(value, false);
if (converted is T)
return (T)converted;
if (converted is T) return (T)converted;

var convertAttempt = converted.TryConvertTo<T>();
if (convertAttempt.Success)
return convertAttempt.Result;
if (convertAttempt.Success) return convertAttempt.Result;

// Try convert data to source
converted = properyType.ConvertDataToSource(value, false);
if (converted is T)
return (T)converted;
if (converted is T) return (T)converted;

convertAttempt = converted.TryConvertTo<T>();
if (convertAttempt.Success)
return convertAttempt.Result;
if (convertAttempt.Success) return convertAttempt.Result;

// Try just converting
var convertAttempt2 = value.TryConvertTo<T>();
if (convertAttempt2.Success)
return convertAttempt2.Result;
if (convertAttempt2.Success) return convertAttempt2.Result;

// Still not right type so return default value
return defaultValue;
Expand Down

0 comments on commit 46b7443

Please sign in to comment.