Skip to content

Commit

Permalink
Fixes secondary issue in #44. This is actually reverting some code ba…
Browse files Browse the repository at this point in the history
…ck to do with issue #19 in which we made ConvertSourceToObject get called first. At the time, I had forgotten why I had them in the order I did, but I remembered the reason now and so added a comment also so we don't forget. ConvertDataToSource MUST occur before ConvertSourceToObject.
  • Loading branch information
mattbrailsford committed Mar 27, 2016
1 parent 46bc4a1 commit e8c768d
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/Our.Umbraco.Vorto/Extensions/IPublishedContentExtensions.cs
Expand Up @@ -94,22 +94,27 @@ public static class IPublishedContentExtensions
content.ContentType);

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

var convertAttempt = converted.TryConvertTo<T>();
// We try this first as the value is stored as JSON not
// as XML as would occur in the XML cache as in the act
// of concerting to XML this would ordinarily get called
// but with JSON it doesn't, so we try this first
var converted1 = properyType.ConvertDataToSource(value, false);
if (converted1 is T) return (T)converted1;

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

// Try convert data to source
converted = properyType.ConvertDataToSource(value, false);
if (converted is T) return (T)converted;
// If the source value isn't right, try converting to object
var converted2 = properyType.ConvertSourceToObject(converted1, false);
if (converted2 is T) return (T)converted2;

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

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

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

0 comments on commit e8c768d

Please sign in to comment.